当前位置: 首页 > news >正文

常州市建设局网站电话网站管理后台制作

常州市建设局网站电话,网站管理后台制作,织梦网站导入链接怎么做,网店装修论文最近接到一个新的需求#xff0c;用户创建好模板文件保存到模板库#xff0c;然后使用在线文档编辑器打开模板时#xff0c;将系统数据填充到模板文件并生成新的word文件#xff0c;然后在线编辑#xff0c;研究使用Apache POI和POI-OOXML实现了这个功能。 Maven依赖 用户创建好模板文件保存到模板库然后使用在线文档编辑器打开模板时将系统数据填充到模板文件并生成新的word文件然后在线编辑研究使用Apache POI和POI-OOXML实现了这个功能。 Maven依赖 !-- Apache POI 和 POI-OOXML --dependencygroupIdorg.apache.poi/groupIdartifactIdpoi/artifactIdversion5.2.2/version/dependencydependencygroupIdorg.apache.poi/groupIdartifactIdpoi-ooxml/artifactIdversion5.2.2/version/dependency 自动填充的接口 import org.apache.poi.xwpf.usermodel.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map;RestController RequestMapping(/fill) public class WordFillController {PostMapping(/wordFill)public ResponseEntity? wordFill() {String templatePath E:\\word\\template.docx;String outputPath E:\\word\\output.docx;// 待填充进模板的数据MapString, String data new HashMap();data.put(name, 张三);data.put(sex, 男);data.put(age, 18);MapString, String data1 new HashMap();data1.put(name, 张三1);data1.put(sex, 男1);data1.put(age, 20);MapString, String data2 new HashMap();data2.put(name, 李四);data2.put(sex, 男);data2.put(age, 21);MapString, String data3 new HashMap();data3.put(name, 王五);data3.put(sex, 女);data3.put(age, 45);ListMapString, String dataList Arrays.asList(data1, data2, data3);try (// 从模板文件创建word文档对象XWPFDocument doc new XWPFDocument(Files.newInputStream(Paths.get(templatePath)));// 文件输出流FileOutputStream fos new FileOutputStream(outputPath)) {replacePlaceholders(doc, data);// 替换占位符fillTable(doc, dataList);// 填充表格// 将文档写入输出流doc.write(fos);// 刷新输出流fos.flush();return ResponseEntity.ok().build();} catch (IOException e) {e.printStackTrace();return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());}}private void replacePlaceholders(XWPFDocument doc, MapString, String data) {// 遍历文档中的段落替换占位符doc.getParagraphs().forEach(paragraph - {// 遍历段落中的文本替换占位符paragraph.getRuns().forEach(run - {// 获取文本内容String text run.getText(0);// 判断文本是否包含占位符如果包含则替换占位符if (text ! null text.contains(${)) {for (Map.EntryString, String entry : data.entrySet()) {text text.replace(${ entry.getKey() }, entry.getValue());}// 设置替换后的文本run.setText(text, 0);}});});}private void fillTable(XWPFDocument doc, ListMapString, String dataList) {XWPFTable table doc.getTables().get(0); // 获取第一个表格// 获取表格的模板行假设表格的第一行是标题行第二行为模板行XWPFTableRow templateRow table.getRow(1);// 添加新的行并删除模板行for (MapString, String data : dataList) {XWPFTableRow newRow table.createRow();// 手动创建每个单元格并复制模板行的样式for (int i 0; i templateRow.getTableCells().size(); i) {XWPFTableCell templateCell templateRow.getCell(i);XWPFTableCell newCell newRow.getCell(i);if (newCell null) {newCell newRow.createCell();}// 复制模板行单元格的样式不复制内容copyCellStyleWithoutText(templateCell, newCell);// 填充数据switch (i) {case 0:newCell.setText(data.get(name));break;case 1:newCell.setText(data.get(sex));break;case 2:newCell.setText(data.get(age));break;}}}// 删除模板行table.removeRow(1);}private void copyCellStyleWithoutText(XWPFTableCell sourceCell, XWPFTableCell targetCell) {// 复制单元格的样式但不复制文本内容targetCell.getParagraphs().forEach(paragraph - {XWPFParagraph sourceParagraph sourceCell.getParagraphs().get(0);paragraph.setAlignment(sourceParagraph.getAlignment());paragraph.setVerticalAlignment(sourceParagraph.getVerticalAlignment());if (!sourceParagraph.getRuns().isEmpty()) {XWPFRun sourceRun sourceParagraph.getRuns().get(0);XWPFRun targetRun paragraph.createRun();targetRun.setBold(sourceRun.isBold());targetRun.setItalic(sourceRun.isItalic());targetRun.setFontFamily(sourceRun.getFontFamily());targetRun.setFontSize(sourceRun.getFontSize());}});// 复制单元格背景色等样式targetCell.setColor(sourceCell.getColor());targetCell.setVerticalAlignment(sourceCell.getVerticalAlignment());} } word模板 调用接口后生成的新文件
http://www.tj-hxxt.cn/news/137819.html

相关文章:

  • 禁止粘贴的网站wordpress文章内图片不显示不出来
  • 网站的背景图怎么做的seo搜索引擎优化技术
  • 新县住房和城乡规划建设网站专业北京网站建设公司哪家好
  • 快速建设一个网站网页设计图片加载不出
  • 网站建设ppt简介西班牙语网站建设
  • 建设一个网站怎么赚钱南宁建站模板厂家
  • 建设厅网站上的信息采集表附近哪里需要招人
  • 网站建设专业输入法合肥网站制作联系方式
  • 旅游网站首页制作wordpress 密码爆破
  • 网站营销方法有哪些内容wordpress数据库删除
  • 同一域名可以做相同网站吗网站标题 关键词 描述之间的关系
  • 90后小姑娘做网站wordpress外网访问网页异常
  • 免费做app页面的网站linux 网站开发
  • 手工做皮具国外的网站盐城seo排名
  • 连锁网站开发建设网站的目的和功能定位
  • dz做网站js不起作用o2o生鲜电商平台有哪些
  • 做化妆品注册和注册的网站吗易语言做网站图片下载
  • 邢台网站建设邢台淘宝客网站做百度竞价
  • 校园网站建设管理制度网站快速收录平台
  • 一个网站的建设需要哪些流程生活分类信息网站源码
  • 北京好一点的微网站开发公司互联网保险有哪些
  • 哪个企业做网站做跨境网站注意事项
  • 网页怎么优化网站做优化有什么用吗
  • 建设官方网站的好处和坏处wordpress设置网站导航
  • 商城建设网站策划怎么创建自己公司的网站
  • 网站建设朋友圈通江县住房和城乡建设局网站
  • 建站公司 深圳惠州 网站建设app开发
  • 宿迁建设企业网站域名的申请注册
  • 购物类网站建设网站管理规范
  • 做网站怎么租用服务器吗工业设计参考网站