莞城注册能源公司网上注册流程,seo网站架构,如何通过网站做调查问卷,东莞营销型网站建设费用Java的XWPFTemplate工具类导出word.docx的使用_xwpftemplate 语法_youmdt的博客-CSDN博客 如果是表格的列表参考上面这篇文章即可#xff0c;比较复杂的列表遍历暂时还没找到方法#xff0c;只能手动创建表格了 上面是模板#xff0c;非常简单#xff0c;以为我们是要自己创… Java的XWPFTemplate工具类导出word.docx的使用_xwpftemplate 语法_youmdt的博客-CSDN博客 如果是表格的列表参考上面这篇文章即可比较复杂的列表遍历暂时还没找到方法只能手动创建表格了 上面是模板非常简单以为我们是要自己创建表格
先看结果吧 自己写的表格基本就是这样后面加上换行和宽度高度调节即可。
下面的代码是最基本的所以也就没加高度换行等调节。有时候再补充上去。
下面的代码比较凌乱还没开发完成想整合成一个通用的接口现在只做了日志的把没有用的内容删掉即可。凑合着看。 /*** 安全日志通用导出word*/Overridepublic void exportWord(AqscSecurityLog aqscSecurityLog, HttpServletResponse response) throws IOException {String filePath ;String filename ;String params1 aqscSecurityLog.getParams1();if (Objects.equals(params1, 1)) { //日报filePath /word/day.docx;filename 安全日志.docx;} else if (Objects.equals(params1, 2)) { //周报filePath /word/day.docx;filename 安全周报.docx;} else if (Objects.equals(params1, 3)) { //月报filePath /word/day.docx;filename 安全月报.docx;} else if (Objects.equals(params1, 4)) { //季报filePath /word/day.docx;filename 安全季报.docx;} else {throw new ServiceException(params1不等于1234请联系管理员!);}InputStream inputStream getServiceFile(filePath);XWPFTemplate document XWPFTemplate.compile(inputStream);MapString, Object data new HashMap();ListString ids aqscSecurityLog.getIds();ListAqscSecurityLog logs aqscSecurityLogMapper.selectAqscSecurityLogsByids(ids);//日志if (Objects.equals(params1, 1)) {processingDayData(logs, data, document);}document.render(data);exportWordAfter(document, response, filename);}//处理日志数据private void processingDayData(ListAqscSecurityLog logs, MapString, Object data, XWPFTemplate document) {data.put(companyName, logs.get(0).getCompanyName());data.put(name, logs.get(0).getFillePerson());XWPFTable table2 document.getXWPFDocument().createTable(1, 2);table2.getRow(0).getCell(0).setText(日 期);table2.getRow(0).getCell(1).setText(工作内容);table2.getRow(0).getCell(0).setWidth(30%);table2.getRow(0).getCell(1).setWidth(68%);logs.forEach(it - {XWPFTable table document.getXWPFDocument().createTable(3, 2);// 合并左边的三行for (int i 0; i 3; i) {XWPFTableCell cell table.getRow(i).getCell(0);if (i 0) {cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);} else {cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);}}// 向表格中插入数据if (it.getWeather() ! null) { //日期 星期 天气table.getRow(0).getCell(0).setText(it.getStringTime() ( it.getWeek() ) it.getWeather());} else {table.getRow(0).getCell(0).setText(it.getStringTime() ( it.getWeek() ));}table.getRow(0).getCell(1).setText(it.getContent());table.getRow(1).getCell(1).setText(it.getExperienceAndExperience());table.getRow(2).getCell(1).setText(it.getOtherRecords());table.getRow(0).getCell(0).setWidth(30%);table.getRow(0).getCell(1).setWidth(68%);});}public void exportWordAfter(XWPFTemplate workbook, HttpServletResponse response, String filename) {filename URLEncodeUtil.encode(filename);response.setContentType(application/vnd.openxmlformats-officedocument.wordprocessingml.documentz);//test.xls是弹出下载对话框的文件名不能为中文中文请自行编码response.setHeader(Content-Disposition, attachment;filename filename);response.setHeader(filename, filename);Assert.isTrue(!ObjectUtils.isEmpty(workbook), 导出遇到了问题请联系管理员);try (ServletOutputStream out response.getOutputStream()) {workbook.write(out);workbook.close();} catch (Exception e) {throw new RuntimeException(e);}}
如果是返回到浏览器前端也得配置一下可以参考下面这篇文章配置 java-excel、word、zip返回前端-CSDN博客