1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > 使用poi根据模版生成word文档并转换成PDF文件(可处理doc文件与docx文件版)

使用poi根据模版生成word文档并转换成PDF文件(可处理doc文件与docx文件版)

时间:2021-03-04 14:37:40

相关推荐

使用poi根据模版生成word文档并转换成PDF文件(可处理doc文件与docx文件版)

该篇文章是《使用poi根据模版生成word文档并转换成PDF文件》后续解决传入文件为doc文档或docx的处理方法

/*** 根据模板生成word** @param path模板的路径* @param params 需要替换的参数* @param tableList 需要插入的参数* @param fileName 生成word文件的文件名* @param response*/public void getWord(String path, Map<String, Object> params, List<String[]> tableList, String fileName,HttpServletResponse response) throws Exception {//模板文件路径String modelFilePath = PathConfig.temporaryFilePath+PathConfig.imgPath;//临时文件路径String temporaryFilePath = PathConfig.temporaryFilePath+PathConfig.temporaryPath;String filePath = modelFilePath+path;File file = new File(filePath);InputStream is = new FileInputStream(file);try{//解析docx,如果异常就用doc解析CustomXWPFDocument docx = new CustomXWPFDocument(is);docx(docx,params,tableList,fileName,temporaryFilePath,response);}catch (Exception e){//解析docis = new FileInputStream(file);HWPFDocument doc = new HWPFDocument(is);doc(doc,params,tableList,fileName,temporaryFilePath,response);}this.close(is);}/*** 解析doc类型文件* @param params* @param tableList* @param fileName* @param temporaryFilePath* @param response* @throws Exception*/public void doc(HWPFDocument doc, Map<String, Object> params, List<String[]> tableList, String fileName, String temporaryFilePath,HttpServletResponse response) throws Exception{Range range = doc.getRange();for (Map.Entry<String, Object> entry : params.entrySet()) {range.replaceText(entry.getKey(), entry.getValue()+"");}OutputStream os = null;fileName = .URLDecoder.decode(fileName, "UTF-8");os = new FileOutputStream(temporaryFilePath + fileName);doc.write(os);try {response.setContentType("application/xls");response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));InputStream input = new FileInputStream(temporaryFilePath + fileName);byte buffBytes[] = new byte[1024];OutputStream as = response.getOutputStream();int read = 0;while ((read = input.read(buffBytes)) != -1) {as.write(buffBytes, 0, read);}as.flush();as.close();input.close();} catch (IOException ex) {ex.printStackTrace();}this.close(os);//删除临时文件File f = new File(temporaryFilePath + fileName);f.delete();}/*** 解析docx类型文件* @param params* @param tableList* @param fileName* @param temporaryFilePath* @param response* @throws Exception*/public void docx(CustomXWPFDocument docx, Map<String, Object> params, List<String[]> tableList, String fileName, String temporaryFilePath,HttpServletResponse response) throws Exception{this.replaceInPara(docx, params); // 替换文本里面的变量this.replaceInTable(docx, params, tableList); // 替换表格里面的变量OutputStream os = null;fileName = .URLDecoder.decode(fileName, "UTF-8");os = new FileOutputStream(temporaryFilePath + fileName);docx.write(os);try {response.setContentType("application/xls");response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes("UTF-8"), "iso-8859-1"));InputStream input = new FileInputStream(temporaryFilePath + fileName);byte buffBytes[] = new byte[1024];OutputStream as = response.getOutputStream();int read = 0;while ((read = input.read(buffBytes)) != -1) {as.write(buffBytes, 0, read);}as.flush();as.close();input.close();} catch (IOException ex) {ex.printStackTrace();}this.close(os);//删除临时文件File f = new File(temporaryFilePath + fileName);f.delete();}

参考链接:/mr-wuxiansheng/p/6131494.html

主要参考方法:

//获取模板文件的目录地址String fileDir = new File(base.getFile(), "../../../../../../doc/").getCanonicalPath();//获取模板文件File demoFile=new File(fileDir + "/1.doc");FileInputStream in = new FileInputStream(demoFile);//doc文件转换HWPFDocument hdt = new HWPFDocument(in);//替换读取到的word模板内容的指定字段Range range = hdt.getRange();Map<String, String> map = new HashMap<String, String>();map.put("$PMBD$", goodsName);map.put("$PMKSSJ$", dateFormater.format(startTime));map.put("$MSRHP$", brandNo);map.put("$PMCJJ$", numberToHanZiUtility.number2CNMontrayUnit(dealPrice));map.put("$PMYJ$", numberToHanZiUtility.number2CNMontrayUnit(clientCommison));map.put("$HJ$", numberToHanZiUtility.number2CNMontrayUnit(totalPrice));map.put("$PMCJJXX$", dealPrice + "");map.put("$PMYJXX$", clientCommison + "");map.put("$HJXX$", totalPrice + "");map.put("$PMJSSJ$", dateFormater.format(endTime));//替换内容for (Map.Entry<String,String> entry:map.entrySet()) {range.replaceText(entry.getKey(),entry.getValue());}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。