word与java文档相关的api
最近开发项目,需要读写word文档内容,并封装成一个对象。
1、引入相关jar包
注意maven导入的三个依赖必须是同一个版本,不然有可能会报错
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<!-- 读取Excel XLSX、PPTX、DOCX、-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>xdocreport</artifactId>
<version>1.0.6</version>
</dependency>
2、通过java生成word
使用springboot框架。通过wordApi自定义格式生成word
上代码
/**
* 生成word文档 文字
*
* @param resource
* @throws IOException
*/
@RequestMapping(value = "exportWord",method = RequestMethod.GET)
public void loadWord(HttpServletResponse resource) throws IOException {
resource.setContentType("application/msword");
resource.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试word文本", "UTF-8").replaceAll("\\+", "%20");
resource.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".doc");
//创建word文档对象
XWPFDocument document = new XWPFDocument();
//创建标题
XWPFParagraph title = document.createParagraph();
//设置段落居中
title.setAlignment(ParagraphAlignment.CENTER);
//开启标题设置
XWPFRun titleRun = title.createRun();
titleRun.setColor("000000");
titleRun.setFontSize(24);
titleRun.setFontFamily("宋体");
//加粗
titleRun.setBold(true);
titleRun.setText("这是第一行标题");
//添加第二行
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun firstRun = firstParagraph.createRun();
firstRun.setText("第一段落");
firstRun.setColor("000000");
firstRun.setFontSize(20);
firstRun.setFontFamily("宋体");
firstRun.addBreak();//换行
firstRun.setText("第一段落-附加---------");
//设置段落背景颜色
CTShd ctShd = firstRun.getCTR().addNewRPr().addNewShd();
ctShd.setVal(STShd.CLEAR);
ctShd.setFill("97FFFF");
//添加第二行
XWPFParagraph secondParagraph = document.createParagraph();
secondParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun secondRun = secondParagraph.createRun();
secondRun.setText("第二段落");
secondRun.setColor("000000");
secondRun.setFontSize(20);
secondRun.setFontFamily("黑体");
secondRun.addBreak();//换行
document.write(resource.getOutputStream());
document.close();
}
/**
* 生成word文档文字
*
* @param resource
* @throws IOException
*/
@RequestMapping(value = "exportWord1",method = RequestMethod.GET)
public void loadWord1(HttpServletResponse resource) throws IOException {
resource.setContentType("application/msword");
resource.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("模拟word文本", "UTF-8").replaceAll("\\+", "%20");
System.out.println("fileName = " + fileName);
resource.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".doc");
//创建word文档对象
XWPFDocument document = new XWPFDocument();
//创建标题
XWPFParagraph title = document.createParagraph();
//设置段落居中
title.setAlignment(ParagraphAlignment.CENTER);
//开启标题设置
XWPFRun titleRun = title.createRun();
titleRun.setColor("000000");
titleRun.setFontSize(24);
titleRun.setFontFamily("宋体");
//加粗
titleRun.setBold(true);
titleRun.setText("xxx公告声明");
//添加第二行
XWPFParagraph firstParagraph = document.createParagraph();
firstParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun firstRun = firstParagraph.createRun();
firstRun.setColor("000000");
firstRun.setFontSize(13);
firstRun.setFontFamily("黑体");
firstRun.addTab();
firstRun.setText("许宏才表示,今年的留抵退税政策跟以往政策相比,有以下特点," +
"一是提前退付。以前对企业增值税有一些留抵退税,但是采取的方式主要是结转下期抵扣。" +
"今年实施的留抵退税政策改为了购进退税,不再等企业产生销售之后再去抵,就是购进设备时,税务部门就把购进的进项增值税办退税了。");
//换行
firstRun.addBreak();
//tab缩进
firstRun.addTab();
firstRun.setText("二是存量退税。“在2019年4月以后企业新购进的设备或者说投资形成的增值税," +
"我们纳入到留抵退税范围。今年不是,不仅退付增量留抵税额,还要退还以前年度结存的进项增值税," +
"就是若干年以来没有通过销项抵掉的进项增值税全部可以退还。所以,今年跟以前比不仅仅是退还增量,存量也要退。”许宏才说。");
//添加第二行
XWPFParagraph secondParagraph = document.createParagraph();
//左对齐
secondParagraph.setAlignment(ParagraphAlignment.LEFT);
XWPFRun secondRun = secondParagraph.createRun();
secondRun.setText("时间:" + new Date().toString());
// secondRun.setColor("000000");
// secondRun.setFontSize(20);
// secondRun.setFontFamily("黑体");
// secondRun.addBreak();//换行
document.write(resource.getOutputStream());
document.close();
}
/**
* 生成word文档 表格
*
* @param resource
* @throws IOException
*/
@RequestMapping(value = "exportWordTable",method = RequestMethod.GET)
public void loadWordTable(HttpServletResponse resource) throws IOException {
resource.setContentType("application/msword");
resource.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("测试word表格", "UTF-8").replaceAll("\\+", "%20");
resource.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".doc");
//创建word文档对象
XWPFDocument document = new XWPFDocument();
//创建标题
XWPFParagraph title = document.createParagraph();
//设置段落居中
title.setAlignment(ParagraphAlignment.CENTER);
//开启标题设置
XWPFRun titleRun = title.createRun();
titleRun.setColor("000000");
titleRun.setFontSize(24);
titleRun.setFontFamily("宋体");
//加粗
titleRun.setBold(true);
titleRun.setText("XXX报表");
//添加第二行-表格 1行4列
XWPFTable table = document.createTable(1, 4);
CTTbl ttb1 = table.getCTTbl();
CTTblPr tblPr = ttb1.getTblPr() == null ? ttb1.addNewTblPr() : ttb1.getTblPr();
CTTblWidth tblWidth = tblPr.isSetTblW() ? tblPr.getTblW() : tblPr.addNewTblW();
//这个宽度是整个大表格的,不是局部到具体的单独大小,注意
tblWidth.setW(new BigInteger("10000"));
tblWidth.setType(STTblWidth.DXA);
/**
* row 是表格的第x行
* cell是第x列
* 和excel的底层原理是一样的
* */
table.getRow(0).setHeight(500);
setCellText(table.getRow(0).getCell(0),"序号","FFFFFF",10000);
setCellText(table.getRow(0).getCell(1),"类别","FFFFFF",20000);
setCellText(table.getRow(0).getCell(2),"证据名称","FFFFFF",20000);
setCellText(table.getRow(0).getCell(3),"备注","FFFFFF",20000);
//setCellText(table.getRow(0).getCell(4),"添加测试添加测试添加测试添加测试添加测试添加测试","FFFFFF",20000);
/**
*
* */
for (int maxCount = 1; maxCount < 5; maxCount++) {
//这个是在表格的添加,添加到第几行
XWPFTableRow row = table.insertNewTableRow(maxCount);
//设置单元格的高度
row.setHeight(450);
for (int i = 1; i < 5; i++) {
XWPFTableCell cell = row.createCell();
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
cell.setText(null);
}
}
document.write(resource.getOutputStream());
document.close();
}
/**
* 设置表头内容
* @param cell 第几列
* @param text 内容
* @param bgColor 背景颜色
* @param width 单元格的局部大小
*/
private static void setCellText(XWPFTableCell cell,
String text, String bgColor, int width) {
CTTc ctTc = cell.getCTTc();
CTTcPr cellPr = ctTc.addNewTcPr();
cellPr.addNewTcW().setW(BigInteger.valueOf(width));
cell.setColor(bgColor);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
CTTcPr ctPr = ctTc.addNewTcPr();
ctPr.addNewVAlign().setVal(STVerticalJc.CENTER);
cell.setText(text);
}
如果在启动项目中有什么问题,可以重试打包编译一哈。本人在启动时抛出500错误,显示某个类没有找到。重新打包编译
3、结果展示
第二个接口成功展示