Java Code Examples for org.apache.poi.hwpf.HWPFDocument#getRange()

The following examples show how to use org.apache.poi.hwpf.HWPFDocument#getRange() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DocProducer.java    From OfficeProducer with Apache License 2.0 6 votes vote down vote up
/**
 * 创建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);
}
 
Example 2
Source File: MSOfficeBox.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 3
Source File: WordUtils.java    From java-tutorial with MIT License 4 votes vote down vote up
/**
 * 向word模板中填充数据,生成新word
 * templePath 是word模板的路径,outPutPath是输出路径
 * 注意模板是doc导出的就是doc,docx导出的是docx,否则会错误,
 *
 * @param mapData    填充的数据
 * @param templePath 模板路径
 * @throws Exception
 */
public static void generateWord(HashMap mapData, String templePath, String outPutPath) throws Exception {
    //因为空格无法输出,过滤一下空格 用-代替
    Iterator<Map.Entry<String, String>> iter = mapData.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry = iter.next();
        Object key = entry.getKey();
        Object val = entry.getValue();
        if ("".equals(val)) {
            //空格无法输出
            mapData.put(key, "-");
        }
    }

    //先创建文件
    File file = new File(outPutPath);
    if (file.exists()) {
        file.delete();
    }
    file.getParentFile().mkdirs();
    file.createNewFile();

    try {
        //获取文件后缀
        String suffix = getSuffix(templePath);
        if (suffix.equalsIgnoreCase(DOCX_SUFFIX)) {
            XWPFDocument doc = WordExportUtil.exportWord07(templePath, mapData);
            FileOutputStream fos = new FileOutputStream(file);
            doc.write(fos);
            fos.close();
        } else if (suffix.equalsIgnoreCase(DOC_SUFFIX)) {
            HWPFDocument hwpfDocument = new HWPFDocument(new FileInputStream(templePath));
            Range range = hwpfDocument.getRange();
            getRange(range, mapData);
            FileOutputStream stream = new FileOutputStream(file);
            hwpfDocument.write(stream);
            stream.flush();
            stream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}