java利用poi导出word文档
项目中,有时候需要使用poi实现将固定数据导入word中
效果图:
代码:
package poiword;import java.io.File;
import java.io.FileOutputStream;
import java.math.BigInteger;import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STShd;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;public class TestPOIWordOut {public static void main(String[] args) {//Blank Document XWPFDocument document= new XWPFDocument(); //Write the Document in file system FileOutputStream out;try {out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/poi_word.docx"));//添加标题 XWPFParagraph titleParagraph = document.createParagraph(); //设置段落居中 titleParagraph.setAlignment(ParagraphAlignment.CENTER); XWPFRun titleParagraphRun = titleParagraph.createRun(); titleParagraphRun.setText("自学指导(标题)"); titleParagraphRun.setColor("000000"); titleParagraphRun.setFontSize(20); //段落 XWPFParagraph firstParagraph = document.createParagraph(); XWPFRun run = firstParagraph.createRun(); run.setText(" 自学遇到问题怎么办?"); run.setColor("698869"); run.setFontSize(16); //设置段落背景颜色 CTShd cTShd = run.getCTR().addNewRPr().addNewShd(); cTShd.setVal(STShd.CLEAR); cTShd.setFill("97FF45"); //换行 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(": xxxx"); //表格第二行 XWPFTableRow infoTableRowTwo = infoTable.createRow(); infoTableRowTwo.getCell(0).setText("姓名"); infoTableRowTwo.getCell(1).setText(": xxxx"); //表格第三行 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(": x"); //表格第五行 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("职位"); //表格第二行 XWPFTableRow comTableRowTwo = ComTable.createRow(); comTableRowTwo.getCell(0).setText("20xx-xx-xx"); comTableRowTwo.getCell(1).setText("至今"); comTableRowTwo.getCell(2).setText("自学指导网站"); comTableRowTwo.getCell(3).setText("Java开发工程师"); //表格第三行 XWPFTableRow comTableRowThree = ComTable.createRow(); comTableRowThree.getCell(0).setText("20xx-xx-xx"); comTableRowThree.getCell(1).setText("至今"); comTableRowThree.getCell(2).setText("s.jf3q.com"); comTableRowThree.getCell(3).setText("Java开发工程师"); document.write(out); out.close(); } catch (Exception e1) {// TODO Auto-generated catch blocke1.printStackTrace();} System.out.println("create_table document written success."); }
}
注意事项:最核心的就是需要的jar包不能下载错哦。
java利用poi导出word文档相关推荐
- java利用POI替换word文档中的标签
java利用POI替换word文档中的标签 <dependency><groupId>org.apache.poi</groupId><artifactId& ...
- java用poi导出word,Java使用POI导出Word文档的操作教程,poiword
Java使用POI导出Word文档的操作教程,poiword 一.主要pom依赖 org.apache.poi poi-ooxml 3.16 二.需要导出word模板 三.相关导出代码 package ...
- JAVA使用POI导出Word文档和Excel文档
导包 注意:POI包的版本,不同版本poi导出API会有差别!!!! <dependency><groupId>org.apache.poi</groupId>&l ...
- Java使用POI导出Word文档
一.主要pom依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-oox ...
- JAVA 利用poi EXCLE模板文档导出数据
JAVA 利用poi EXCLE模板文档导出数据 1.导入jar包 下载地址:添加链接描述 提取码:xqkg 2.EXCLE模板 3.代码示例 package utill;import java.io ...
- [实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能)
[实用][更新中]Java Apache POI 打印Word文档工具(含文本替换,动态表格功能) 基于Apache POI对Word进行操作 一.基于Apache POI封装的word文档工具V1. ...
- java用poi导出wrod文档支持图片并解释代码
使用POI(Poor Obfuscation Implementation,即弱混淆实现)库可以很方便地在Java代码中导出Word文档,同时还支持插入图片. 下面是一个简单的例子,它创建了一个包含一 ...
- 使用POI导出word文档
使用POI导出word文档 步骤一.创建一个需要导出的word文档模板.将表里需要填充的数据对应好要填充的键最后在表的最前方加入一个书签(一个table一个书签) 步骤二.工具类 public cla ...
- JAVA根据word模版使用poi导出word文档,包含图片、文字
模版word文件,内容和表格都已处理,保留了字体和样式,图片可以指定大小,docx类型的模版字段有问题的话,整个字段复制进去即可,不要手敲${} 生成的word文件 /*** word工具类** @a ...
最新文章
- docker 安装部署 activemq ActiveMQ
- 怎么在mac下运行映像dmg_仅用Mac OS X系统映像文件(dmg)安装系统
- [Python人工智能] 六.TensorFlow实现分类学习及MNIST手写体识别案例
- 有赞组件vant密码输入框input在微信小程序里隐藏显示密码有问题
- 汇编使用跳转实现1累加到100的和
- MyEclipse-6.5注冊码生成器源代码
- [Angular 2] Nesting Elements in Angular 2 Components with ng-content (AKA Angular 2 Transclusion)
- 5、overflow、hover
- Android5.0特性阴影效果和裁剪
- 此计算机上缺少vc2005_sp1_with_atl_fix_redist
- 【四足机器人那些事儿】MiniCheetah中的MPC控制
- DeepLearning | Broad Learning System 宽度学习系统 : 高效增量式浅层神经网络
- 联想服务器安装win10系统安装教程,联想win10企业版操作系统安装步骤详解
- 二元函数可微与切平面存在的关系
- 关于mis系统开发的一些配置含义
- 四、初识python编程-程序结构(课时11-13)
- 蝴蝶展翅鸿蒙云飞,关于蝴蝶的伤感句子 开辟鸿蒙,谁为情种?都只为风月情浓...
- java常见正则表达式用法
- 几种比较好看的颜色代码
- 关于位运算符号的辨识及运用示例
热门文章
- 如何获取应用宝APP ID
- 忠告:使用TNT版本PD的不要升级新版本Mac系统
- Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
- 【Arduino 连接 SD 卡模块实现数据读写】
- matlab获取地图边界,科学网—提取百度地图县域的矢量边界 - 张乐乐的博文
- SQL Server 2005无日志文件附加数据库
- HTML 标签 (HTML超文本标记语言)
- IntelliJ IDEA pycharm webstorm 激活
- Typora极简教程
- PD3.0 PPS限流