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

The following examples show how to use org.apache.poi.hwpf.HWPFDocument#write() . 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: WordUtil.java    From javatech with Creative Commons Attribution Share Alike 4.0 International 7 votes vote down vote up
public static void setDocProperties(String filename) throws IOException {
    System.out.println("filename = [" + filename + "]");
    FileInputStream fis = new FileInputStream(new File(filename));
    HWPFDocument doc = new HWPFDocument(fis);

    SummaryInformation summaryInformation = doc.getSummaryInformation();
    summaryInformation.setAuthor("张鹏");
    summaryInformation.setLastAuthor("张鹏");
    DocumentSummaryInformation documentSummaryInformation = doc.getDocumentSummaryInformation();
    documentSummaryInformation.setCompany("张鹏");
    documentSummaryInformation.setDocumentVersion("1");

    FileOutputStream fos = new FileOutputStream(new File(filename));
    doc.write(fos);

    fos.close();
    doc.close();
    fis.close();
}
 
Example 2
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 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();
    }
}