From f891000086af5ac150fc120e1f3c781266f1d063 Mon Sep 17 00:00:00 2001 From: wzp <2040239371@qq.com> Date: 星期三, 21 八月 2024 17:13:57 +0800 Subject: [PATCH] 优化LAQ全景图图表,使用python生成 --- src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java | 212 +++++++++++++++++++++ src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java | 191 +++++++++++++++++-- src/main/java/com/ots/common/utils/poi/WordUtil.java | 141 +++++++------ 3 files changed, 457 insertions(+), 87 deletions(-) diff --git a/src/main/java/com/ots/common/utils/poi/WordUtil.java b/src/main/java/com/ots/common/utils/poi/WordUtil.java index 53c83c4..b9238b8 100644 --- a/src/main/java/com/ots/common/utils/poi/WordUtil.java +++ b/src/main/java/com/ots/common/utils/poi/WordUtil.java @@ -23,6 +23,7 @@ import com.ots.project.tool.report.CIAQ.chart.CIAQChart; import com.ots.project.tool.report.JAQ.condition.JAQTeam; import com.ots.project.tool.report.LAQ.chart.LAQAllChart; +import com.ots.project.tool.report.LAQ.chart.LAQAllPng; import com.ots.project.tool.report.LAQ.chart.LAQChart; import com.ots.project.tool.report.MAQTR.chart.MAQTRChart; import com.ots.project.tool.report.PAQ.chart.PAQChart; @@ -1326,69 +1327,69 @@ * @return */ private static List<String> reportNameList = new ArrayList<>(); - public static String makeReportFileForLAQ(String fileName, TReportTemplate tReportTemplate, Map<String, Object> textMap, Map<Integer, Map<Integer, List<String[]>>> autoTableMap, List<String> deleteFileStrList,Integer index,Integer totalNum,Boolean cleanFlag) { - String returnMessage = "瀵煎嚭妯℃澘杞Щ閿欒:"; - InputStream in = null; - FileOutputStream out = null; - String zipFilePath = ""; - String reportName = ""; - - if(cleanFlag) - { - reportNameList = new ArrayList<>(); - } - - try { - - String reportType = tReportTemplate.getReportType(); - reportName = textMap.get("sendEmailFileName") + "_" + reportType + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getTemplateType()).getCode() + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getLangType()).getCode() + ".docx"; - reportName = reportName.replaceAll(" ", "_"); - - reportNameList.add(reportName); - - //妯℃澘鏂囦欢闇�瑕佸姞閿� - out = getDownLoadFileOutputStream(reportName); - - long startTime1 = System.currentTimeMillis(); - setChageWord(fileName, out, textMap, tReportTemplate.getReportType(), tReportTemplate.getDataPath(), autoTableMap); - // 鑾峰彇缁撴潫鏃堕棿 - long endTime1 = System.currentTimeMillis(); - // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛� - long duration1 = endTime1 - startTime1; - log.info("setChageWord 绋嬪簭鎵ц鑰楁椂锛�" + duration1 + " 姣锛岀敓鎴愭姤鍛婏細"+reportName); - - out.flush(); - -// if(ReportTypeEnum.LAQ.getCode().equals(reportType)){ -// // 鑾峰彇寮�濮嬫椂闂� -// long startTime = System.currentTimeMillis(); +// public static String makeReportFileForLAQ(String fileName, TReportTemplate tReportTemplate, Map<String, Object> textMap, Map<Integer, Map<Integer, List<String[]>>> autoTableMap, List<String> deleteFileStrList,Integer index,Integer totalNum,Boolean cleanFlag) { +// String returnMessage = "瀵煎嚭妯℃澘杞Щ閿欒:"; +// InputStream in = null; +// FileOutputStream out = null; +// String zipFilePath = ""; +// String reportName = ""; // -// PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName); -// // 鑾峰彇缁撴潫鏃堕棿 -// long endTime = System.currentTimeMillis(); -// // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛� -// long duration = endTime - startTime; -// log.info("dockerConvertPDF 绋嬪簭鎵ц鑰楁椂锛�" + duration + " 姣锛岀敓鎴愭姤鍛婏細"+reportName); +// if(cleanFlag) +// { +// reportNameList = new ArrayList<>(); +// } +// +// try { +// +// String reportType = tReportTemplate.getReportType(); +// reportName = textMap.get("sendEmailFileName") + "_" + reportType + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getTemplateType()).getCode() + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getLangType()).getCode() + ".docx"; +// reportName = reportName.replaceAll(" ", "_"); +// +// reportNameList.add(reportName); +// +// //妯℃澘鏂囦欢闇�瑕佸姞閿� +// out = getDownLoadFileOutputStream(reportName); +// +// long startTime1 = System.currentTimeMillis(); +// setChageWord(fileName, out, textMap, tReportTemplate.getReportType(), tReportTemplate.getDataPath(), autoTableMap); +// // 鑾峰彇缁撴潫鏃堕棿 +// long endTime1 = System.currentTimeMillis(); +// // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛� +// long duration1 = endTime1 - startTime1; +// log.info("setChageWord 绋嬪簭鎵ц鑰楁椂锛�" + duration1 + " 姣锛岀敓鎴愭姤鍛婏細"+reportName); +// +// out.flush(); +// +//// if(ReportTypeEnum.LAQ.getCode().equals(reportType)){ +//// // 鑾峰彇寮�濮嬫椂闂� +//// long startTime = System.currentTimeMillis(); +//// +//// PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName); +//// // 鑾峰彇缁撴潫鏃堕棿 +//// long endTime = System.currentTimeMillis(); +//// // 璁$畻鑰楁椂锛堝崟浣嶏細姣锛� +//// long duration = endTime - startTime; +//// log.info("dockerConvertPDF 绋嬪簭鎵ц鑰楁椂锛�" + duration + " 姣锛岀敓鎴愭姤鍛婏細"+reportName); +//// } +// if(Objects.equals(index, totalNum)) +// { +// //骞惰杞崲 +// execLAQ2PDF(reportNameList); // } - if(Objects.equals(index, totalNum)) - { - //骞惰杞崲 - execLAQ2PDF(reportNameList); - } - - zipFilePath = getPdfPath(reportName); - - deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName); - } catch (Exception ex) { - returnMessage = returnMessage + ex.getMessage(); - deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName); - log.error("瀵煎嚭妯℃澘杞Щ閿欒锛歿}\n妫�鏌ユ枃浠讹細" + fileName, returnMessage, ex); - } finally { - closeChannel(out); - closeChannel(in); - } - return zipFilePath; - } +// +// zipFilePath = getPdfPath(reportName); +// +// deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName); +// } catch (Exception ex) { +// returnMessage = returnMessage + ex.getMessage(); +// deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName); +// log.error("瀵煎嚭妯℃澘杞Щ閿欒锛歿}\n妫�鏌ユ枃浠讹細" + fileName, returnMessage, ex); +// } finally { +// closeChannel(out); +// closeChannel(in); +// } +// return zipFilePath; +// } private static void execLAQ2PDF(List<String> reportNames) @@ -1553,6 +1554,12 @@ //璁剧疆鍥捐〃鎺т欢 changChart(textMap, document, reportType,dataPath); + + //濡傛灉鏄疞AQ鐨勫叏鏅浘锛岃缃浘琛� + if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())&& textMap.containsKey("allList")){ + LAQAllPng.changeChart(document,textMap); + } + document.write(out); } @@ -1709,16 +1716,16 @@ } else if (StringUtils.equals(reportType, ReportTypeEnum.CIAQ.getCode())) { CIAQChart.changeChart(document, textMap); } else if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())) { - if(!textMap.containsKey("allList")) + if(textMap.containsKey("allList")) { - System.out.println("涓汉鐢ㄦ埛鎶ュ憡"); - LAQChart.changeChart(document, textMap); + log.info("銆愮敓鎴愬叏鏅浘鎶ュ憡銆�"); + //濡傛灉鍖呭惈allList锛屽垯娓叉煋鍏ㄦ櫙鍥惧浘琛� + LAQAllChart.changeChart(document,textMap); } else { - System.out.println("鍏ㄦ櫙鍥炬姤鍛�"); - //濡傛灉鍖呭惈allList锛屽垯娓叉煋鍏ㄦ櫙鍥惧浘琛� - LAQAllChart.changeChart(document,textMap); + log.info("鐢熸垚鐨勬槸涓汉鐢ㄦ埛鎶ュ憡"); + LAQChart.changeChart(document, textMap); } } else if(StringUtils.equals(reportType, ReportTypeEnum.API_Fan.getCode())) { diff --git a/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java index 924d655..abe27f9 100644 --- a/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java +++ b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java @@ -2,6 +2,7 @@ import com.ots.common.utils.StringUtils; import com.ots.common.utils.poi.ExcelUtil; +import com.ots.framework.config.EssConfig; import com.ots.project.tool.report.LAQ.LAQTemplate; import com.ots.project.tool.report.MAQ.base.RowData; import com.ots.project.tool.report.MAQ.base.SeriesData; @@ -9,13 +10,17 @@ import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.ooxml.POIXMLDocumentPart; import org.apache.poi.openxml4j.util.ZipSecureFile; +import org.apache.poi.util.Units; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import org.apache.poi.xwpf.usermodel.XWPFChart; -import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.*; +import org.apache.xmlbeans.XmlException; import org.openxmlformats.schemas.drawingml.x2006.chart.*; +import org.openxmlformats.schemas.drawingml.x2006.chartDrawing.CTDrawing; +import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject; +import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTAnchor; import java.io.*; import java.util.*; @@ -24,6 +29,7 @@ /** * 鍏ㄦ櫙鍥惧浘琛ㄦ覆鏌� + * 锛�2024.8.21浣滃簾锛� */ @Slf4j public class LAQAllChart { @@ -35,7 +41,7 @@ */ public static void main(String[] args) throws Exception { System.out.println("鍚姩娴嬭瘯.."); - String dataPath = "D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\LAQ瀵煎叆娴嬭瘯鏁版嵁.xlsx"; + String dataPath = "D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\53208d066321f94f47deecec7e9c6907.xlsx"; // 鑾峰彇瀵煎叆鏁版嵁 File file = new File(dataPath); InputStream in = new FileInputStream(file); @@ -66,19 +72,111 @@ * @param textMap 鏁版嵁婧� */ public static void changeChart(XWPFDocument document, Map<String, Object> textMap) { -// Map<Integer, List<SeriesData>> seriesDatas = initData(textMap); -// changeChart(document,seriesDatas,textMap); - System.out.println("杩涘叆鍏ㄦ櫙鍥炬覆鏌撴ā寮�"); + log.info("銆愯繘鍏ュ叏鏅浘娓叉煋妯″紡銆�"); try{ List<LAQTemplate> laqTemplateList = (List<LAQTemplate>)textMap.get("allList"); - changeChartNew(document,laqTemplateList); + //changeChartNew(document,laqTemplateList); + updateImage(document); System.out.println("鍏ㄦ櫙鍥炬覆鏌撳畬鎴�"); } catch (Exception ex) { log.error("LAQ鍏ㄦ櫙鍥惧紓甯革細"+ex.getMessage()); } + } + public static void updateImage(XWPFDocument document){ + //鏇挎崲鍥剧墖 + try{ + // 鑾峰彇鎵�鏈夋钀� + List<XWPFParagraph> paragraphs = document.getParagraphs(); + + //鑾峰彇鍥剧墖璺緞 +// String selectedPic = EssConfig.getReportTemplates()+"apifanSelected.png"; +// String defaultPic =EssConfig.getReportTemplates()+"apifanDefalut.png"; + + //娴嬭瘯鐢ㄧ殑 +// String img1 = "D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\111.png"; +// String img2 ="D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\222.png"; +// String img3 ="D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\333.png"; + + // /home/data + String img1 = EssConfig.getReportTemplates()+"111.png"; + String img2 =EssConfig.getReportTemplates()+"222.png"; + String img3 =EssConfig.getReportTemplates()+"333.png"; + + // 閬嶅巻娈佃惤杩涜鏇挎崲鎿嶄綔 + synchronized (paragraphs) { + for (XWPFParagraph paragraph : paragraphs) { + String text = paragraph.getText(); + + log.info("銆愯幏鍙栧埌鏂囨湰銆戯細"+text); + + // 灏嗗浘鐗囨彃鍏ュ埌娈佃惤涓� + int width = Units.toEMU(580); // 鍥剧墖瀹藉害 + int height = Units.toEMU(700); // 鍥剧墖楂樺害 + + //鐢�%%鍙峰湪鍓嶉潰灏辫鏇挎崲浜� + if (text.contains("{{box0}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + log.info("銆愯幏鍙栧埌box0銆戯細"+img1); + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + + //鍒涘缓Random绫诲璞� + Random random = new Random(); + //浜х敓闅忔満鏁� + int number = random.nextInt(999) + 1; + + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + run.addPicture(new FileInputStream(img1), pictureType, "Seal"+number, width, height); + + } + if (text.contains("{{box1}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + + run.addPicture(new FileInputStream(img2), pictureType, "image.png", width, height); + + } + if (text.contains("{{box2}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + + run.addPicture(new FileInputStream(img3), pictureType, "image.png", width, height); + + } + } + } + + + + } + catch (Exception ex) + { + log.info("銆愬紓甯搞��"+ex.getMessage()); + } } @@ -373,11 +471,13 @@ perfomance = 0.0; } + String name = laqTemplate.getName();//鏍囩鍚嶅瓧銆傝�冭檻鍒拌嫳鏂囦細姣旇緝闀匡紝浼氭牴鎹暱搴﹀仛閫傞厤 - //濡傛灉y杞村皬浜�1锛岃璁剧疆鎴�3锛屽線涓婄Щ鍔ㄤ竴涓� + + //濡傛灉y杞村皬浜�1锛岃璁剧疆鎴�3锛屽線涓婄Щ鍔ㄤ竴涓嬨�倅杞磋皟鏁� if(perfomance<=1.2) { perfomance=perfomance+1.5; @@ -385,7 +485,7 @@ //濡傛灉y杞村ぇ浜�97锛岃缃垚97锛屽線涓嬬Щ鍔ㄤ竴鐐癸紝濂界湅 if(perfomance>=99) { - perfomance=99.0; + perfomance=98.0; } if(perfomance>33.0&&perfomance<34.0) @@ -405,12 +505,30 @@ //x杞磋寰�鍙宠竟绉诲姩 if(olas>67.0&olas<=70.0) { - olas=olas+1.5; - } + //鏍规嵁鍚嶅瓧闀垮害杩涜浣嶇Щ + double len = Math.ceil(name.length()/10.0)+1.0; + if(name.length()>15) + { + System.out.println("鍙栨暣鍊硷細"+len); + olas=olas+len; + } + else + { + olas=olas+1.5; + } + + } if(olas>=99.0) { - olas = olas-2.0; + if(name.length()>15) + { + olas = olas-3.0; + } + else { + olas = olas-2.0; + } + } } @@ -419,26 +537,59 @@ //x杞磋寰�鍙宠竟绉诲姩 if(olas>=33.0&olas<=35.0) { - olas= olas+1; + //鏍规嵁鍚嶅瓧闀垮害杩涜浣嶇Щ + double len = Math.ceil(name.length()/10.0)+1.0; + + if(name.length()>15) + { + System.out.println("鍙栨暣鍊硷細"+len); + olas=olas+len; + } + else + { + olas=olas+1.5; + } } if(olas>66.0&olas<=67.0) { - olas = olas -2.0; + if(name.length()>15) + { + olas = olas-3.0; + } + else { + olas = olas-2.0; + } } } if(type==3) { //x杞磋寰�鍙宠竟绉诲姩 - if(olas>0.0 && olas<=3.0) + if(olas>=0.0 && olas<=3.0) { - olas= olas + 1; + double len = Math.ceil(name.length()/10.0)+1.0; + + if(name.length()>15) + { + System.out.println("鍙栨暣鍊硷細"+len); + olas=olas+len; + } + else + { + olas=olas+1.5; + } } if(olas>=32.0&olas<=33.0) { - olas = olas -2.0; + if(name.length()>15) + { + olas = olas-3.0; + } + else { + olas = olas-2.0; + } } } @@ -526,7 +677,7 @@ { if(olas>67) { - seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")"); + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); dataIndex++; } } @@ -535,7 +686,7 @@ { if(olas<=67 && olas>33) { - seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")"); + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); dataIndex++; } } @@ -544,7 +695,7 @@ { if(olas<=33) { - seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"\n("+olasStr+"-"+perfomanceStr+")"); + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); dataIndex++; } diff --git a/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java new file mode 100644 index 0000000..4244ffa --- /dev/null +++ b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java @@ -0,0 +1,212 @@ +package com.ots.project.tool.report.LAQ.chart; + +import com.ots.framework.config.EssConfig; +import com.ots.project.tool.report.LAQ.LAQTemplate; +import lombok.extern.slf4j.Slf4j; +import org.apache.poi.util.Units; +import org.apache.poi.xwpf.usermodel.Document; +import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.UUID; + +/** + * 閲囩敤python鐢熸垚鍥剧墖锛屽啀鎻掑叆 + */ +@Slf4j +public class LAQAllPng { + + public static void changeChart(XWPFDocument document, Map<String, Object> textMap) { + log.info("銆愯繘鍏ュ叏鏅浘娓叉煋妯″紡銆�"); + try{ + List<LAQTemplate> laqTemplateList = (List<LAQTemplate>)textMap.get("allList"); + //鐢熸垚鍥剧墖 + + String laqExcelPath = (String) textMap.get("laqExcelPath"); + log.info("laqExcelPath鏂囦欢璺緞锛�"+laqExcelPath); + + // 鎸囧畾瑕佹鏌ユ垨鍒涘缓鐨勭洰褰曡矾寰� + String baseDirectoryPath = "/home/data/laq/"; + + String bgPath = baseDirectoryPath+"bgPng/"; + String reportPath = baseDirectoryPath+"reportCharts/"+ UUID.randomUUID()+"/"; + + // 鍒涘缓 File 瀵硅薄 + File reportDirectory = new File(reportPath); + // 妫�鏌ョ洰褰曟槸鍚﹀瓨鍦� + if (!reportDirectory.exists()) { + // 鐩綍涓嶅瓨鍦紝鍒涘缓鐩綍 + boolean created = reportDirectory.mkdirs(); // 浣跨敤 mkdirs() 鏂规硶鍙互鍒涘缓澶氱骇鐩綍 + } + + // 鏋勫缓鍛戒护鍜屽弬鏁� + ProcessBuilder processBuilder = new ProcessBuilder( + "/root/miniconda3/envs/generate-charts-env/bin/python", // Python 鎵ц绋嬪簭 + "/root/python-program/generate_charts.py", // Python 鑴氭湰璺緞 + laqExcelPath, bgPath, reportPath // 浼犻�掔粰 Python 鑴氭湰鐨勫弬鏁� + ); + + try { + // 鍚姩杩涚▼ + Process process = processBuilder.start(); + + // 鑾峰彇鏍囧噯杈撳嚭 + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); + String line; + while ((line = reader.readLine()) != null) { + log.info("銆愭墽琛宲y鐢熸垚鍥捐〃缁撴灉銆�"+line); + } + + // 鑾峰彇閿欒杈撳嚭锛堢敤浜庤皟璇曪級 + BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); + while ((line = errorReader.readLine()) != null) { + log.info("銆愭墽琛宲y鐢熸垚鍥捐〃缁撴灉222銆戦敊璇細"+line); + } + + // 绛夊緟杩涚▼瀹屾垚 + int exitCode = process.waitFor(); + log.info("鎵цpython瀹屾垚锛屽浘琛ㄥ凡缁忕敓鎴愬埌璺緞锛�"+reportPath); + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + + updateImage(document,reportPath); + System.out.println("鍏ㄦ櫙鍥炬覆鏌撳畬鎴�"); + + //瑕佸垹闄ょ敓鎴愬浘琛ㄨ矾寰勩�� + deleteDirectory(reportDirectory); + log.info("鐩綍锛�"+reportPath+",鍒犻櫎鎴愬姛"); + + } + catch (Exception ex) + { + log.error("LAQ鍏ㄦ櫙鍥惧紓甯革細"+ex.getMessage()); + } + } + + + /** + * 鍒犻櫎鐩綍鍙婂叾鍐呭 + * @param dir 瑕佸垹闄ょ殑鐩綍 + * @return 鍒犻櫎鏄惁鎴愬姛 + */ + public static boolean deleteDirectory(File dir) { + // 濡傛灉鐩綍涓嶅瓨鍦紝鐩存帴杩斿洖 false + if (!dir.exists()) { + return false; + } + + // 濡傛灉鏄洰褰曪紝鍒欏垹闄ょ洰褰曚腑鐨勬墍鏈夊唴瀹� + if (dir.isDirectory()) { + File[] files = dir.listFiles(); + if (files != null) { + for (File file : files) { + // 閫掑綊鍒犻櫎瀛愭枃浠舵垨瀛愮洰褰� + if (!deleteDirectory(file)) { + return false; // 濡傛灉鍒犻櫎瀛愭枃浠�/瀛愮洰褰曞け璐ワ紝杩斿洖 false + } + } + } + } + + // 鍒犻櫎绌虹洰褰曟垨鏂囦欢 + return dir.delete(); + } + + public static void updateImage(XWPFDocument document,String reportImgPath){ + //鏇挎崲鍥剧墖 + try{ + // 鑾峰彇鎵�鏈夋钀� + List<XWPFParagraph> paragraphs = document.getParagraphs(); + + //娴嬭瘯鐢ㄧ殑 +// String img1 = "D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\111.png"; +// String img2 ="D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\222.png"; +// String img3 ="D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\333.png"; + + // /home/data + String img1 = reportImgPath+"charts_top.png"; + String img2 =reportImgPath+"charts_middle.png"; + String img3 =reportImgPath+"charts_bottom.png"; + + // 閬嶅巻娈佃惤杩涜鏇挎崲鎿嶄綔 + synchronized (paragraphs) { + for (XWPFParagraph paragraph : paragraphs) { + String text = paragraph.getText(); + // 灏嗗浘鐗囨彃鍏ュ埌娈佃惤涓� + int width = Units.toEMU(580); // 鍥剧墖瀹藉害 + int height = Units.toEMU(700); // 鍥剧墖楂樺害 + + //鐢�%%鍙峰湪鍓嶉潰灏辫鏇挎崲浜� + if (text.contains("{{box0}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + + //鍒涘缓Random绫诲璞� + Random random = new Random(); + //浜х敓闅忔満鏁� + int number = random.nextInt(999) + 1; + + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + run.addPicture(new FileInputStream(img1), pictureType, "Seal"+number, width, height); + + } + if (text.contains("{{box1}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + + run.addPicture(new FileInputStream(img2), pictureType, "image.png", width, height); + + } + if (text.contains("{{box2}}")) { // 妫�鏌ユ槸鍚﹀寘鍚鏇挎崲鐨勬枃瀛楀彉閲� + + // 鑾峰彇鎵�鏈夎繍琛岀殑鍒楄〃 + List<XWPFRun> runs = paragraph.getRuns(); + // 绉婚櫎鎵�鏈夎繍琛� + for (int i = runs.size() - 1; i >= 0 ; i--) { + paragraph.removeRun(i); + } + // 鍒涘缓鍥剧墖瀵硅薄 + XWPFRun run = paragraph.createRun(); + int pictureType = Document.PICTURE_TYPE_PNG; // 鍥剧墖绫诲瀷 + + run.addPicture(new FileInputStream(img3), pictureType, "image.png", width, height); + + } + } + } + + + + } + catch (Exception ex) + { + log.info("銆愬紓甯搞��"+ex.getMessage()); + } + } + +} -- Gitblit v1.9.1