本文整理汇总了Java中org.apache.poi.hwpf.HWPFDocument.getRange方法的典型用法代码示例。如果您正苦于以下问题:Java HWPFDocument.getRange方法的具体用法?Java HWPFDocument.getRange怎么用?Java HWPFDocument.getRange使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.poi.hwpf.HWPFDocument
的用法示例。
在下文中一共展示了HWPFDocument.getRange方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: replaceWordDoc
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
public static void replaceWordDoc(String inPath, String outPath, Map<String, String> context) {
Validate.notBlank(inPath);
Validate.notBlank(outPath);
Validate.notNull(context);
try (FileInputStream in = new FileInputStream(new File(inPath));
FileOutputStream out = new FileOutputStream(outPath, false)) {
HWPFDocument hdt = new HWPFDocument(in);
Range range = hdt.getRange();
for (Map.Entry<String, String> entry : context.entrySet()) {
range.replaceText(entry.getKey(), entry.getValue());
}
hdt.write(out);
} catch (IOException e) {
e.printStackTrace();
}
}
示例2: saveDoc
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
/**
* Method to save the file by parameters in doc format
*
* @param toSave The file where the information will be saved
*/
private void saveDoc(File toSave) {
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("down/empty.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Range range = doc.getRange();
Paragraph parContainer = range.insertAfter(new ParagraphProperties(), 0);
for (String para : paragraphs) {
parContainer.setSpacingAfter(200);
parContainer.insertAfter(para);
parContainer = range.insertAfter(new ParagraphProperties(), 0);
}
FileOutputStream fos = new FileOutputStream(toSave);
doc.write(fos);
fos.close();
} catch (Exception e) {
Logger.getGlobal().log(Level.SEVERE, e.getMessage() + "\n" + Arrays.toString(e.getStackTrace()));
}
}
示例3: CreateDocFromTemplate
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
/**
* 创建Doc并保存
*
* @param templatePath 模板doc路径
* @param parameters 参数和值
* //* @param imageParameters 书签和图片
* @param savePath 保存doc的路径
* @return
*/
public static void CreateDocFromTemplate(String templatePath,
HashMap<String, String> parameters,
//HashMap<String, String> imageParameters,
String savePath)
throws Exception {
@Cleanup InputStream is = DocProducer.class.getResourceAsStream(templatePath);
HWPFDocument doc = new HWPFDocument(is);
Range range = doc.getRange();
//把range范围内的${}替换
for (Map.Entry<String, String> next : parameters.entrySet()) {
range.replaceText("${" + next.getKey() + "}",
next.getValue()
);
}
@Cleanup OutputStream os = new FileOutputStream(savePath);
//把doc输出到输出流中
doc.write(os);
}
示例4: testReadByDoc
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
@Test
public void testReadByDoc() throws Exception {
InputStream is = new FileInputStream("E://video/doc/xiuParty/90Xiu-NEW/oss/90秀--oss与服务器端接口文档.doc");
POIFSFileSystem pfilesystem = new POIFSFileSystem(is);
HWPFDocument doc = new HWPFDocument(pfilesystem);
// // 输出书签信息
// this.printInfo(doc.getBookmarks());
// // 输出文本
// System.out.println(doc.getDocumentText());
Range range = doc.getRange();
// this.insertInfo(range);
this.printInfo(range);
// 读表格
this.readTable(range);
// 读列表
this.readList(range);
// 删除range
Range r = new Range(2, 5, doc);
r.delete();// 在内存中进行删除,如果需要保存到文件中需要再把它写回文件
// 把当前HWPFDocument写到输出流中
doc.write(new FileOutputStream("D:\\test.doc"));
IOUtils.closeQuietly(is);
}
示例5: processParagraphRequest
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
/**
* Processing of text requests.
*
* @param doc word document
* @param paragraphRef reference to paragraphs
* @return list of text values as {@code LinkedList<Value>}
*/
private LinkedList<Value> processParagraphRequest(HWPFDocument doc, String paraRef) {
LinkedList<Value> valList = new LinkedList<Value>();
Range docRange = doc.getRange();
setStartEndParagraph(paraRef, "*");
if (this.startPara == -1 || this.endPara == -1) {
this.startPara = 0;
this.endPara = docRange.numParagraphs() - 1;
}
// -1 => all paragraphs requested
if (this.startPara < docRange.numParagraphs()) {
if (!(this.endPara < docRange.numParagraphs())) {
this.endPara = docRange.numParagraphs() - 1;
}
for (Integer pPos = this.startPara; pPos <= this.endPara; pPos++) {
Paragraph p = docRange.getParagraph(pPos);
Integer subURI = pPos + 1;
valList.add(getParagraph(p, subURI.toString()));
}
}
return valList;
}
示例6: read
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
public ArrayList<TableData> read(String path) throws IOException {
FileInputStream in = new FileInputStream(new File(path));
HWPFDocument hwpf = new HWPFDocument(in);
Range range = hwpf.getRange();// 得到文档的读取范围
TableIterator it = new TableIterator(range);
ArrayList<TableData> list=new ArrayList<TableData>();
int count=0;
// 迭代文档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
TableData data=new TableData();
// 迭代行,默认从0开始
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
// 迭代列,默认从0开始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);// 取得单元格
// 取得单元格的内容
StringBuffer sb=new StringBuffer();
for (int k = 0; k < td.numParagraphs(); k++) {
Paragraph para = td.getParagraph(k);
sb.append(para.text());
}
String string=sb.toString().trim();
if(i==0&&j==1)
{
data.setClazz(string);
}
else if(i==1&&j==1)
{
data.setName(string);
}
else if(i==2&&j==1)
{
data.setLifeCircle(string);
}
else if(i==2&&j==3)
{
data.setTheme(string);
}
else if(i==3&&j==3)
{
data.setForm(string);
}
else if(i==4&&j==1)
{
data.setKeywords(string);
}
else if(i==9&&j==1)
{
data.setContent(string);
}
}
}
// show("第"+count+"条:"+data.toString());
count+=1;
list.add(data);
}
return list;
}
示例7: main
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
HWPFDocument doc = new HWPFDocument(new FileInputStream(
"data/document.doc"));
Range range = doc.getRange();
String text = range.text();
System.out.println("Range: " + text);
}
示例8: main
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
String dataPath = "src/featurescomparison/workingwithranges/accessranges/data/";
HWPFDocument doc = new HWPFDocument(new FileInputStream(
dataPath + "document.doc"));
Range range = doc.getRange();
String text = range.text();
System.out.println("Range: " + text);
}
示例9: getWordParagraphText
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
/**
* Get the text from the word file, as an array with one String
* per paragraph
*/
public static String[] getWordParagraphText(HWPFDocument doc) {
String[] ret;
// Extract using the model code
try {
Range r = doc.getRange();
ret = new String[r.numParagraphs()];
for(int i=0; i<ret.length; i++) {
Paragraph p = r.getParagraph(i);
ret[i] = p.text();
// Fix the line ending
if(ret[i].endsWith("\r")) {
ret[i] = ret[i] + "\n";
}
}
}
catch(Exception e) {
// Something's up with turning the text pieces into paragraphs
// Fall back to ripping out the text pieces
ret = new String[1];
ret[0] = getWordTextFromPieces(doc);
}
return ret;
}
示例10: writePDFFromDoc
import org.apache.poi.hwpf.HWPFDocument; //导入方法依赖的package包/类
public static void writePDFFromDoc(
final String docFilePath,
final String pdfFilePath) throws SSErr{
try{
final Document document = new Document();
final POIFSFileSystem fs = new POIFSFileSystem(openFileForRead(docFilePath));
final HWPFDocument word = new HWPFDocument (fs);
final WordExtractor we = new WordExtractor (word);
final OutputStream out = openOrCreateFileWithPathForWrite(pdfFilePath);
final PdfWriter writer = PdfWriter.getInstance(document, out);
final Range range = word.getRange();
document.open();
writer.setPageEmpty(true);
document.newPage();
writer.setPageEmpty(true);
String[] paragraphs = we.getParagraphText();
for (int i = 0; i < paragraphs.length; i++) {
org.apache.poi.hwpf.usermodel.Paragraph pr = range.getParagraph(i);
// CharacterRun run = pr.getCharacterRun(i);
// run.setBold(true);
// run.setCapitalized(true);
// run.setItalic(true);
paragraphs[i] = paragraphs[i].replaceAll("\\cM?\r?\n", "");
System.out.println("Length:" + paragraphs[i].length());
System.out.println("Paragraph" + i + ": " + paragraphs[i].toString());
// add the paragraph to the document
document.add(new Paragraph(paragraphs[i]));
}
document.close();
}catch(Exception error){
SSServErrReg.regErrThrow(error);
}
}