博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Docx4j替换word
阅读量:7094 次
发布时间:2019-06-28

本文共 2406 字,大约阅读时间需要 8 分钟。

@SuppressWarnings("serial")	public static void main(String[] args) throws Exception {				// 模板文件路径        String templatePath = "/Users/lijun/Downloads/work/房产评估/outFile.doc";        // 生成的文件路径        String targetPath = "/Users/lijun/Downloads/work/房产评估/target.docx";        // 书签名        String bmImage = "place_image";        String bmTable = "table_test";        // 图片路径        String imagePath = "/Users/lijun/Documents/workspace1/portal_assess/src/main/webapp/static/images/2.jpg";        // 载入模板文件        WordprocessingMLPackage template = getTemplate(templatePath);//        		WordprocessingMLPackage.load(new FileInputStream(templatePath));        // 提取正文        MainDocumentPart mainDocumentPart = template.getMainDocumentPart();        Document wmlDoc = (Document) mainDocumentPart.getJaxbElement();        Body body = wmlDoc.getBody();                List texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class);        //Map
map = new HashMap
(); searchAndReplace(texts, new HashMap
(){ { this.put("${project.user}", "李俊"); this.put("${project.name}", "蜀山新村2幢202"); this.put("${project.no}", "房子No201703780011");// this.put("${one_here_and}", "Firstname");// this.put("${one}", "ChildA");// this.put("${two}", "ChildB");// this.put("${three}", "ChildC"); } @Override public String get(Object key) { return super.get(key); } }); List
tbl = new ArrayList
(); tbl.add(new String[]{"估价对象及可比实例修正项目","估价对象","可比实例A","可比实例B","可比实例c"}); tbl.add(new String[]{"建筑面积","95","120","86","96"}); tbl.add(new String[]{"楼层","2","1","4","3"}); replaceTable(template,"${table}",tbl); // 提取正文中所有段落 List
paragraphs = body.getContent(); // 提取书签并创建书签的游标 RangeFinder rt = new RangeFinder("CTBookmark", "CTMarkupRange"); new TraversalUtil(paragraphs, rt); for (CTBookmark bm:rt.getStarts()) { //这儿可以对单个书签进行操作,也可以用一个map对所有的书签进行处理 if (bm.getName().equals(bmImage)){ insertImageAtBookmark(template,imagePath,bm); }else if (bm.getName().equals(bmTable)){ // insertTableAtBookmark(template,tbl,bm); } } template.save(new FileOutputStream(targetPath)); }

  

转载于:https://www.cnblogs.com/fg-fd/p/7196204.html

你可能感兴趣的文章
C语言中结构体变量之间赋值
查看>>
javascript精度问题与调整
查看>>
《从零開始学Swift》学习笔记(Day 63)——Cocoa Touch设计模式及应用之单例模式...
查看>>
hdu 3342 Legal or Not (拓扑排序)
查看>>
Dubbo限制大数据传输的解决方案
查看>>
ML学习分享系列(2)_计算广告小窥[中]
查看>>
form怎样正确post文件
查看>>
JVM概述
查看>>
artTemplate子模板include
查看>>
C#模拟POST提交表单(一)--WebClient
查看>>
[Spark][python]从 web log 中提取出 UserID 作为key 值,形成新的 RDD
查看>>
数据结构与算法(周鹏-未出版)-第六章 树-6.5 Huffman 树
查看>>
Zephyr的Shell
查看>>
fpga技能树
查看>>
国内的Android SDK镜像
查看>>
Bootstrap系列 -- 36. 向上弹起的下拉菜单
查看>>
TMS320C6455 SRIO 实现方案
查看>>
Hough transform(霍夫变换)
查看>>
background-color
查看>>
提升单元测试体验的利器--Mockito使用总结
查看>>