maven的pom

  1. <dependency>
  2. <groupId>org.apache.poi</groupId>
  3. <artifactId>ooxml-schemas</artifactId>
  4. <version>1.1</version>
  5. </dependency>
  6. <!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core -->
  7. <dependency>
  8. <groupId>fr.opensagres.xdocreport</groupId>
  9. <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
  10. <version>1.0.6</version>
  11. </dependency>

Java代码

  1. 1 package com.seawater.controller;
  2. 2
  3. 3 import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
  4. 4 import org.apache.poi.xwpf.usermodel.*;
  5. 5 import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
  6. 6
  7. 7 import java.io.File;
  8. 8 import java.io.FileOutputStream;
  9. 9 import java.math.BigInteger;
  10. 10
  11. 11
  12. 12 /**
  13. 13 * Created by zhouhs on 2017/1/9.
  14. 14 */
  15. 15 public class WordExportController {
  16. 16
  17. 17 public static void main(String[] args)throws Exception {
  18. 18 //Blank Document
  19. 19 XWPFDocument document= new XWPFDocument();
  20. 20
  21. 21 //Write the Document in file system
  22. 22 FileOutputStream out = new FileOutputStream(new File("create_table.docx"));
  23. 23
  24. 24
  25. 25 //添加标题
  26. 26 XWPFParagraph titleParagraph = document.createParagraph();
  27. 27 //设置段落居中
  28. 28 titleParagraph.setAlignment(ParagraphAlignment.CENTER);
  29. 29
  30. 30 XWPFRun titleParagraphRun = titleParagraph.createRun();
  31. 31 titleParagraphRun.setText("Java PoI");
  32. 32 titleParagraphRun.setColor("000000");
  33. 33 titleParagraphRun.setFontSize(20);
  34. 34
  35. 35
  36. 36 //段落
  37. 37 XWPFParagraph firstParagraph = document.createParagraph();
  38. 38 XWPFRun run = firstParagraph.createRun();
  39. 39 run.setText("Java POI 生成word文件。");
  40. 40 run.setColor("696969");
  41. 41 run.setFontSize(16);
  42. 42
  43. 43 //设置段落背景颜色
  44. 44 CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
  45. 45 cTShd.setVal(STShd.CLEAR);
  46. 46 cTShd.setFill("97FFFF");
  47. 47
  48. 48
  49. 49 //换行
  50. 50 XWPFParagraph paragraph1 = document.createParagraph();
  51. 51 XWPFRun paragraphRun1 = paragraph1.createRun();
  52. 52 paragraphRun1.setText("\r");
  53. 53
  54. 54
  55. 55 //基本信息表格
  56. 56 XWPFTable infoTable = document.createTable();
  57. 57 //去表格边框
  58. 58 infoTable.getCTTbl().getTblPr().unsetTblBorders();
  59. 59
  60. 60
  61. 61 //列宽自动分割
  62. 62 CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
  63. 63 infoTableWidth.setType(STTblWidth.DXA);
  64. 64 infoTableWidth.setW(BigInteger.valueOf(9072));
  65. 65
  66. 66
  67. 67 //表格第一行
  68. 68 XWPFTableRow infoTableRowOne = infoTable.getRow(0);
  69. 69 infoTableRowOne.getCell(0).setText("职位");
  70. 70 infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");
  71. 71
  72. 72 //表格第二行
  73. 73 XWPFTableRow infoTableRowTwo = infoTable.createRow();
  74. 74 infoTableRowTwo.getCell(0).setText("姓名");
  75. 75 infoTableRowTwo.getCell(1).setText(": seawater");
  76. 76
  77. 77 //表格第三行
  78. 78 XWPFTableRow infoTableRowThree = infoTable.createRow();
  79. 79 infoTableRowThree.getCell(0).setText("生日");
  80. 80 infoTableRowThree.getCell(1).setText(": xxx-xx-xx");
  81. 81
  82. 82 //表格第四行
  83. 83 XWPFTableRow infoTableRowFour = infoTable.createRow();
  84. 84 infoTableRowFour.getCell(0).setText("性别");
  85. 85 infoTableRowFour.getCell(1).setText(": 男");
  86. 86
  87. 87 //表格第五行
  88. 88 XWPFTableRow infoTableRowFive = infoTable.createRow();
  89. 89 infoTableRowFive.getCell(0).setText("现居地");
  90. 90 infoTableRowFive.getCell(1).setText(": xx");
  91. 91
  92. 92
  93. 93 //两个表格之间加个换行
  94. 94 XWPFParagraph paragraph = document.createParagraph();
  95. 95 XWPFRun paragraphRun = paragraph.createRun();
  96. 96 paragraphRun.setText("\r");
  97. 97
  98. 98
  99. 99
  100. 100 //工作经历表格
  101. 101 XWPFTable ComTable = document.createTable();
  102. 102
  103. 103
  104. 104 //列宽自动分割
  105. 105 CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();
  106. 106 comTableWidth.setType(STTblWidth.DXA);
  107. 107 comTableWidth.setW(BigInteger.valueOf(9072));
  108. 108
  109. 109 //表格第一行
  110. 110 XWPFTableRow comTableRowOne = ComTable.getRow(0);
  111. 111 comTableRowOne.getCell(0).setText("开始时间");
  112. 112 comTableRowOne.addNewTableCell().setText("结束时间");
  113. 113 comTableRowOne.addNewTableCell().setText("公司名称");
  114. 114 comTableRowOne.addNewTableCell().setText("title");
  115. 115
  116. 116 //表格第二行
  117. 117 XWPFTableRow comTableRowTwo = ComTable.createRow();
  118. 118 comTableRowTwo.getCell(0).setText("2016-09-06");
  119. 119 comTableRowTwo.getCell(1).setText("至今");
  120. 120 comTableRowTwo.getCell(2).setText("seawater");
  121. 121 comTableRowTwo.getCell(3).setText("Java开发工程师");
  122. 122
  123. 123 //表格第三行
  124. 124 XWPFTableRow comTableRowThree = ComTable.createRow();
  125. 125 comTableRowThree.getCell(0).setText("2016-09-06");
  126. 126 comTableRowThree.getCell(1).setText("至今");
  127. 127 comTableRowThree.getCell(2).setText("seawater");
  128. 128 comTableRowThree.getCell(3).setText("Java开发工程师");
  129. 129
  130. 130
  131. 131 CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();
  132. 132 XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);
  133. 133
  134. 134 //添加页眉
  135. 135 CTP ctpHeader = CTP.Factory.newInstance();
  136. 136 CTR ctrHeader = ctpHeader.addNewR();
  137. 137 CTText ctHeader = ctrHeader.addNewT();
  138. 138 String headerText = "Java POI create MS word file.";
  139. 139 ctHeader.setStringValue(headerText);
  140. 140 XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);
  141. 141 //设置为右对齐
  142. 142 headerParagraph.setAlignment(ParagraphAlignment.RIGHT);
  143. 143 XWPFParagraph[] parsHeader = new XWPFParagraph[1];
  144. 144 parsHeader[0] = headerParagraph;
  145. 145 policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);
  146. 146
  147. 147
  148. 148 //添加页脚
  149. 149 CTP ctpFooter = CTP.Factory.newInstance();
  150. 150 CTR ctrFooter = ctpFooter.addNewR();
  151. 151 CTText ctFooter = ctrFooter.addNewT();
  152. 152 String footerText = "http://blog.csdn.net/zhouseawater";
  153. 153 ctFooter.setStringValue(footerText);
  154. 154 XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);
  155. 155 headerParagraph.setAlignment(ParagraphAlignment.CENTER);
  156. 156 XWPFParagraph[] parsFooter = new XWPFParagraph[1];
  157. 157 parsFooter[0] = footerParagraph;
  158. 158 policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);
  159. 159
  160. 160
  161. 161 document.write(out);
  162. 162 out.close();
  163. 163 System.out.println("create_table document written success.");
  164. 164 }
  165. 165
  166. 166
  167. 167 }

背景颜色

  1. 1 //设置段落背景颜色
  2. 2 CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
  3. 3 cTShd.setVal(STShd.CLEAR);
  4. 4 cTShd.setFill("97FFFF");

Java POI 生成Word文档的更多相关文章

  1. POI生成WORD文档

    h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...

  2. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  3. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  4. Java POI 解析word文档

    实现步骤: 1.poi实现word转html 2.模型化解析html 3.html转Map数组 Map数组(数组的操作处理不做说明) 1.导jar包. 2.代码实现 package com.web.o ...

  5. [转载]Java动态生成word文档(图文并茂)

    很多情况下,软件开发者需要从数据库读取数据,然后将数据动态填充到手工预先准备好的Word模板文档里,这对于大批量生成拥有相同格式排版的正式文件非常有用,这个功能应用PageOffice的基本动态填充功 ...

  6. [原创]Java动态生成word文档(图文并茂)

    很多情况下,软件开发者需要从数据库读取数据,然后将数据动态填充到手工预先准备好的Word模板文档里,这对于大批量生成拥有相同格式排版的正式文件非常有用,这个功能应用PageOffice的基本动态填充功 ...

  7. java freemark生成word文档

    1.下载freemarker-2.3.19.jar 2.把要填充的内容用  ${title},${no}代替 3.用word 打开,保存为2003xml 4.打开生成xml文件,看下有没有把表达式  ...

  8. java POI往word文档中指定位置插入表格

    1.Service  demo import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a ...

  9. java poi 合并 word文档

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import jav ...

  10. velocity模板技术生成word文档

    本文介绍採用velocity技术在Java中生成word文档的方法. 1.新建一个word文档,编辑内容例如以下: 2.将上述word文档另存为htm格式的文件 3.新建一个Java Project项 ...

随机推荐

  1. 龙蜥开源内核追踪利器 Surftrace:协议包解析效率提升 10 倍! | 龙蜥技术

    ​简介:如何将网络报文与内核协议栈清晰关联起来精准追踪到关注的报文行进路径呢? ​ 文/系统运维 SIG Surftrace 是由系统运维 SIG 推出的一个 ftrace 封装器和开发编译平台,让用 ...

  2. 基于 Flink SQL 构建流批一体的 ETL 数据集成

    简介: 如何利用 Flink SQL 构建流批一体的 ETL 数据集成. 本文整理自云邪.雪尽在 Flink Forward Asia 2020 的分享,该分享以 4 个章节来详细介绍如何利用 Fli ...

  3. Serverless 场景排查问题利器 : 函数实例命令行操作

    ​简介:实例命令行功能的推出希望能消除用户使用 Serverless 的"最后一公里",直接将真实的函数运行环境展现给用户. 背景介绍 全托管的 Serverless 计算平台能给 ...

  4. [FAQ] "cannot refer to unexported name" in Golang ?

    Golang 项目中如果使用了其它模块中找不到的函数.常量等,都会提示 "cannot refer to unexported name". 遇到这种情况,要么是拼写错误了,要么是 ...

  5. 2019-11-29-dotnet-remoting-使用事件

    title author date CreateTime categories dotnet remoting 使用事件 lindexi 2019-11-29 10:20:1 +0800 2018-2 ...

  6. C语言结构体的内存分配

    一.结构体内存分配原则 原则一:结构体中元素按照定义顺序存放到内存中,但并不是紧密排列.从结构体存储的首地址开始 ,每一个元素存入内存中时,它都会认为内存是以自己的宽度来划分空间的,因此元素存放的位置 ...

  7. 01、Windows 排查

    Windows 分析排查 分析排查是指对 Windows 系统中的文件.进程.系统信息.日志记录等进行检测,挖掘 Windows 系统中是否具有异常情况 1.开机启动项检查 一般情况下,各种木马.病毒 ...

  8. DNS(7) -- 智能DNS实现

    目录 1. 智能DNS 1.1 智能DNS概述 1.2 ACL控制列表 1.3 智能DNS实现 1.3.1 bind-view功能 1.3.2 智能DNS场景实现 1.3.3 生产场景配置示例 1. ...

  9. 【漏洞分析】HPAY 攻击事件分析

    背景 造成本次攻击的原因是关键函数的鉴权不当,使得任意用户可以设置关键的变量值,从而导致攻击的发生. 被攻击合约:https://www.bscscan.com/address/0xe9bc03ef0 ...

  10. 日常Bug排查-连接突然全部关闭

    日常Bug排查-连接突然全部关闭 前言 日常Bug排查系列都是一些简单Bug的排查.笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材. Bug现场 最近碰到一个问题,一台机器上的连接数在达到 ...