src/main/java/com/ots/common/utils/poi/WordUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllChart.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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); //如果是LAQ的全景图,设置图表 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())) { 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,往上移动一下。y轴调整 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++; } src/main/java/com/ots/project/tool/report/LAQ/chart/LAQAllPng.java
New file @@ -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("【执行py生成图表结果】"+line); } // 获取错误输出(用于调试) BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); while ((line = errorReader.readLine()) != null) { log.info("【执行py生成图表结果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()); } } }