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

软考证书有用吗张雪峰网站快速排名优化

软考证书有用吗张雪峰,网站快速排名优化,工程建设项目网站,php动态网站开发网易云课堂突然想起来有个小作业&#xff1a;需要根据提供的Word模板填充数据。这里使用POI写了一个小demo验证下。 测试用模板&#xff1a; 执行结果 1.引入依赖坐标 <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId&…

突然想起来有个小作业:需要根据提供的Word模板填充数据。这里使用POI写了一个小demo验证下。

测试用模板:
在这里插入图片描述

执行结果
在这里插入图片描述

1.引入依赖坐标

        <dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version></dependency>

2.获取相关数据的方法

    private static Object getValueFromData(String key) {// 根据需要实现此方法以从数据源获取值// 例如,从数据库、配置文件或用户输入中获取值Map<String, Object> textMap = new HashMap<String, Object>();textMap.put("title","《模板》");textMap.put("myName","--尉某人--");textMap.put("today","2023-11-02");List<Map> tables = new ArrayList<Map>();Map<String, String> tableMap = new HashMap<String, String>();tableMap.put("name","尉某人");tableMap.put("age","26");tableMap.put("phone","189*******");tables.add(tableMap);tableMap = new HashMap<String, String>();tableMap.put("name","王某人");tableMap.put("age","28");tableMap.put("phone","186*******");tables.add(tableMap);tableMap = new HashMap<String, String>();tableMap.put("name","张某人");tableMap.put("age","24");tableMap.put("phone","130*******");tables.add(tableMap);textMap.put("tables",tables);return textMap.get(key);}

3.解析模板并填充数据

    public static void readTemplate() throws Exception {// 读取Word模板文件FileInputStream fis = new FileInputStream("C:\\Users\\Desktop\\template.docx");XWPFDocument document = new XWPFDocument(fis);// 获取所有段落List<XWPFParagraph> paragraphs = document.getParagraphs();for (XWPFParagraph paragraph : paragraphs) {List<XWPFRun> runs = paragraph.getRuns();for (XWPFRun run : runs) {String text = run.getText(0);if (text != null && text.startsWith("${")) {String key = text.substring(2, text.length() - 1);String value = getValueFromData(key).toString(); run.setText(value, 0);}}}// 获取表格List<XWPFTable> rowTables = document.getTables();for (XWPFTable table : rowTables) {// 遍历表格的每一行for (XWPFTableRow row : table.getRows()) {row.getCell(0).setText("尉某人");row.getCell(1).setText("26");row.getCell(2).setText("おひさしぶりだな");}}// 创建表格并填充数据List<Map> tables = (List<Map>) getValueFromData("tables");int size = tables.size();XWPFTable table = document.createTable(size+1, 3);table.getRow(0).getCell(0).setText("姓名");table.getRow(0).getCell(1).setText("年龄");table.getRow(0).getCell(2).setText("联系方式");for (int i = 0; i < tables.size(); i++) {Map<String,String> map = tables.get(i);XWPFTableRow row = table.getRow(i + 1);row.getCell(0).setText(map.get("name"));row.getCell(1).setText(map.get("age"));row.getCell(2).setText(map.get("phone"));}// 保存文件FileOutputStream fos = new FileOutputStream("C:\\Users\\Desktop\\output.docx");document.write(fos);fos.close();document.close();fis.close();}

4.测试结果

    public static void main(String[] args){try {readTemplate();System.out.println("执行成功!!!");} catch (Exception e) {e.printStackTrace();}}
http://www.rdtb.cn/news/21614.html

相关文章:

  • 2023中国互联网公司排行榜seo是什么单位
  • 博客和网站有什么不同今日新闻联播主要内容摘抄
  • 出名的网站有哪些舆情网站直接打开
  • 做动态网站需要什么软件seo系统源码
  • 能被百度收录的建站网站百度广告联盟一个月能赚多少
  • 做网站找那个公司2023搜索最多的关键词
  • 手机怎样建立网站站长网站seo查询
  • 响应式网站和普通网站网页优化建议
  • 广州哪个区最繁华重庆seo网站管理
  • 唐山做网站多少钱疫情最新数据消息地图
  • 衡水网站建设浩森宇特南昌seo数据监控
  • 网站开发评分标准淘宝流量助手平台
  • 网站建设服务ysaigo企业网站快速排名
  • 网站开发用了什么平台网站制作费用一览表
  • 三维家是在网站上做还是在app上网络营销模式下品牌推广研究
  • 淘宝做网站费用深圳货拉拉
  • 服务器里怎么建设网站关键词投放
  • 泰州网站建设与网页制作营销策略有哪些有效手段
  • adobe做网站的是哪个软件世界杯32强排名
  • 手机网站导航菜单营销型网站的公司
  • 徐州玉龙湾旅游开发公司seo优化公司
  • 曲阜做网站哪家好b2b网站平台有哪些
  • 好用的土木建筑网站推广软件赚钱的app
  • 黄骅做网站的电话seo整站优化费用
  • 怎么做网页设计稿seo网站优化经理
  • 网站开发的工具某网站seo诊断分析和优化方案
  • 云主机怎么做网站十大网络营销经典案例
  • 网站网页设计百度文库官网
  • 游戏网站建设毕业论文推广普通话活动方案
  • 如何在app上做网站竞价托管哪家便宜