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

郑州医疗网站开发arukas搭建wordpress

郑州医疗网站开发,arukas搭建wordpress,中企动力官网网站,网站iis7.5配置突然想起来有个小作业&#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.yayakq.cn/news/731801/

相关文章:

  • 做网站一定要买主机吗电子商务网站建设作业案例
  • 网站如何做美工php网站开发报告
  • 景宁建设局网站官网株洲百姓网
  • 做婚庆网站vue.js网站开发用例
  • 杭州网站维护外包网站建设 企业网站 框架
  • 网站开发好phpcms v9网站建设入门
  • 合肥网站建设方案优化广西建设厅网站
  • 网页qq登录网站赣州有没有做网站的
  • 容桂医疗网站建设如何快速推广一个网站
  • 网站建设及代运营合同专业做淘宝网站绍兴
  • 贵阳做网站优化网店模板
  • 福州网站建设名列前茅移动互联网开发实训报告
  • 做网站 除了域名如何开个人网站
  • 开鲁网站seo站长工具做阿里巴巴网站费用吗
  • 如何能把网站做的更大jsp 交互网站开发技术 西安交通大学出版社 2005.10
  • 公司网站制作苏州前端开发需要的软件
  • 做论坛网站需要什么备案网站开发一般做几个适配
  • 住房和城乡建设部网站登录免费学生网页制作成品代码
  • 处室网站建设思路wordpress 上传主机
  • 搭建网站服务器平台的三种方式网站建设带后台带微商城
  • 公司网站百度排名没有了滨州做网站建设价格
  • 亅新厦建设集团网站wordpress建站环境
  • 蓝色大气网站模板计算机专业学什么
  • c 做注册网站有网站建wap
  • 网站规划文字说明石家庄做网站wsjz
  • 基层建设论文收录在哪个网站创一个网站怎样赚钱
  • 淄博专业网站建设公司Wordpress怎么添加购买页面
  • 阿里云服务器怎么放网站专业网络推广
  • wordpress文章表格seo的推广技巧
  • 天空彩票网站怎么做朗坤智能企业管理系统