| | |
| | | import com.ots.project.tool.report.JAQ.condition.Table2; |
| | | import com.ots.project.tool.report.JAQ.condition.Table3; |
| | | import com.ots.project.tool.report.JAQ.condition.Table4; |
| | | import com.ots.project.tool.report.SAQ.chart.SAQChart; |
| | | import org.apache.commons.collections.map.HashedMap; |
| | | import org.apache.poi.ooxml.POIXMLDocument; |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.*; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public class WordUtil { |
| | | private static final Logger log = LoggerFactory.getLogger(WordUtil.class); |
| | |
| | | if (run.toString().indexOf("%TTime%") != -1) { |
| | | run.setText(textMap.get("reportGenerationDate").toString(), 0); |
| | | } |
| | | if (run.toString().indexOf("%LIBSAQGS0018%") != -1) { |
| | | run.setText(textMap.get("LIBSAQGS0018").toString(), 0); |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject, |
| | | String deskFileName, int width, int height, |
| | | int leftOffset, int topOffset, boolean behind, int topPosition) { |
| | | long relativeHeight = 251649024l + topPosition; |
| | | long relativeHeight = 251649024L + topPosition; |
| | | String anchorXML = |
| | | "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " |
| | | + "simplePos=\"0\" relativeHeight=\"" + relativeHeight + "\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">" |
| | |
| | | List<XWPFTableCell> cells = row.getTableCells(); |
| | | for (XWPFTableCell cell : cells) { |
| | | |
| | | |
| | | //如果当前表格包含%就代表需要替换 |
| | | if (checkText(cell.getText())) { |
| | | List<XWPFParagraph> paragraphs = cell.getParagraphs(); |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | | |
| | | //获取所有行 |
| | | List<IRunElement> iRunElements = paragraph.getIRuns(); |
| | | List<XWPFRun> runs = new ArrayList<>(); |
| | | //遍历所有行 |
| | | for (IRunElement iRunElement : iRunElements) { |
| | | runs.add((XWPFRun) iRunElement); |
| | | } |
| | | int runPosition = 0; |
| | | //替换所有表格的文字 |
| | | for (XWPFRun run : runs) { |
| | | setTextValue(textMap, document, paragraph, runPosition, run); |
| | | } |
| | |
| | | } |
| | | } |
| | | private static void setTextValue(Map<String, Object> textMap, XWPFDocument document, XWPFParagraph paragraph, int runPosition, XWPFRun run) { |
| | | System.out.println("表格的:" + run.toString()); |
| | | log.info("表格的:{}", run.toString()); |
| | | try { |
| | | run.setText(changeValue(run.toString(), textMap, run, paragraph, document, runPosition), 0); |
| | | } catch (Exception e) { |
| | |
| | | } |
| | | |
| | | public static String changeValue(String runValue, Map<String, Object> textMap, XWPFRun run, XWPFParagraph paragraph, XWPFDocument document, int runPosition) throws IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException { |
| | | |
| | | log.debug("changeText:{}",runValue); |
| | | Set<Map.Entry<String, Object>> textSets = textMap.entrySet(); |
| | | for (Map.Entry<String, Object> textSet : textSets) { |
| | | |
| | |
| | | if (runValue.indexOf("<BoldText>") != -1) { |
| | | return setOtherStyle(runValue, paragraph, runPosition, run); |
| | | } |
| | | break; |
| | | //有些表格可能存在多个字典 需替换多次不能轻易break |
| | | //没有可替换内容直接break |
| | | if(getTextSize(runValue) == 0){ |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | |
| | | String filePath = getTemplateDownLoadPathByUpLoad(fileName); |
| | | |
| | | XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(getTemplateDownLoadPathByUpLoad(fileName))); |
| | | |
| | | |
| | | //设置文本 |
| | | changeText(document, textMap); |
| | | |
| | | |
| | | //设置文本框 |
| | | changeTextBox(document, textMap); |
| | | |
| | | |
| | | //设置图表 |
| | | if (Objects.equals(reportType, ReportTypeEnum.SAQ.getCode())) { |
| | | SAQChart.changeChart(document, textMap); |
| | | } |
| | | |
| | | //插入表格 |
| | | addTableValue(document, autoTableMap); |
| | | |
| | | |
| | | //更改表格的值 |
| | | changeTable(document, textMap); |
| | | |
| | | |
| | | //设置图表控件 |
| | | changChar(textMap, document, reportType, templateType); |
| | | document.write(out); |
| | | } |
| | |
| | | } |
| | | } |
| | | private static String getValByKey(String text, Map<String, Object> textMap) { |
| | | log.debug("changeTextBox:{}",text); |
| | | if (!checkText(text)) { |
| | | return text; |
| | | } |
| | |
| | | document.write(out); |
| | | out.flush(); |
| | | } |
| | | |
| | | /** |
| | | * 设置图标控件 |
| | | * @param textMap |
| | | * @param document |
| | | * @param reportType |
| | | * @param templateType |
| | | */ |
| | | private static void changChar(Map<String, Object> textMap, XWPFDocument document, String reportType, String templateType) { |
| | | if (StringUtils.isEmpty(reportType) || StringUtils.isEmpty(templateType)) { |
| | | return; |
| | |
| | | if (ExamUtil.isListEmpty(charts)) { |
| | | return; |
| | | } |
| | | if (StringUtils.equals(reportType, "MAQ")) { |
| | | |
| | | if (StringUtils.equals(reportType, ReportTypeEnum.MAQ.getCode())) { |
| | | setMAQCompleteChars(textMap, charts); |
| | | } else if (StringUtils.equals(reportType, ReportTypeEnum.MAQV2.getCode())) { |
| | | setMAQ_V2CompleteChars(textMap, charts); |
| | | } else if (StringUtils.equals(reportType, "RuiLin")) { |
| | | } else if (StringUtils.equals(reportType, ReportTypeEnum.RuiLin.getCode())) { |
| | | setRuiLinChar(textMap, charts); |
| | | } else { |
| | | return; |
| | | } |
| | | } |
| | | private static void setMAQ_V2CompleteChars(Map<String, Object> textMap, List<XWPFChart> charts) { |
| | | String p_People32 = textMap.get("P_Task31").toString(); |
| | | String p_Task31 = textMap.get("P_People32").toString(); |
| | | String p_Task31 = textMap.get("P_Task31").toString(); |
| | | String p_People32 = textMap.get("P_People32").toString(); |
| | | String P_INCON25 = textMap.get("P_INCON25").toString(); |
| | | String P_IM24 = textMap.get("P_IM24").toString(); |
| | | String P_SDE23 = textMap.get("P_SDE23").toString(); |
| | |
| | | List<CTBarSer> serList = ctBarChart.getSerList(); |
| | | CTBarSer ctBarSer = serList.get(0); |
| | | CTNumDataSource val = ctBarSer.getVal(); |
| | | val.getNumRef().getNumCache().getPtList().get(0).setV(p_Task31); |
| | | val.getNumRef().getNumCache().getPtList().get(1).setV(p_People32); |
| | | val.getNumRef().getNumCache().getPtList().get(0).setV(p_People32); |
| | | val.getNumRef().getNumCache().getPtList().get(1).setV(p_Task31); |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | |
| | | textMap.put("JAQTableStyle", jaqTableStyleList); |
| | | } |
| | | } |
| | | |
| | | public static int getTextSize(String runValue){ |
| | | Pattern pattern = Pattern.compile("%\\w+%"); |
| | | Matcher matcher = pattern.matcher(runValue); |
| | | List<String> result = new ArrayList<>(); |
| | | while(matcher.find()){ |
| | | result.add(matcher.group()); |
| | | } |
| | | return result.size(); |
| | | } |
| | | } |