在开发中有时候我们需要导出 word文档。最近因为需要做一个生成word文件的功能。就将这块拿出来和大家分享。

生成word文件和我们写word文档是相同的概念,只不过在这里我们换成了用代码来操作。下面的例子中主要有添加页眉,页脚,正文(段落,表格)。在正文中,段落包含文字字体和背景的设置。表格主要是数据的填充和样式(有无边框)。这里写的例子给出的内容只是Java POI 方式生成word文件的极少数的一些方法,需要使用更多方法的还是要自己根据自己的需求去查看API。

看到很多小伙伴反应不能用的问题,这里我又重新把代码下载下来生成了一次试试。确实是没有问题。以前使用的是jdk6,最后一个版本使用的是jdk8.我再把我的导包情况贴出来。供大家参考,生成的文件office 和wps打开均没有问题。

那就直接先上代码吧:

package com.seawater.controller;import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;/*** Created by wan.tao on 2018/10/23.*/
public class WordExportController {public static void main(String[] args)throws Exception {//Blank DocumentXWPFDocument document= new XWPFDocument();//Write the Document in file systemFileOutputStream out = new FileOutputStream(new File("create_table.docx"));//添加标题XWPFParagraph titleParagraph = document.createParagraph();//设置段落居中titleParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFRun titleParagraphRun = titleParagraph.createRun();titleParagraphRun.setText("Java PoI");titleParagraphRun.setColor("000000");titleParagraphRun.setFontSize(20);//段落XWPFParagraph firstParagraph = document.createParagraph();XWPFRun run = firstParagraph.createRun();run.setText("Java POI 生成word文件。");run.setColor("696969");run.setFontSize(16);//设置段落背景颜色CTShd cTShd = run.getCTR().addNewRPr().addNewShd();cTShd.setVal(STShd.CLEAR);cTShd.setFill("97FFFF");//换行XWPFParagraph paragraph1 = document.createParagraph();XWPFRun paragraphRun1 = paragraph1.createRun();paragraphRun1.setText("\r");//基本信息表格XWPFTable infoTable = document.createTable();//去表格边框infoTable.getCTTbl().getTblPr().unsetTblBorders();//列宽自动分割CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();infoTableWidth.setType(STTblWidth.DXA);infoTableWidth.setW(BigInteger.valueOf(9072));//表格第一行XWPFTableRow infoTableRowOne = infoTable.getRow(0);infoTableRowOne.getCell(0).setText("职位");infoTableRowOne.addNewTableCell().setText(": Java 开发工程师");//表格第二行XWPFTableRow infoTableRowTwo = infoTable.createRow();infoTableRowTwo.getCell(0).setText("姓名");infoTableRowTwo.getCell(1).setText(": seawater");//表格第三行XWPFTableRow infoTableRowThree = infoTable.createRow();infoTableRowThree.getCell(0).setText("生日");infoTableRowThree.getCell(1).setText(": xxx-xx-xx");//表格第四行XWPFTableRow infoTableRowFour = infoTable.createRow();infoTableRowFour.getCell(0).setText("性别");infoTableRowFour.getCell(1).setText(": 男");//表格第五行XWPFTableRow infoTableRowFive = infoTable.createRow();infoTableRowFive.getCell(0).setText("现居地");infoTableRowFive.getCell(1).setText(": xx");//两个表格之间加个换行XWPFParagraph paragraph = document.createParagraph();XWPFRun paragraphRun = paragraph.createRun();paragraphRun.setText("\r");//工作经历表格XWPFTable ComTable = document.createTable();//列宽自动分割CTTblWidth comTableWidth = ComTable.getCTTbl().addNewTblPr().addNewTblW();comTableWidth.setType(STTblWidth.DXA);comTableWidth.setW(BigInteger.valueOf(9072));//表格第一行XWPFTableRow comTableRowOne = ComTable.getRow(0);comTableRowOne.getCell(0).setText("开始时间");comTableRowOne.addNewTableCell().setText("结束时间");comTableRowOne.addNewTableCell().setText("公司名称");comTableRowOne.addNewTableCell().setText("title");//表格第二行XWPFTableRow comTableRowTwo = ComTable.createRow();comTableRowTwo.getCell(0).setText("2016-09-06");comTableRowTwo.getCell(1).setText("至今");comTableRowTwo.getCell(2).setText("seawater");comTableRowTwo.getCell(3).setText("Java开发工程师");//表格第三行XWPFTableRow comTableRowThree = ComTable.createRow();comTableRowThree.getCell(0).setText("2016-09-06");comTableRowThree.getCell(1).setText("至今");comTableRowThree.getCell(2).setText("seawater");comTableRowThree.getCell(3).setText("Java开发工程师");CTSectPr sectPr = document.getDocument().getBody().addNewSectPr();XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(document, sectPr);//添加页眉CTP ctpHeader = CTP.Factory.newInstance();CTR ctrHeader = ctpHeader.addNewR();CTText ctHeader = ctrHeader.addNewT();String headerText = "Java POI create MS word file.";ctHeader.setStringValue(headerText);XWPFParagraph headerParagraph = new XWPFParagraph(ctpHeader, document);//设置为右对齐headerParagraph.setAlignment(ParagraphAlignment.RIGHT);XWPFParagraph[] parsHeader = new XWPFParagraph[1];parsHeader[0] = headerParagraph;policy.createHeader(XWPFHeaderFooterPolicy.DEFAULT, parsHeader);//添加页脚CTP ctpFooter = CTP.Factory.newInstance();CTR ctrFooter = ctpFooter.addNewR();CTText ctFooter = ctrFooter.addNewT();String footerText = "http://blog.csdn.net/zhouseawater";ctFooter.setStringValue(footerText);XWPFParagraph footerParagraph = new XWPFParagraph(ctpFooter, document);headerParagraph.setAlignment(ParagraphAlignment.CENTER);XWPFParagraph[] parsFooter = new XWPFParagraph[1];parsFooter[0] = footerParagraph;policy.createFooter(XWPFHeaderFooterPolicy.DEFAULT, parsFooter);document.write(out);out.close();System.out.println("create_table document written success.");}}

代码我放到这一个文件当中了。下面我就一些代码做一些解释,因为有的是我在做的过程中遇到的问题。大部分的代码大家都是一眼就可以看懂的。

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

这段代码设置段落的背景颜色。

如果我们的表格不需要边框呢就加下面的代码:

infoTable.getCTTbl().getTblPr().unsetTblBorders();

infoTable换成自己的table名称就可以了。
建立一个表格的时候设置列宽跟随内容伸缩

CTTblWidth infoTableWidth = infoTable.getCTTbl().addNewTblPr().addNewTblW();
infoTableWidth.setType(STTblWidth.DXA);
infoTableWidth.setW(BigInteger.valueOf(9072));

其他的代码我就不解释了。运行就可以得到我们的word文件了。

如图:

Java POI 生成Word文档相关推荐

  1. Java poi 生成word文档并下载

    我使用的是Springboot框架开发的.首先需要在pom.xml文件中引入以下maven包: <dependency><groupId>org.apache.poi</ ...

  2. POI生成word文档,包括标题,段落,表格,统计图(非图片格式)

    Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...

  3. 关于用java编写生成word文档,动态添加数据到word文档的一些心得

    关于用java编写生成word文档,动态添加数据到word文档的一些心得,经过翻阅了无数的有用的和无用的资料以后,总算找到了一种靠谱的方法 1.概述 经过反反复复的查阅资料,总算找到了一个靠谱的生成w ...

  4. poi生成word文档,插入图片,echar报表生成到word,word表格

    poi生成word文档,word表格,将echar报表生成到word 项目中用到生成word报表,报表中有表格的合并 .页眉.表格中会有报表图片.然后查找了网上的资料,利用echar生成柱状图,然后已 ...

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

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

  6. POI生成word文档,图片显示为空白或不显示

    我想要用java,通过poi实现word文档中插入文字和图片来发送邮箱附件.但是发现在对word操作中,图片是白的,size如果设置小了直接没有图片.  经过百度 参考解决 Java poi 3.8 ...

  7. Java freemarker 生成word文档

    工具类 package cn.gh.util;import freemarker.template.Configuration; import freemarker.template.Template ...

  8. 关于Apache / poi 生成word文档之后不能正常换行的问题

    近期公司项目有个把文本转成word文档的功能,开始使用io操作输出文件的方式(后缀名是docx),使用手机自带的文档浏览工具打开是没有问题的,但是在电脑上用微软office就打开有问题了,于是找了三方 ...

  9. java itext 生成word文档

    /**       *  创建word文档 步骤:          * 1,建立文档          * 2,创建一个书写器          * 3,打开文档          * 4,向文档中 ...

最新文章

  1. Win64 驱动内核编程-12.回调监控进线程创建和退出
  2. 若依后端实现pdfjs预览PDF文件
  3. 用es5实现es6的promise,彻底搞懂promise的原理
  4. Lucene学习入门——核心类API
  5. rocketmq在Kubernetes(k8s)中的集群配置,2m-2s-async:多Master多Slave模式,异步复制
  6. 基于Python实现的遗传算法求TSP问题
  7. android在体检报告叫什么,体检报告检测分析app
  8. python聊天小程序支持私聊和多人_Python 使用 django 框架实现多人在线匿名聊天的小程序...
  9. 数学界再出变态神人!竟用一个比基尼方程,暴力吊打美国数学家!看完我惊了......
  10. 图片转文字、视频转文字 超赞网页分享
  11. NB-IoT SIM7000C调试笔记 01 NB-IoT及GPRS加网测试
  12. 计算机公共课5——演示文稿软件 PowerPoint 2010
  13. 维修汽车服务器,汽修门店没有这些工具,千万别帮车主维修汽车电脑!
  14. 2021年氯化工艺考试总结及氯化工艺复审考试
  15. C++ 对象模型 第二章 构造函数语意学
  16. 学习英文-学以致用【场景:美式音标】
  17. 酒店点餐系统开发详解(三)
  18. THHP 与 HTTPS 有什么区别?
  19. 如何应对运维中出现的突发事件问题
  20. 前景理论-风险决策分析的思维模型

热门文章

  1. 关于电商推广一些常见问题的总结
  2. 用C语言实现辅助教学系统,老师无处安放的魅力呀!
  3. 在北京的第四年,继续颠肺流离~
  4. AJPFX讲解java关键词过滤算法
  5. docker容器/etc/hosts文件
  6. docker中的Volume
  7. bzoj1968【AHOI2005】COMMON 约数研究
  8. 工信部专家:中国传感器最大的问题是什么?怎么解决?(最新观点)
  9. python excel 数据匹配_python初学者,如何快速匹配excel任务
  10. QQ支持微信登录,手机QQ与微信联手!