maven的pom

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/org.apache.poi.xwpf.converter.core -->
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>org.apache.poi.xwpf.converter.core</artifactId>
<version>1.0.6</version>
</dependency>

Java代码

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

背景颜色

1 //设置段落背景颜色
2 CTShd cTShd = run.getCTR().addNewRPr().addNewShd();
3 cTShd.setVal(STShd.CLEAR);
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. 成本节省 50%,9人团队使用函数计算开发 wolai 在线文档应用

    简介: 通过使用函数计算,wolai 的前端工程师们就可以把从前到后的一整套开发流程负责起来,我们的研发迭代速度非常快.   ​ 作者| 马锐拉(wolai.com 创始人) 我们的日常工作场景几乎离 ...

  2. 有赞 Flink 实时任务资源优化探索与实践

    简介: 目前有赞实时计算平台对于 Flink 任务资源优化探索已经走出第一步. 随着 Flink K8s 化以及实时集群迁移完成,有赞越来越多的 Flink 实时任务运行在 K8s 集群上,Flink ...

  3. 基于 EventBridge 构建 SaaS 应用集成方案

    ​简介:事件源是事件驱动的基石,如何获取更多事件源也是 EventBridge 一直在探索和尝试的方向.针对市场上其他云厂商和垂直领域的 Saas 服务,EventBridge 发布了 HTTP So ...

  4. [FE] nvm-windows: Microsoft/NPM/Google 推荐 Windows 的 Node.js 版本管理器, posix 的 nvm-sh/nvm

    1. 到 github 下载 nvm-setup.zip 并安装. Releases · coreybutler/nvm-windows (github.com) 2. 安装一个版本的 nodejs. ...

  5. 【爬虫+情感判定+Top10高频词+词云图】"乌克兰"油管热评python舆情分析

    目录 一.分析背景 二.整体思路 三.代码讲解 3.1 爬虫采集 3.2 情感判定 3.3 Top10高频词 3.4 词云图 四.得出结论 五.同步视频演示 六.附完整源码 一.分析背景 乌克兰局势这 ...

  6. 获取list集合中最大值、最小值及索引值

    一.获取最大最小值的同时,获取到最大/小值在list中的索引值 public static void main(String[] args) { List<Integer> numList ...

  7. Seata原理浅析

    前言 Seata是阿里开源的分布式事务解决方案,本文将详细介绍 Seata 的事务模式.原理以及使用.了解之前需清楚什么是分布式事务. 一.什么是 Seata Seata 是一款开源的分布式事务解决方 ...

  8. 鸿蒙HarmonyOS实战-Stage模型(ExtensionAbility组件)

    一.ExtensionAbility组件 1.概念 HarmonyOS中的ExtensionAbility组件是一种能够扩展系统功能的能力组件.它可以通过扩展系统能力接口,为应用程序提供一些特定的功能 ...

  9. vulnhub靶场-->MATRIX-BREAKOUT: 2 MORPHEUS

    靶机下载地址 MATRIX-BREAKOUT: 2 MORPHEUS << 点我下载 开始打靶 IP发现 nmap扫描网段发现靶机ip:192.168.111.139 端口发现 对靶机进行 ...

  10. 源码分析——MyBatis核心接口SqlSession的实现原理

    在上一篇文章中(<MyBatis动态代理调用过程源码分析>),我们知道了MyBatis动态代理的核心是MapperProxy,在它内部封装了动态代理的调用逻辑,而我们也知道了在使用动态代理 ...