From 059ee32aba0bf2f57e0788e8cad4d5cf56a037d6 Mon Sep 17 00:00:00 2001 From: 吴祝攀 <2040239371@qq.com> Date: 星期四, 13 六月 2024 14:46:38 +0800 Subject: [PATCH] JAQ报告优化 --- src/main/java/com/ots/common/utils/poi/WordUtil.java | 283 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 233 insertions(+), 50 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 4363b87..785a6ea 100644 --- a/src/main/java/com/ots/common/utils/poi/WordUtil.java +++ b/src/main/java/com/ots/common/utils/poi/WordUtil.java @@ -16,12 +16,19 @@ import com.ots.project.tool.ShellTool; import com.ots.project.tool.exam.ExamUtil; import com.ots.project.tool.exam.ImageUtil; +import com.ots.project.tool.exam.JsonUtil; import com.ots.project.tool.exam.ZipUtil; +import com.ots.project.tool.libreoffice.LibreOfficeUtil; +import com.ots.project.tool.report.APIFan.chart.APIFanChart; +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.LAQChart; import com.ots.project.tool.report.MAQTR.chart.MAQTRChart; import com.ots.project.tool.report.PAQ.chart.PAQChart; import org.apache.commons.collections.map.HashedMap; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.util.Units; import org.apache.poi.xwpf.usermodel.*; import org.apache.xmlbeans.XmlCursor; @@ -37,8 +44,11 @@ import org.slf4j.LoggerFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import springfox.documentation.spring.web.json.Json; + import java.awt.image.BufferedImage; import java.io.*; +import java.time.LocalDate; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -309,6 +319,7 @@ log.info("琛ㄦ牸鏁版嵁锛�" + table.getText()); //澧炲姞琛ㄦ牸鎴栬�呮笡灏戣〃鏍� if (Objects.equals(textMap.get("sendEmailFileName"), ReportTypeEnum.JAQ.getCode())) { + log.info("琛細{}",i); addOrDelTableRow(table,i,textMap); } if (checkText(table.getText())) { @@ -336,12 +347,15 @@ Integer tableNum = Integer.valueOf(textMap.get(tableEnum.getName()).toString()); Integer rows = table.getRows().size(); + log.info("[娴嬭瘯]-tableEnum锛�"+tableEnum.toString()); + //琛�1-琛�3 闇�鍓旈櫎琛ㄥご Integer contentRows = rows - 1; //琛�4 闇�鍓旈櫎琛ㄥご璺熷熬閮ㄩ鐣欒鏁� if(tableEnum == TableEnum.table4){ - contentRows -= 5; - rows -= 5; + //濡傛灉琛�4锛岃澧炲姞鏄剧ず琛岋紝杩欓噷瑕佹敼鍔� + contentRows -= 3; + rows -= 3; //琛�4 琛屼负琛ㄧ幇涓句緥澶勭悊 Integer table4ChildrenNum = Integer.valueOf(textMap.get(TableEnum.table4Children.getName()).toString()); @@ -383,39 +397,75 @@ } } - //闄愬埗琛屾暟涓嶄负绌� 鍜� 闄愬埗琛屾暟璺熸姤鍛婄幇鏈変笉涓�鑷撮渶杩涜澶勭悊 - if(tableNum != null && !tableNum.equals(contentRows)){ - if(tableNum > contentRows){ - //澧炲姞琛屾暟 - Integer addRow = tableNum - contentRows; - log.info("{}澧炲姞{}琛�",tableEnum.getName(),addRow); - for (int i = rows; i < rows+addRow; i++) { - addJAQTable(table,tableEnum,i,textMap); - } + //table5涓嶉渶瑕佽拷鍔狅紝鍗曠嫭澶勭悊 + if(tableEnum != TableEnum.table5) { + //闄愬埗琛屾暟涓嶄负绌� 鍜� 闄愬埗琛屾暟璺熸姤鍛婄幇鏈変笉涓�鑷撮渶杩涜澶勭悊 + if (tableNum != null && !tableNum.equals(contentRows)) { + if (tableNum > contentRows) { + //澧炲姞琛屾暟 + Integer addRow = tableNum - contentRows; + log.info("{}澧炲姞{}琛�", tableEnum.getName(), addRow); + for (int i = rows; i < rows + addRow; i++) { + addJAQTable(table, tableEnum, i, textMap); + } - }else{ - //鍑忓皯琛屾暟 - Integer delRow = rows - tableNum; - for (int i = contentRows; i > rows-delRow; i--) { - delJAQTable(table,i); + } else { + //鍑忓皯琛屾暟 + Integer delRow = rows - tableNum; + for (int i = contentRows; i > rows - delRow; i--) { + delJAQTable(table, i); + } + log.info("{}鍑忓皯{}琛屾暟", TableEnum.codeOf(index).getName(), delRow - 1); } - log.info("{}鍑忓皯{}琛屾暟",TableEnum.codeOf(index).getName(),delRow-1); } } + //濡傛灉琛�4锛岃澧炲姞鏄剧ず琛岋紝杩欓噷瑕佹敼鍔� //琛�4 鑳滀换鍔涢鐣欎綅缃簭鍙峰鐞� if(tableEnum == TableEnum.table4){ - if(tableNum != null && tableNum != 8){ - for (int i = tableNum; i <= tableNum + 4; i++) { + if(tableNum != null && tableNum != 12){ + for (int i = tableNum; i <= tableNum + 2; i++) { + if(i+2 > table.getRows().size()){ + continue; + } XWPFTableRow row = table.getRows().get(i+2); List<XWPFTableCell> cells = row.getTableCells(); changeText(cells.get(0).getParagraphs().get(0),"#"+(i+1)+"."); } } } + + + //琛�6锛屾樉绀篔AQ鎶ュ憡鍙備笌浜� + if(tableEnum == TableEnum.table5) + { + log.info("銆愯〃5銆戝紑濮嬪鐞�...{}",tableNum); +// if(tableNum != null && !tableNum.equals(contentRows)) { +// if (tableNum > contentRows) { +// //澧炲姞琛屾暟 +// Integer addRow = tableNum - contentRows; +// log.info("{}澧炲姞{}琛�", tableEnum.getName(), addRow); +// for (int i = rows; i < rows + addRow; i++) { +// addJAQTable(table, tableEnum, i, textMap); +// } +// } +// } + if(tableNum>0) + { + for(int i=rows;i<rows+tableNum;i++) + { + addJAQTable(table, tableEnum, i, textMap); + } + + delJAQTable(table,1); + } + log.info("銆愯〃5銆戝鐞嗗畬鎴�..."); + } + }catch (Exception e){ e.printStackTrace(); log.info("琛▄}澧炲姞琛ㄦ牸鎴栬�呮笡灏戣〃鏍煎紓甯�",index+1); + log.info("寮傚父锛歿}",e.getMessage()); } } @@ -467,6 +517,7 @@ XWPFTableRow row = table.getRows().get(index); List<XWPFTableCell> cells = row.getTableCells(); + switch (tableEnum){ case table1: cells.get(0).getParagraphs().get(0).getRuns().get(0).setText(textMap.get("T1rank"+index)+""); @@ -537,6 +588,48 @@ cell.getParagraphs().get(cell.getParagraphs().size()-1).getRuns().get(0).setText(null); } } + break; + case table5: + log.info("[add] table5,澧炲姞琛屻�俰ndex={}",index); + String teamUsers = textMap.get("teamUserList").toString(); + List<JAQTeam> teamUserList = JsonUtil.toJsonListObject(teamUsers,JAQTeam.class); + + int keyIndex = index-2; + if(keyIndex>=0) + { + log.info("褰撳墠keyIndex:{}",keyIndex); + for(int k =0;k<3;k++) + { + XWPFParagraph p =cells.get(k).getParagraphs().get(0); + List<XWPFRun> runs = p.getRuns(); + if(runs.isEmpty()) + { + p.createRun(); + } + for (int i = runs.size()-1;i>0;i--) + { + p.removeRun(i); + } + XWPFRun run = runs.get(0); + + switch (k){ + case 0:run.setText(String.valueOf(keyIndex+1),0); break; + case 1:run.setText(teamUserList.get(keyIndex).getTeamName(),0);break; + case 2:run.setText(teamUserList.get(keyIndex).getTeamDepart(),0);break; + case 3:run.setText(teamUserList.get(keyIndex).getTeamName(),0);break; + default:break; + } + } + } + + + + + +// cells.get(0).getParagraphs().get(0).getRuns().get(0).setText(index.toString()); +// cells.get(1).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamName()); +// cells.get(2).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamDepart()); +// cells.get(3).getParagraphs().get(0).getRuns().get(0).setText(teamUserList.get(index).getTeamName()); break; } } @@ -899,9 +992,8 @@ if (checkText(runValue)) { runValue = runValue.replaceAll("%.*%", ""); - runValue = runValue.replaceAll("N/A NONE", ""); - } + runValue = runValue.replaceAll("N/A NONE", ""); return runValue; } @@ -1119,7 +1211,7 @@ if(StringUtils.contains(keyValue, "%table-line-feed%")){ String[] keyValues = keyValue.split("%table-line-feed%"); runValue = delDynList(keyTemp, Arrays.asList(keyValues), paragraph, document,1); - }else{ + }else if(StringUtils.contains(keyValue, "%line-feed%")){ String[] keyValues = keyValue.split("%line-feed%"); runValue = delDynList(keyTemp, Arrays.asList(keyValues), paragraph, document,0); } @@ -1164,21 +1256,30 @@ reportType = getTypeIfIsSAQ(tReportTemplate, reportType); reportType = getPositionIfJAQ(textMap, reportType); reportName = textMap.get("sendEmailFileName") + "_" + reportType + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getTemplateType()).getCode() + "_" + ReportTypeNameEnum.valueOf(tReportTemplate.getLangType()).getCode() + ".docx"; - reportName = reportName.replaceAll(" ", "_"); - out = getDownLoadFileOutputStream(reportName); - log.info("reportTemplate:" + fileName); - //String templateType = tReportTemplate.getTemplateType(); - setChageWord(fileName, out, textMap, tReportTemplate.getReportType(),tReportTemplate.getDataPath(), autoTableMap); - out.flush(); - changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType()); - //PAQ銆丮AQTR閲囩敤鏂扮殑doc杞琾df - if(ReportTypeEnum.PAQ.getCode().equals(reportType) || ReportTypeEnum.MAQTR.getCode().equals(reportType)){ + //妯℃澘鏂囦欢闇�瑕佸姞閿� + out = getDownLoadFileOutputStream(reportName); + //synchronized (fileName.intern()){ + //String templateType = tReportTemplate.getTemplateType(); + setChageWord(fileName, out, textMap, tReportTemplate.getReportType(),tReportTemplate.getDataPath(), autoTableMap); + out.flush(); + changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType()); + //} + + //PAQ銆丮AQTR閲囩敤銆丆IAQ銆丄PIFan鏂扮殑doc杞琾df + if(ReportTypeEnum.PAQ.getCode().equals(reportType) || ReportTypeEnum.MAQTR.getCode().equals(reportType) + || ReportTypeEnum.CIAQ.getCode().equals(reportType) ||ReportTypeEnum.API_Fan.getCode().equals(reportType)){ PdfUtil.convertPDF(EssConfig.getProfile() + "/" + reportName); + }else if(ReportTypeEnum.LAQ.getCode().equals(reportType)){ + PdfUtil.dockerConvertPDF(EssConfig.getProfile(),EssConfig.getDocx2pdfPath(),reportName); }else{ ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/"); + // ShellTool shellTool = ShellTool.builder(); + // shellTool.execNewLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/" + reportName.replace("docx","pdf")); + //鏂版柟娉曟祴璇� + //LibreOfficeUtil.convertOffice2PDFSyncIsSuccess(new File(EssConfig.getProfile() + "/" + reportName),new File(EssConfig.getProfile() + "/" + reportName.replace("docx","pdf"))); } zipFilePath = getPdfPath(reportName); @@ -1271,14 +1372,32 @@ */ private static void setChageWord(String fileName, OutputStream out, Map<String, Object> textMap, String reportType,String dataPath, Map<Integer, Map<Integer, List<String[]>>> autoTableMap) throws IOException, InvalidFormatException, DocumentException { //String filePath = getTemplateDownLoadPathByUpLoad(fileName); + ZipSecureFile.setMinInflateRatio(0.001); XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(getTemplateDownLoadPathByUpLoad(fileName))); - //璁剧疆鏂囨湰 - changeText(document, textMap); + if (StringUtils.equals(reportType, ReportTypeEnum.JAQ.getCode())) { + //璁剧疆鏂囨湰妗� + changeTextBox(document, textMap); - //璁剧疆鏂囨湰妗� - changeTextBox(document, textMap); + //璁剧疆鏂囨湰 + changeText(document, textMap); + }else { + //璁剧疆鏂囨湰 + changeText(document, textMap); + + //璁剧疆鏂囨湰妗� + changeTextBox(document, textMap); + } + +// //璁剧疆鏂囨湰妗� +// changeTextBox(document, textMap); +// +// //璁剧疆鏂囨湰 +// changeText(document, textMap); + + //淇敼椤佃剼 + changeFootNotes(document,textMap); //鎻掑叆琛ㄦ牸 addTableValue(document, autoTableMap); @@ -1290,6 +1409,49 @@ changChart(textMap, document, reportType,dataPath); document.write(out); } + + /** + * 淇敼椤佃剼锛堜富瑕佹槸鎶婂勾闄愮粰鎹簡锛� + * @param document + * @param textMap + * @throws DocumentException + */ + private static void changeFootNotes(XWPFDocument document,Map<String,Object> textMap) throws DocumentException{ + + LocalDate currentDate = LocalDate.now(); + int year = currentDate.getYear(); + + // 鑾峰彇鎵�鏈夐〉 + List<XWPFFooter> footers = document.getFooterList(); + + for (XWPFFooter footer : footers) { + // 鑾峰彇姣忎釜椤佃剼鐨勬钀� + List<XWPFParagraph> paragraphs = footer.getParagraphs(); + + // 鎵撳嵃姣忎釜娈佃惤鐨勬枃鏈� + for (XWPFParagraph paragraph : paragraphs) { + System.out.println(paragraph.getText()); + + log.info("銆愰〉鑴氥�戯細{}",paragraph.getText()); + if(paragraph.getText().contains("%year%")) + { + // 鑾峰彇娈佃惤涓殑鎵�鏈塺un + List<XWPFRun> runs = paragraph.getRuns(); + + // 鏇挎崲鏂囨湰 + for (XWPFRun run : runs) { + String text = run.getText(0); + if (text != null && text.contains("%year%")) { + text = text.replace("%year%", String.valueOf(year)); + run.setText(text, 0); + } + } + } + } + } + + } + private static void changeTextBox(XWPFDocument document, Map<String, Object> textMap) throws DocumentException { List<XWPFParagraph> paragraphs = document.getParagraphs(); @@ -1325,7 +1487,7 @@ String key = "%" + textSet.getKey() + "%"; if (text.indexOf(key) != -1) { - text = text.replaceAll(key, (String) textMap.get(textSet.getKey())); + text = text.replaceAll(key, String.valueOf(textMap.get(textSet.getKey()))); repText = text; if (checkText(text)) { continue; @@ -1338,9 +1500,9 @@ repText = repText.replaceAll("<BoldText>", ""); repText = repText.replaceAll("</BoldText>", ""); } - if (repText.indexOf("N/A NONE") != -1) { +/* if (repText.indexOf("N/A NONE") != -1) { repText = repText.replaceAll("N/A NONE", ""); - } + }*/ return repText; } public static void changTableColor(String fileName, OutputStream out, List<JAQTableStyle> jaqTableStyleList) throws IOException { @@ -1387,19 +1549,32 @@ if (ExamUtil.isListEmpty(charts)) { return; } - if (StringUtils.equals(reportType, ReportTypeEnum.MAQ.getCode())) { - setMAQCompleteChars(textMap, charts); - } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQV2.getCode()) || StringUtils.equals(reportType, ReportTypeEnum.MAQIAR.getCode())) { - setMAQ_V2CompleteChars(textMap, charts); - } else if (StringUtils.equals(reportType, ReportTypeEnum.RuiLin.getCode())) { - setRuiLinChar(textMap, charts); - } else if (StringUtils.equals(reportType, ReportTypeEnum.PAQ.getCode())) { - PAQChart.changeChart(document, textMap); - } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQTR.getCode())) { - MAQTRChart.changeMAQTRChart(document, dataPath); - } else { - return; + try{ + if (StringUtils.equals(reportType, ReportTypeEnum.MAQ.getCode())) { + setMAQCompleteChars(textMap, charts); + } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQV2.getCode()) || StringUtils.equals(reportType, ReportTypeEnum.MAQIAR.getCode())) { + setMAQ_V2CompleteChars(textMap, charts); + } else if (StringUtils.equals(reportType, ReportTypeEnum.RuiLin.getCode())) { + setRuiLinChar(textMap, charts); + } else if (StringUtils.equals(reportType, ReportTypeEnum.PAQ.getCode())) { + PAQChart.changeChart(document, textMap); + } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQTR.getCode())) { + MAQTRChart.changeMAQTRChart(document, dataPath); + } else if (StringUtils.equals(reportType, ReportTypeEnum.CIAQ.getCode())) { + CIAQChart.changeChart(document, textMap); + } else if (StringUtils.equals(reportType, ReportTypeEnum.LAQ.getCode())) { + LAQChart.changeChart(document, textMap); + } else if(StringUtils.equals(reportType, ReportTypeEnum.API_Fan.getCode())) + { + APIFanChart.changeChart(document,textMap); + } + else { + return; + } + }catch (Exception e){ + e.printStackTrace(); } + } private static void setMAQ_V2CompleteChars(Map<String, Object> textMap, List<XWPFChart> charts) { String p_Task31 = textMap.get("P_Task31").toString(); @@ -1773,7 +1948,15 @@ //鏇存敼琛ㄦ牸鐨勫�� textMap.put("sendEmailFileName", "JAQ"); + textMap.put("SelectUsers",8); + //changeTable(document, textMap); + + + changeText(document, textMap); + changeTextBox(document, textMap); + addTableValue(document, null); changeTable(document, textMap); + //changChart(textMap, document, reportType, dataPath); //寤虹珛鏂囦欢瀵硅薄 File file = new File("C:\\Users\\Administrator\\Desktop\\JAQ涓枃鐗�1.docx"); -- Gitblit v1.9.1