From 4ce181bd784aeb873a4751826c227161d35de2b5 Mon Sep 17 00:00:00 2001 From: wzp <2040239371@qq.com> Date: 星期二, 17 六月 2025 11:29:16 +0800 Subject: [PATCH] fix:修复报告文件名里有tab键导致的bug --- src/main/java/com/ots/project/tool/report/LAQ/chart/LAQTest.java | 864 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 files changed, 734 insertions(+), 130 deletions(-) diff --git a/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQTest.java b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQTest.java index 2ec6ea3..053dd0a 100644 --- a/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQTest.java +++ b/src/main/java/com/ots/project/tool/report/LAQ/chart/LAQTest.java @@ -1,16 +1,44 @@ package com.ots.project.tool.report.LAQ.chart; import com.alibaba.fastjson.JSON; +import com.aspose.words.Chart; +import com.aspose.words.Document; +import com.aspose.words.DocumentBuilder; +import com.aspose.words.Paragraph; import com.microsoft.schemas.vml.CTTextbox; +import com.ots.common.utils.StringUtils; import com.ots.common.utils.poi.ExcelUtil; import com.ots.common.utils.poi.WordUtil; import com.ots.project.tool.report.LAQ.LAQReport; 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; +import javafx.scene.chart.ScatterChart; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.ooxml.POIXMLDocument; +import org.apache.poi.ooxml.POIXMLDocumentPart; +import org.apache.poi.openxml4j.util.ZipSecureFile; +import org.apache.poi.sl.usermodel.PictureData; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.Units; +import org.apache.poi.xddf.usermodel.chart.AxisPosition; +import org.apache.poi.xddf.usermodel.chart.XDDFCategoryAxis; +import org.apache.poi.xddf.usermodel.chart.XDDFChartData; +import org.apache.poi.xddf.usermodel.chart.XDDFValueAxis; +import org.apache.poi.xssf.usermodel.*; +import org.apache.poi.xwpf.usermodel.XWPFChart; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; +import org.apache.xmlbeans.XmlObject; +import org.openxmlformats.schemas.drawingml.x2006.chart.*; +import org.openxmlformats.schemas.officeDocument.x2006.math.CTText; +import org.openxmlformats.schemas.officeDocument.x2006.math.CTUnSignedInteger; +import org.openxmlformats.schemas.officeDocument.x2006.math.impl.CTUnSignedIntegerImpl; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTxbxContent; import java.awt.*; import java.io.*; @@ -21,151 +49,727 @@ import java.io.FileOutputStream; import java.io.IOException; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; @Slf4j public class LAQTest { + + //杩欓噷鏄敤鏉ユ祴璇曠殑 public static void main(String[] args) throws Exception { System.out.println("鍚姩娴嬭瘯.."); +// Map<String, Object> textMap = new HashMap<String, Object>(); +// textMap.put("%boxgrid1%","鏄撶传杞┿�侀偟璇楁偊"); +// textMap.put("%boxgrid2%","鍙惰瘲鎮�"); +// textMap.put("%boxgrid3%","鍒樻櫒娑�"); +// textMap.put("%boxgrid4%","娈靛浗璐�"); +// textMap.put("%boxgrid5%","璐虹憺杈�"); +// textMap.put("%boxgrid6%","璧电繑"); +// textMap.put("%boxgrid7%","閽变匠姣�"); +// textMap.put("%boxgrid8%","鍗㈡枃杞�"); +// textMap.put("%boxgrid9%","鍞愬繝鏋�"); + String dataPath = "D:\\妗岄潰鏂囦欢\\LAQ鍏ㄦ櫙鍥炬姤鍛奬\LAQ瀵煎叆娴嬭瘯鏁版嵁.xlsx"; + // 鑾峰彇瀵煎叆鏁版嵁 + File file = new File(dataPath); + InputStream in = new FileInputStream(file); + ExcelUtil<LAQTemplate> util = new ExcelUtil<>(LAQTemplate.class); + List<LAQTemplate> laqTemplateList = util.importExcel(in); + // 鐢熸垚鏂囦欢闆嗗悎 + List<String> fileNameList = new ArrayList<>(); + List<String> deleteStrList = new ArrayList<>(); + +// for (int i = 0; i < laqTemplateList.size(); i++) { +// LAQTemplate laqTemplate = laqTemplateList.get(i); +// //缁勮鎶ュ憡鎵�鐢ㄦ暟鎹� +// Map<String, Object> textMap = new HashMap<>(); +// textMap.put("olas", "85"); +// textMap.put("ca", "32"); +// textMap.put("bla", "34"); +// textMap.put("nr", "3"); +// textMap.put("ar", "45"); +// textMap.put("open", "67"); +// textMap.put("cons", "12"); +// textMap.put("extra", "48"); +// textMap.put("agree", "90"); +// textMap.put("emosta", "23"); +// textMap.put("perfomance", "60"); // -// try { -// FileInputStream fis = new FileInputStream("D:\\妗岄潰鏂囦欢\\TaiTest.docx"); -// XWPFDocument document = new XWPFDocument(fis); // -// List<XWPFParagraph> paragraphs = document.getParagraphs(); -// for (XWPFParagraph paragraph : paragraphs) { -// for (XWPFRun run : paragraph.getRuns()) { -// for (XWPFPicture picture : run.getEmbeddedPictures()) { -// if (picture.getCTPicture() != null -// && picture.getCTPicture().getSpPr() != null -// && picture.getCTPicture().getSpPr().getXfrm() != null -// && picture.getCTPicture().getSpPr().getXfrm().getExt() != null) { +// LAQReport report = new LAQReport(); // -// CTPositiveSize2D ext = picture.getCTPicture().getSpPr().getXfrm().getExt(); -// if (ext != null) { -// long widthEmus = ext.getCx(); -// long heightEmus = ext.getCy(); -// -// int width = (int) (widthEmus / 9525); // Convert EMUs to points (assuming 1 inch = 9525 EMUs) -// int height = (int) (heightEmus / 9525); // Convert EMUs to points (assuming 1 inch = 9525 EMUs) -// -// System.out.println("Image Width: " + width + " points"); -// System.out.println("Image Height: " + height + " points"); -// } -// } else { -// System.err.println("Error: Unable to retrieve image width and height."); -// } -// } -// } +// ZipSecureFile.setMinInflateRatio(0.001); +// try (OutputStream os = new FileOutputStream("D:\\妗岄潰鏂囦欢\\LAQ鑻辨枃鎶ュ憡"+UUID.randomUUID().toString()+".docx"); +// XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\妗岄潰鏂囦欢\\LAQ鑻辨枃鎶ュ憡-鍙犲姞鐗堟湰.docx"))) { +// Map<Integer, List<SeriesData>> seriesDatas = initData(textMap); +// Map<Integer,String> lableDatas = initLables(); +// changeChart(document, seriesDatas, textMap,lableDatas); +// document.write(os); +// } catch (Exception e) { +// e.printStackTrace(); // } -// -// -// // 鍒涘缓涓�涓柊鐨勬钀� -//// XWPFParagraph paragraph = document.createParagraph(); -//// -//// // 璁剧疆娈佃惤鐨勫乏杈硅窛鍜屼笂杈硅窛 -////// paragraph.setIndentationLeft(0); // 0鏈�灏忋��7500 -////// paragraph.setSpacingBefore(0); // 璁剧疆涓婅竟璺濅负0 -//// paragraph.setIndentationLeft(7200);//鏈�澶�8000 -//// paragraph.setSpacingBefore(7000);//鏈�澶�7200 -//// -//// // 娣诲姞鏂囨湰鍒版钀� -//// XWPFRun run = paragraph.createRun(); -//// run.setText("鍚寸鏀�"); -//// -//// -//// XWPFParagraph paragraph1 = document.createParagraph(); -//// paragraph1.setIndentationLeft(5000);//鏈�澶�8000 -//// paragraph1.setSpacingBefore(5000);//鏈�澶�7200 -//// XWPFRun run1= paragraph1.createRun(); -//// run1.setText("鍚磋鏅�"); -// -// -// // 鍒涘缓涓�涓舰鐘朵綔涓烘枃鏈 -// Shape textBox = new Shape(document, ShapeType.TEXT_BOX); -// -// // 璁剧疆鏂囨湰妗嗙殑鍐呭 -// textBox.getTextPath().setText("鏂囨湰妗嗕腑鐨勬枃鏈�"); -// -// // 璁剧疆鏂囨湰妗嗙殑浣嶇疆鍜屽ぇ灏� -// textBox.setWidth(200); // 璁剧疆瀹藉害 -// textBox.setHeight(100); // 璁剧疆楂樺害 -// -// // 璁剧疆鏂囨湰妗嗙殑鑳屾櫙閫忔槑 -// textBox.setFilled(false); // 璁剧疆濉厖鑹蹭负閫忔槑 -// textBox.setStrokeColor(Color.WHITE); // 璁剧疆杈规棰滆壊涓洪�忔槑 -// -// // 灏嗘枃鏈娣诲姞鍒版枃妗d腑 -// doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox); -// -// -// // 灏嗘枃妗e啓鍏ュ埌鏂囦欢 -// FileOutputStream fos = new FileOutputStream("D:\\妗岄潰鏂囦欢\\TaiTest_out_"+ UUID.randomUUID().toString() +".docx"); -// document.write(fos); -// -// // 鍏抽棴鏂囦欢娴� -// fis.close(); -// fos.close(); -// -// } catch (IOException e) { -// e.printStackTrace(); // } -// -// FileInputStream fis = new FileInputStream("D:\\妗岄潰鏂囦欢\\TaiTest.docx"); -// Document doc = new Document(fis); -// -// // 鍒涘缓鏂囨湰妗� -// Shape textBox = new Shape(doc, ShapeType.TEXT_BOX); -// textBox.setWidth(200); // 璁剧疆鏂囨湰妗嗗搴� -// textBox.setHeight(50); // 璁剧疆鏂囨湰妗嗛珮搴� -// textBox.setWrapType(WrapType.NONE); // 璁剧疆鏂囨湰妗嗕笉鑷姩鎹㈣ -// -// // 鍒涘缓鏂囨湰妗嗙殑娈佃惤鍜孯un瀵硅薄锛屽苟娣诲姞鍐呭 -// Paragraph para = new Paragraph(doc); -// Run run = new Run(doc, "杩欐槸鏂囨湰妗嗕腑鐨勫唴瀹�"); -// para.appendChild(run); -// textBox.appendChild(para); -// -// // 娣诲姞鏂囨湰妗嗗埌Word鏂囨。 -// doc.getFirstSection().getBody().getFirstParagraph().appendChild(textBox); -// -// // 淇濆瓨淇敼鍚庣殑Word鏂囨。 -// doc.save("D:\\妗岄潰鏂囦欢\\TaiTest_out_"+ UUID.randomUUID().toString() +".docx"); -// - // 鍒涘缓涓�涓┖鐨刉ord鏂囨。 -// XWPFDocument document = new XWPFDocument(); -// -// // 鍒涘缓涓�涓钀� -// XWPFParagraph paragraph = document.createParagraph(); -// -// // 鍒涘缓涓�涓枃鏈 -// XWPFRun run = paragraph.createRun(); -// CTTextbox textbox = run.getCTR().addNewTextbox(); -// CTTextboxContent textboxContent = textbox.addNewTextboxContent(); -// -// // 璁剧疆鏂囨湰妗嗙殑浣嶇疆鍜屽ぇ灏� -// textbox.setInset(new BigInteger("0")); -// textboxContent.setWMode(STTextbox.WMode.WINDOW); -// textboxContent.setNoBorder(true); -// textboxContent.setAnchor(STTextAnchoringType.MIDDLE); -// textboxContent.setAnchorCenter(true); -// -// // 璁剧疆鏂囨湰妗嗙殑鍐呭 -// textboxContent.setT("This is a text box."); -// -// // 淇濆瓨鏂囨。 -// try { -// FileOutputStream out = new FileOutputStream("example.docx"); -// document.write(out); -// out.close(); -// System.out.println("Word鏂囨。鍒涘缓鎴愬姛锛�"); -// } catch (Exception e) { -// e.printStackTrace(); -// } + ZipSecureFile.setMinInflateRatio(0.001); + try (OutputStream os = new FileOutputStream("D:\\妗岄潰鏂囦欢\\鍙垹闄\LAQ鑻辨枃鎶ュ憡"+UUID.randomUUID().toString()+".docx"); + XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("D:\\妗岄潰鏂囦欢\\LAQ鑻辨枃鎶ュ憡-鍙犲姞鐗堟湰.docx"))) { + changeChartNew(document,laqTemplateList); + document.write(os); + } catch (Exception e) { + e.printStackTrace(); + } + + + System.out.println("澶勭悊瀹屾垚..."); } + + /** + * 鍒濆鍖栨暟鎹� + * @param list + * @return + */ + public static Map<Integer,List<SeriesData>> initNewData(List<LAQTemplate> list) + { + Map<Integer,List<SeriesData>> seriesDataMap = new HashMap<>(); + for (int i = 0; i < list.size(); i++) + { + LAQTemplate laqTemplate = list.get(i); + Double olas = Double.valueOf(laqTemplate.getOlas().toString()); + Double perfomance; + if(laqTemplate.getPerfomance()!= null) + { + perfomance = Double.valueOf(laqTemplate.getPerfomance().toString()); + } + else { + perfomance = 0.0; + } + + List<SeriesData> seriesDatas = new ArrayList<>(); + seriesDatas = Arrays.asList( + new SeriesData("X 鍊�", Arrays.asList( + new RowData("X 鍊�", olas) + )), + new SeriesData("Y 鍊�", Arrays.asList( + new RowData("Y 鍊�", perfomance) + )) + ); + + + Integer box = laqTemplate.getBox(); + if(olas>=66) + { + seriesDataMap.put(100+i+1,seriesDatas); + } + else if(olas>33&&olas<66) + { + seriesDataMap.put(200+i+1,seriesDatas); + } + else + { + seriesDataMap.put(300+i+1,seriesDatas); + } + } + + return seriesDataMap; + } + + public static Map<Integer,List<SeriesData>> initData(Map<String, Object> textMap){ + Map<Integer,List<SeriesData>> seriesDataMap = new HashMap<>(); + //鑾峰彇鍛戒腑鏁板�� + Double olas = Double.valueOf((String)textMap.get("olas")); + Double ca = Double.valueOf((String)textMap.get("ca")); + Double bla = Double.valueOf((String)textMap.get("bla")); + Double nr = Double.valueOf((String)textMap.get("nr")); + Double ar = Double.valueOf((String)textMap.get("ar")); + Double open = Double.valueOf((String)textMap.get("open")); + Double cons = Double.valueOf((String)textMap.get("cons")); + Double extra = Double.valueOf((String)textMap.get("extra")); + Double agree = Double.valueOf((String)textMap.get("agree")); + Double emosta = Double.valueOf((String)textMap.get("emosta")); + Double perfomance; + if(textMap.get("perfomance") != null){ + perfomance = Double.valueOf((String)textMap.get("perfomance")); + }else{ + perfomance = -1D; + } + + + // 鍥捐〃1 + List<SeriesData> seriesDatas = new ArrayList<>(); + // 鍥捐〃1 + seriesDatas = Arrays.asList( + new SeriesData("X 鍊�", Arrays.asList( + new RowData("X 鍊�", perfomance.doubleValue() == -1 ? perfomance.doubleValue() : olas.doubleValue()) + )), + new SeriesData("Y 鍊�", Arrays.asList( + new RowData("Y 鍊�", perfomance.doubleValue()) + )) + ); + seriesDataMap.put(1,seriesDatas); + + + seriesDatas = Arrays.asList( + new SeriesData("X 鍊�", Arrays.asList( + new RowData("X 鍊�", 95) + )), + new SeriesData("Y 鍊�", Arrays.asList( + new RowData("Y 鍊�", 87) + )) + ); + seriesDataMap.put(2,seriesDatas); + + + seriesDatas = Arrays.asList( + new SeriesData("X 鍊�", Arrays.asList( + new RowData("X 鍊�", 96) + )), + new SeriesData("Y 鍊�", Arrays.asList( + new RowData("Y 鍊�", 90) + )) + ); + seriesDataMap.put(3,seriesDatas); + + + + return seriesDataMap; + } + + + /** + * 鑾峰彇鎸囧畾绫诲瀷鐨凷eriesData銆� + * @param list + * @param type + * @return + */ + public static Map<Integer,List<SeriesData>> getSeriesDataByType(List<LAQTemplate> list,Integer type) + { + Map<Integer,List<SeriesData>> seriesDataMap = new HashMap<>(); + int dataIndex =1; + for (int i = 0; i < list.size(); i++) + { + LAQTemplate laqTemplate = list.get(i); + Double olas = Double.valueOf(laqTemplate.getOlas().toString()); + Double perfomance; + if(laqTemplate.getPerfomance()!= null) + { + perfomance = Double.valueOf(laqTemplate.getPerfomance().toString()); + } + else { + perfomance = 0.0; + } + + List<SeriesData> seriesDatas = new ArrayList<>(); + seriesDatas = Arrays.asList( + new SeriesData("X 鍊�", Arrays.asList( + new RowData("X 鍊�", olas) + )), + new SeriesData("Y 鍊�", Arrays.asList( + new RowData("Y 鍊�", perfomance) + )) + ); + + + if(type==1) + { + if(olas>67) + { + seriesDataMap.put(dataIndex,seriesDatas); + dataIndex++; + } + } + + if(type==2) + { + if(olas<=67 && olas>33) + { + seriesDataMap.put(dataIndex,seriesDatas); + dataIndex++; + } + } + + if(type==3) + { + if(olas<=33) + { + seriesDataMap.put(dataIndex,seriesDatas); + dataIndex++; + } + } + } + + return seriesDataMap; + } + + + public static Map<Integer,String> getSeriesDataLablesByType(List<LAQTemplate> list,Integer type) + { + Map<Integer,String> seriesDataMapLables = new HashMap<>(); + int dataIndex =1; + for (int i = 0; i < list.size(); i++) + { + LAQTemplate laqTemplate = list.get(i); + Double olas = Double.valueOf(laqTemplate.getOlas().toString()); + Double perfomance; + if(laqTemplate.getPerfomance()!= null) + { + perfomance = Double.valueOf(laqTemplate.getPerfomance().toString()); + } + else { + perfomance = 0.0; + } + + + String olasStr = olas.toString(); + String perfomanceStr = perfomance.toString(); + + // 鍒ゆ柇灏忔暟閮ㄥ垎鏄惁涓�0 + if (Math.abs(olas - Math.floor(olas)) < 1e-9) { + // 濡傛灉灏忔暟閮ㄥ垎鎺ヨ繎浜�0锛屽垯鍙栨暣鏁伴儴鍒� + olasStr = String.valueOf(olas.intValue()); + } + + if (Math.abs(perfomance - Math.floor(perfomance)) < 1e-9) { + // 濡傛灉灏忔暟閮ㄥ垎鎺ヨ繎浜�0锛屽垯鍙栨暣鏁伴儴鍒� + perfomanceStr = String.valueOf(perfomance.intValue()); + } + + if(type==1) + { + if(olas>67) + { + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); + dataIndex++; + } + } + + if(type==2) + { + if(olas<=67 && olas>33) + { + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); + dataIndex++; + } + } + + if(type==3) + { + if(olas<=33) + { + seriesDataMapLables.put(dataIndex,laqTemplate.getName()+"("+olasStr+"-"+perfomanceStr+")"); + + dataIndex++; + } + } + } + + return seriesDataMapLables; + } + + + public static void changeChartNew(XWPFDocument document,List<LAQTemplate> list) { + if(list.size()<=0){ + return; + } + try { + //鑾峰彇鏁版嵁 + Map<Integer,List<SeriesData>> seriesDataMapTop = getSeriesDataByType(list,1); + Map<Integer,List<SeriesData>> seriesDataMapMiddle = getSeriesDataByType(list,2); + Map<Integer,List<SeriesData>> seriesDataMapBottom = getSeriesDataByType(list,3); + + //鑾峰彇鏍囩 + Map<Integer,String> seriesDataLablesTop = getSeriesDataLablesByType(list,1); + Map<Integer,String> seriesDataLablesMiddle = getSeriesDataLablesByType(list,2); + Map<Integer,String> seriesDataLablesBottom = getSeriesDataLablesByType(list,3); + + List<POIXMLDocumentPart> relations = document.getRelations(); + int barIndex = 1; + + //涓変釜鍥捐〃鐨勪釜鏁� + int topIndex =1; + int middleIndex =1; + int bottomIndex =1; + + for (POIXMLDocumentPart part : relations) { + if (part instanceof XWPFChart) { + // 鍥捐〃鍏冪礌 + XWPFChart chart = (XWPFChart) part; + // 鏌ョ湅閲岄潰鐨勫浘琛ㄦ暟鎹紝鎵嶈兘鐭ラ亾鏄粈涔堝浘琛� + CTPlotArea plot = chart.getCTChart().getPlotArea(); + getChartType(chart); + // excel鍐呯疆琛ㄦ牸 + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + //鏁g偣鍥� + if (!plot.getScatterChartList().isEmpty()) { + CTScatterChart scatterChart = plot.getScatterChartArray(0); + //绗竴涓暎鐐瑰浘1-90锛岀浜屼釜91-180锛岀涓変釜181-270 + System.out.println("绗�"+barIndex+"涓暎鐐瑰浘"); + String lables = ""; + List<SeriesData> seriesDatas = new ArrayList<>(); + + // 鑾峰彇鍥捐〃鐨勫�艰酱,骞舵妸鏈�灏忓�艰缃负-0锛� + // chart.getAxes().get(1) 鑾峰彇鐨勭旱鍧愭爣锛宑hart.getAxes().get(0) 鑾峰彇鐨勬槸妯潗鏍� + chart.getAxes().get(1).setMinimum(0); + Double maximum = chart.getAxes().get(0).getMaximum(); + + Double minimum = chart.getAxes().get(0).getMinimum(); +// System.out.println("X杞存渶灏忓�兼槸锛�"+minimum+",鏈�澶у�兼槸锛�"+maximum); + + //鍒ゆ柇鍥捐〃鎵�鍦ㄤ綅缃� + if(minimum>=67) + { + seriesDatas = seriesDataMapTop.get(topIndex); + lables = seriesDataLablesTop.get(topIndex); + topIndex++; +// System.out.println("绗竴椤靛浘琛ㄤ釜鏁帮細"+topIndex+"锛屾爣绛撅細"+lables+";"+"X杞存渶灏忓�兼槸锛�"+minimum+",鏈�澶у�兼槸锛�"+maximum); + } + else if(minimum>=33) + { + seriesDatas = seriesDataMapMiddle.get(middleIndex); + lables = seriesDataLablesMiddle.get(middleIndex); +// System.out.println("绗簩椤靛浘琛ㄤ釜鏁帮細"+middleIndex); + + middleIndex++; + } + else + { + seriesDatas = seriesDataMapBottom.get(bottomIndex); + lables = seriesDataLablesBottom.get(bottomIndex); + bottomIndex++; +// System.out.println("绗笁椤靛浘琛ㄤ釜鏁帮細"+bottomIndex); + } + + + barIndex++; + + + if(seriesDatas==null) + { + for (CTScatterSer ser : scatterChart.getSerList()) { + ser.getTx().getStrRef().getStrCache().getPtArray(0).setV(""); + } + continue; + } + + //鍒锋柊鍐呯疆excel鏁版嵁 + refreshExcel(seriesDatas, sheet); + workbook.write(chart.getPackagePart().getOutputStream()); + int i = 0; + for (CTScatterSer ser : scatterChart.getSerList()) { + //鏇存柊鏁g偣鍥剧紦瀛� + updateScatterChart(seriesDatas, ser.getXVal(), ser.getYVal()); + ser.getTx().getStrRef().getStrCache().getPtArray(0).setV(lables); + ++i; + } + + } + + + + } + } + + + System.out.println("绗竴椤靛浘琛ㄤ釜鏁帮細"+(topIndex-1)); + System.out.println("绗簩椤靛浘琛ㄤ釜鏁帮細"+(middleIndex-1)); + System.out.println("绗笁椤靛浘琛ㄤ釜鏁帮細"+(bottomIndex-1)); + + } catch (Exception e) { + e.printStackTrace(); + log.error("LAQ.鏇挎崲word鍥捐〃 鏁g偣鍥惧浘褰㈠け璐ワ細{}",e); + } + } + + + public static void changeChart(XWPFDocument document, Map<Integer,List<SeriesData>> seriesDataMap, Map<String, Object> textMap,Map<Integer,String> lableDatas) { + if(StringUtils.isEmpty(textMap)){ + return; + } + try { + + + List<POIXMLDocumentPart> relations = document.getRelations(); + int barIndex = 1; + int chartIndex =1;//鏁g偣鍥捐〃鐨勬暟閲� + for (POIXMLDocumentPart part : relations) { + if (part instanceof XWPFChart) { + // 鍥捐〃鍏冪礌 + XWPFChart chart = (XWPFChart) part; + // 鏌ョ湅閲岄潰鐨勫浘琛ㄦ暟鎹紝鎵嶈兘鐭ラ亾鏄粈涔堝浘琛� + CTPlotArea plot = chart.getCTChart().getPlotArea(); + getChartType(chart); + // excel鍐呯疆琛ㄦ牸 + XSSFWorkbook workbook = chart.getWorkbook(); + XSSFSheet sheet = workbook.getSheetAt(0); + + + //鏁g偣鍥� + if (!plot.getScatterChartList().isEmpty()) { + + if(chartIndex>=1&&chartIndex<=90) + { + + } + + + List<SeriesData> seriesDatas = seriesDataMap.get(barIndex); + + CTScatterChart scatterChart = plot.getScatterChartArray(0); + System.out.println("鏁g偣鍥緖"+plot.getScatterChartList().size()+"}"); + + + if(seriesDatas==null) + { + for (CTScatterSer ser : scatterChart.getSerList()) { + ser.getTx().getStrRef().getStrCache().getPtArray(0).setV(""); + } + continue; + } + + // 鑾峰彇鍥捐〃鐨勫�艰酱,骞舵妸鏈�灏忓�艰缃负-0锛� + // chart.getAxes().get(1) 鑾峰彇鐨勭旱鍧愭爣锛宑hart.getAxes().get(0) 鑾峰彇鐨勬槸妯潗鏍� + chart.getAxes().get(1).setMinimum(0); + Double maximum = chart.getAxes().get(1).getMaximum(); + Double minimum = chart.getAxes().get(1).getMinimum(); + + + System.out.println("銆愬紑濮嬨�戞渶澶э細"+maximum+",鏈�灏忥細"+minimum); + + + //鍒锋柊鍐呯疆excel鏁版嵁 + refreshExcel(seriesDatas, sheet); + workbook.write(chart.getPackagePart().getOutputStream()); + int i = 0; + for (CTScatterSer ser : scatterChart.getSerList()) { + + //鏇存柊鏁g偣鍥剧紦瀛� + updateScatterChart(seriesDatas, ser.getXVal(), ser.getYVal()); + + String lables = lableDatas.get(barIndex); + ser.getTx().getStrRef().getStrCache().getPtArray(0).setV(lables); + +// updateDLbls(ser); + ++i; + } + + barIndex++; + chartIndex++; + } + + + + } + } + + } catch (Exception e) { + e.printStackTrace(); + log.error("LAQ.鏇挎崲word鍥捐〃 鏁g偣鍥惧浘褰㈠け璐ワ細{}",e); + } + } + + /** + * 鏇存柊鏍囩 + * @param ser + */ + private static void updateDLbls(CTScatterSer ser) { + + // 璁剧疆鏁g偣鐨勫ぇ灏� +// CTMarkerSize ctMarkerSize = CTMarkerSize.Factory.newInstance(); +// ctMarkerSize.setVal((short)0); +// ser.getMarker().setSize(ctMarkerSize); + + ser.getTx().getStrRef().getStrCache().getPtArray(0).setV(UUID.randomUUID().toString()); + + } + + + + + /** + * 鏇存柊鍥捐〃鐨勫叧鑱� excel + * + * @param seriesDatas + * @param sheet + */ + protected static void refreshExcel(List<SeriesData> seriesDatas, XSSFSheet sheet) { + XSSFRow title = sheet.getRow(0); + //閬嶅巻鏁版嵁鏁扮粍杩涜excel璧嬪�� + for (int i = 0; i < seriesDatas.size(); i++) { + SeriesData data = seriesDatas.get(i); + if (data.name != null && !data.name.isEmpty()) { + // 绗竴琛屾爣棰� + XSSFCell cell = title.getCell(i); + if (cell == null) { + cell = title.createCell(i + 1); + } + cell.setCellValue(data.name); + } + int size = data.value.size(); + //閬嶅巻鏁版嵁杩涜璧嬪�� + for (int j = 0; j < size; j++) { + //浠庣浜岃寮�濮嬭祴鍊� + XSSFRow row = sheet.getRow(j + 1); + if (row == null) { + row = sheet.createRow(j + 1); + } + RowData cellValu = data.value.get(j); + + //绗竴鍒楀紑濮嬭祴鍊� + XSSFCell cell = row.getCell(i); + if (cell == null) { + cell = row.createCell(i); + } + //浼樺厛浣跨敤瀛楃涓插�� + if(StringUtils.isNotEmpty(cellValu.valueStr)){ + cell.setCellValue(cellValu.valueStr); + }else{ + if(cellValu.value != -1){ + cell.setCellValue(cellValu.value); + }else{ + cell.setCellValue((String) null); + } + } + + } + //鍒犻櫎澶氫綑琛屾暟 + int lastRowNum = sheet.getLastRowNum(); + //log.error("lastRowNum:{},size:{}",lastRowNum,size); + if (lastRowNum > size) { + for (int idx = lastRowNum; idx > size; idx--) { + if(sheet.getRow(idx) == null){ + continue; + } + sheet.removeRow(sheet.getRow(idx)); + } + } + } + } + + + /** + * 鏇存柊 鏁g偣鍥� 缂撳瓨鏁版嵁 + * + * @param seriesDatas 鏁版嵁 + * @param xDataSource X鍧愭爣鏁版嵁缂撳瓨 + * @param yDataSource Y鍧愭爣鏁版嵁缂撳瓨 + */ + protected static void updateScatterChart(List<SeriesData> seriesDatas, CTAxDataSource xDataSource, + CTNumDataSource yDataSource) { + + //鑾峰彇xy鍧愭爣鏁版嵁鏉℃暟 + long xNumCnt = xDataSource.getNumRef().getNumCache().getPtCount().getVal(); + long yNumCnt = yDataSource.getNumRef().getNumCache().getPtCount().getVal(); + + + System.out.println("[鏈夋暟鎹甝鏉℃暟 x:"+xNumCnt+" y:"+yNumCnt); + + //鑾峰彇XY鏈�鏂版暟鎹� + List<RowData> xRowDatas = seriesDatas.get(0).getValue(); + int xRowDataSize = xRowDatas.size(); + List<RowData> yRowDatas = seriesDatas.get(1).getValue(); + int yRowDataSize = yRowDatas.size(); + + //鏇存柊X鍧愭爣缂撳瓨 + for (int i = 0; i < xRowDatas.size(); i++) { + RowData cellValu = xRowDatas.get(i); + CTNumVal val = xNumCnt > i ? xDataSource.getNumRef().getNumCache().getPtArray(i) + : xDataSource.getNumRef().getNumCache().addNewPt(); + val.setIdx(i); + if(cellValu.value == -1){ + val.setV(null); + }else { + val.setV(String.format("%.0f", cellValu.value)); + } + } + + //鏇存柊Y鍧愭爣缂撳瓨 + for (int i = 0; i < yRowDatas.size(); i++) { + RowData cellValu = yRowDatas.get(i); + CTNumVal val = yNumCnt > i ? yDataSource.getNumRef().getNumCache().getPtArray(i) + : yDataSource.getNumRef().getNumCache().addNewPt(); + val.setIdx(i); + if(cellValu.value == -1){ + val.setV(null); + }else { + val.setV(String.format("%.0f", cellValu.value)); + } + + } + + // 鏇存柊瀵瑰簲excel鐨勮寖鍥� + xDataSource.getNumRef().setF( + replaceRowEnd(xDataSource.getNumRef().getF(), + xNumCnt, + xRowDataSize)); + yDataSource.getNumRef().setF( + replaceRowEnd(yDataSource.getNumRef().getF(), + yNumCnt, + yRowDataSize)); + + // 鍒犻櫎澶氱殑 + if (xNumCnt > xRowDataSize) { + for (int idx = xRowDataSize; idx < xNumCnt; idx++) { + xDataSource.getNumRef().getNumCache().removePt(xRowDataSize); + } + } + if (yNumCnt > yRowDataSize) { + for (int idx = yRowDataSize; idx < yNumCnt; idx++) { + yDataSource.getNumRef().getNumCache().removePt(yRowDataSize); + } + } + // 鏇存柊涓暟 + xDataSource.getNumRef().getNumCache().getPtCount().setVal(xRowDataSize); + // 鏇存柊涓暟 + yDataSource.getNumRef().getNumCache().getPtCount().setVal(yRowDataSize); + } + + + + /** + * 鏇挎崲 褰㈠锛� Sheet1!$A$2:$A$4 鐨勫瓧绗� + * + * @param range + * @return + */ + public static String replaceRowEnd(String range, long oldSize, long newSize) { + Pattern pattern = Pattern.compile("(:\\$[A-Z]+\\$)(\\d+)"); + Matcher matcher = pattern.matcher(range); + if (matcher.find()) { + long old = Long.parseLong(matcher.group(2)); + return range.replaceAll("(:\\$[A-Z]+\\$)(\\d+)", "$1" + Long.toString(old - oldSize + newSize)); + } + return range; + } + + /** + * 鑾峰彇鍥捐〃鏈夊摢浜涚被鍨� + * @param part + */ + public static void getChartType(POIXMLDocumentPart part){ + XWPFChart chart = (XWPFChart) part; + //鏍规嵁灞炴�х涓�鍒楀悕绉板垏鎹㈡暟鎹被鍨� + CTChart ctChart = chart.getCTChart(); + CTPlotArea plotArea = ctChart.getPlotArea(); + + if (!plotArea.getBarChartList().isEmpty()) { + System.out.println("鏌辩姸鍥緖"+plotArea.getBarChartList().size()+"}"); + } + if (!plotArea.getPieChartList().isEmpty()) { + System.out.println("楗煎浘{"+plotArea.getPieChartList().size()+"}"); + } + if (!plotArea.getLineChartList().isEmpty()) { + System.out.println("绾垮舰鍥緖"+plotArea.getLineChartList().size()+"}"); + } + if (!plotArea.getBar3DChartList().isEmpty()) { + System.out.println("鏌辩姸鍥�3D{"+plotArea.getBar3DChartList().size()+"}"); + } + if (!plotArea.getScatterChartList().isEmpty()) { +// System.out.println("鏁g偣鍥剧被鍨媨"+plotArea.getScatterChartList().size()+"}"); + } + } } -- Gitblit v1.9.1