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

网页设计这个行业怎么样十大seo免费软件

网页设计这个行业怎么样,十大seo免费软件,网站的总体结构,松北区建设局网站最近接到一个新的需求&#xff0c;用户创建好模板文件保存到模板库&#xff0c;然后使用在线文档编辑器打开模板时&#xff0c;将系统数据填充到模板文件并生成新的word文件&#xff0c;然后在线编辑&#xff0c;研究使用Apache POI和POI-OOXML实现了这个功能。 Maven依赖 <…

 最近接到一个新的需求,用户创建好模板文件保存到模板库,然后使用在线文档编辑器打开模板时,将系统数据填充到模板文件并生成新的word文件,然后在线编辑,研究使用Apache POI和POI-OOXML实现了这个功能。

Maven依赖

        <!-- Apache POI 和 POI-OOXML --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>5.2.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>5.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";// 待填充进模板的数据Map<String, String> data = new HashMap<>();data.put("name", "张三");data.put("sex", "男");data.put("age", "18");Map<String, String> data1 = new HashMap<>();data1.put("name", "张三1");data1.put("sex", "男1");data1.put("age", "20");Map<String, String> data2 = new HashMap<>();data2.put("name", "李四");data2.put("sex", "男");data2.put("age", "21");Map<String, String> data3 = new HashMap<>();data3.put("name", "王五");data3.put("sex", "女");data3.put("age", "45");List<Map<String, 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, Map<String, String> data) {// 遍历文档中的段落,替换占位符doc.getParagraphs().forEach(paragraph -> {// 遍历段落中的文本,替换占位符paragraph.getRuns().forEach(run -> {// 获取文本内容String text = run.getText(0);// 判断文本是否包含占位符,如果包含,则替换占位符if (text != null && text.contains("${")) {for (Map.Entry<String, String> entry : data.entrySet()) {text = text.replace("${" + entry.getKey() + "}", entry.getValue());}// 设置替换后的文本run.setText(text, 0);}});});}private void fillTable(XWPFDocument doc, List<Map<String, String>> dataList) {XWPFTable table = doc.getTables().get(0); // 获取第一个表格// 获取表格的模板行(假设表格的第一行是标题行,第二行为模板行)XWPFTableRow templateRow = table.getRow(1);// 添加新的行,并删除模板行for (Map<String, 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.hkea.cn/news/920051/

相关文章:

  • 深圳教育平台网站建设好消息疫情要结束了
  • 国外设计文章的网站淘宝代运营靠谱吗
  • 市桥网站建设sem论坛
  • 猎头公司是做什么的可靠吗排名优化外包公司
  • 扶贫网站建设关键词查询神器
  • 沈阳酒店企业网站制作公司2023年9月疫情又开始了吗
  • 厦门专业网站建设如何快速推广一个新产品
  • 帮人做传销网站违法吗seo网站排名助手
  • 如何做优品快报下的子网站营销型网站建设目标
  • 用织梦做网站调用乱码营业推广是什么意思
  • 做走私网站北京口碑最好的it培训机构
  • 网站建设OA系统开发it培训机构哪家好
  • 网站运维可以做哪些域名查询网站入口
  • 网站开发的基本语言外贸平台自建站
  • 女生自己做网站营销方法有哪些
  • 怎么自己做网站吓别人金融网站推广圳seo公司
  • 彩票网站的客服有做吗海淀seo搜索优化多少钱
  • 河源哪有做网站网页模板设计
  • 手机网站可以做英文版本吗近三天时政热点
  • 怎么做网站游戏网络优化排名培训
  • ic外贸网站建设黑帽seo技巧
  • 实业有限公司网站怎么做百度一下了你就知道官网
  • 企业电子商务网站推广平台有哪些渠道
  • 本地用织梦做网站百度的网站网址
  • 基础展示营销型型网站新闻发稿平台有哪些
  • 做游戏赚钱的网站最新新闻热点事件2022
  • 商务网站建设哪家好推广代理公司
  • 自己做网站是否要买云主机西安百度提升优化
  • 成都注册公司哪个区好分析网站推广和优化的原因
  • 模板建站杭州seo泽成