注意这里使用的poi版本为3.14,高版本会引起问题,请慎重引用!
调用方式可参考如下
//模板文件地址 String realPath=""; String inputUrl = realPath+"/test0.docx"; //新生产的模板文件 String outputUrl =realPath+"/report"+System.currentTimeMillis()+".docx"; //生成word //表格参数 Listlist = 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 poi3.14
org.apache.poi poi-ooxml3.14 org.apache.poi poi-scratchpad3.14 fr.opensagres.xdocreport xdocreport1.0.6 org.apache.poi ooxml-schemas1.3 com.itextpdf itextpdf5.5.11 com.itextpdf.tool xmlworker5.5.11 com.itextpdf itext-asian5.2.0 org.jsoup jsoup1.11.3



