栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用poi根据模板生成word以及word转pdf的demo

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用poi根据模板生成word以及word转pdf的demo

注意这里使用的poi版本为3.14,高版本会引起问题,请慎重引用!

调用方式可参考如下

//模板文件地址
String realPath="";
String inputUrl = realPath+"/test0.docx";
//新生产的模板文件
String outputUrl =realPath+"/report"+System.currentTimeMillis()+".docx";
//生成word
//表格参数
List list = new ArrayList();
//先生成word
Map testMap = new HashMap();
testMap.put("name","监测站");
//图片参数
String fileImg="";
Map picture = new HashMap();
picture.put("width", 618);
picture.put("height", 516);
picture.put("type", "png");
picture.put("content", WordUtils.inputStream2ByteArray(new FileInputStream(fileImg), true));
testMap.put("header",picture);
WordUtils.changWord(inputUrl, outputUrl, testMap, list);
//转pdf
File file = new File(outputUrl);
String pdfNameUrl = this.itextDocx2Pdf(realPath, file);
//获取文件流给前端预览下载
File filePdf = new File(pdfNameUrl);


public String itextDocx2Pdf (String localPath, File inputWordFile) {
    String docxPath = localPath + "/" + inputWordFile.getName();
 String pdfName = inputWordFile.getName();
 pdfName = localPath + "/" + pdfName.substring(0, pdfName.indexOf("."))+ ".pdf";
 String imageDir = localPath + "/image/";
 String docxHtml = itextpdf.docx2Html(docxPath, imageDir);
 docxHtml = itextpdf.formatHtml(docxHtml);
 itextpdf.htmlToPdf(docxHtml, pdfName);
 return pdfName;
}

代码中使用的demo在如下资源中CSDN 

需要引入的pom依赖


 org.apache.poi
 poi
 3.14


 org.apache.poi
 poi-ooxml
 3.14


 org.apache.poi
 poi-scratchpad
 3.14


 fr.opensagres.xdocreport
 xdocreport
 1.0.6


 org.apache.poi
 ooxml-schemas
 1.3


 com.itextpdf
 itextpdf
 5.5.11


 com.itextpdf.tool
 xmlworker
 5.5.11


 com.itextpdf
 itext-asian
 5.2.0



 org.jsoup
 jsoup
 1.11.3
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946450.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号