JAVA通过模板生成DOCX文档

心已赠人 2022-07-12 22:29 389阅读 0赞

第二篇文章,代码更精简,增加生成图片的思路

大致流程:

Center

1.创建模板docx并取出document.xml

新建一个docx文档,放在D盘命名test_template.docx

Center 1

![Image 1][]

2.用winrar打开test_template.docx,取出word/document.xml

把xml文件格式化一下看起来更清晰,并且把要替换的内容用freemarker的指令代替,最后将文件重命名为test.xml放在D盘下面

Center 2

3.准备工作完毕,上代码了,这个类是把内容填充到xml

  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. import java.util.Map;
  5. import freemarker.template.Configuration;
  6. import freemarker.template.Template;
  7. import freemarker.template.TemplateException;
  8. public class XmlToExcel {
  9. private static XmlToExcel tplm = null;
  10. private Configuration cfg = null;
  11. private XmlToExcel() {
  12. cfg = new Configuration();
  13. try {
  14. // 注册tmlplate的load路径
  15. // cfg.setClassForTemplateLoading(this.getClass(), "/template/");
  16. cfg.setDirectoryForTemplateLoading(new File("D:/"));
  17. } catch (Exception e) {
  18. }
  19. }
  20. private static Template getTemplate(String name) throws IOException {
  21. if (tplm == null) {
  22. tplm = new XmlToExcel();
  23. }
  24. return tplm.cfg.getTemplate(name);
  25. }
  26. /**
  27. *
  28. * @param templatefile 模板文件
  29. * @param param 需要填充的内容
  30. * @param out 填充完成输出的文件
  31. * @throws IOException
  32. * @throws TemplateException
  33. */
  34. public static void process(String templatefile, Map param, Writer out) throws IOException, TemplateException {
  35. // 获取模板
  36. Template template = XmlToExcel.getTemplate(templatefile);
  37. template.setOutputEncoding("UTF-8");
  38. // 合并数据
  39. template.process(param, out);
  40. if (out != null) {
  41. out.close();
  42. }
  43. }
  44. }

4.这个类是把填充完毕的xml转成docx

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Enumeration;
  7. import java.util.zip.ZipEntry;
  8. import java.util.zip.ZipException;
  9. import java.util.zip.ZipFile;
  10. import java.util.zip.ZipOutputStream;
  11. /**
  12. * 其实docx属于zip的一种,这里只需要操作word/document.xml中的数据,其他的数据不用动
  13. *
  14. * @author yigehui
  15. *
  16. */
  17. public class XmlToDocx {
  18. /**
  19. *
  20. * @param documentFile 动态生成数据的docunment.xml文件
  21. * @param docxTemplate docx的模板
  22. * @param toFileName 需要导出的文件路径
  23. * @throws ZipException
  24. * @throws IOException
  25. */
  26. public void outDocx(File documentFile, String docxTemplate, String toFilePath) throws ZipException, IOException {
  27. try {
  28. File docxFile = new File(docxTemplate);
  29. ZipFile zipFile = new ZipFile(docxFile);
  30. Enumeration<? extends ZipEntry> zipEntrys = zipFile.entries();
  31. ZipOutputStream zipout = new ZipOutputStream(new FileOutputStream(toFilePath));
  32. int len = -1;
  33. byte[] buffer = new byte[1024];
  34. while (zipEntrys.hasMoreElements()) {
  35. ZipEntry next = zipEntrys.nextElement();
  36. InputStream is = zipFile.getInputStream(next);
  37. // 把输入流的文件传到输出流中 如果是word/document.xml由我们输入
  38. zipout.putNextEntry(new ZipEntry(next.toString()));
  39. if ("word/document.xml".equals(next.toString())) {
  40. InputStream in = new FileInputStream(documentFile);
  41. while ((len = in.read(buffer)) != -1) {
  42. zipout.write(buffer, 0, len);
  43. }
  44. in.close();
  45. } else {
  46. while ((len = is.read(buffer)) != -1) {
  47. zipout.write(buffer, 0, len);
  48. }
  49. is.close();
  50. }
  51. }
  52. zipout.close();
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. }

5.main方法调用

  1. public static void main(String[] args) throws IOException, TemplateException {
  2. try {
  3. // xml的文件名
  4. String xmlTemplate = "test.xml";
  5. // docx的路径和文件名
  6. String docxTemplate = "d:\\test_template.docx";
  7. // 填充完数据的临时xml
  8. String xmlTemp = "d:\\temp.xml";
  9. // 目标文件名
  10. String toFilePath = "d:\\test.docx";
  11. Writer w = new FileWriter(new File(xmlTemp));
  12. // 1.需要动态传入的数据
  13. Map<String, Object> p = new HashMap<String, Object>();
  14. List<String> students = new ArrayList<String>();
  15. students.add("张三");
  16. students.add("李四");
  17. students.add("王二");
  18. p.put("ddeptdept", "研发部门");
  19. p.put("ddatedate", "2016-12-15");
  20. p.put("dnamename", students);
  21. // 2.把map中的数据动态由freemarker传给xml
  22. XmlToExcel.process(xmlTemplate, p, w);
  23. // 3.把填充完成的xml写入到docx中
  24. XmlToDocx xtd = new XmlToDocx();
  25. xtd.outDocx(new File(xmlTemp), docxTemplate, toFilePath);
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. }

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,389人围观)

还没有评论,来说两句吧...

相关阅读

    相关 合并DOCX文档

    找了好久好久的方法,结果无意间发现了,感激上苍啊 只能合并DOCX噢,不能合并DOC,强制修改文件名后缀也是不可以的哟,所以又花了好久找上一篇文章的方法。 废话不多说,上代