Java用POI生成DOC

2025-05-11 00:05:52

1、导入两个必须的Jar包:poi-3.10.1.jar和poi-scratchpad-3.10.1.jar

Java用POI生成DOC

4、使用模板导出Doc的话需要先定义好模板内容。例如模板内容如下图所示。

Java用POI生成DOC

6、调用方法如下:Map<String, String> contentMap=new HashMap<String, String>();contentMap.put("name", "飞翔家族");contentMap.put("age", "123");contentMap.put("email", "1231231231@123.com");HWPFDocument document = replaceDoc("C:\\template.doc", contentMap); if(document != null){ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { document.write(byteArrayOutputStream); OutputStream outputStream = new FileOutputStream(destFile); outputStream.write(byteArrayOutputStream.toByteArray()); outputStream.close(); } catch (IOException e) { } }

7、使用模板导出DOC文件我们还可以使用其他的方法。我们还是先需要制作好DOC模板文件,然后另存为XML文件。使用velocity,将数据填充,导出成DOC文件。

8、这里介绍的都是最简单的方法,直接导出文本Word,如果要导出复杂一点的Word,请再查阅。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢