| | |
| | | package com.ots.project.tool.report.APIFan.chart; |
| | | |
| | | import com.ots.framework.config.EssConfig; |
| | | import com.ots.project.tool.report.MAQ.base.RowData; |
| | | import com.ots.project.tool.report.MAQ.base.SeriesData; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.poi.ooxml.POIXMLDocument; |
| | | import org.apache.poi.ooxml.POIXMLDocumentPart; |
| | | 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.openxmlformats.schemas.drawingml.x2006.chart.*; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.*; |
| | | |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.OutputStream; |
| | | import java.math.BigInteger; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | |
| | | textMap.put("P_Impmg11","65"); |
| | | textMap.put("BST1","66"); |
| | | textMap.put("BST2","67"); |
| | | textMap.put("Box","1"); |
| | | changeChart(document,textMap); |
| | | document.write(os); |
| | | }catch (Exception e){ |
| | |
| | | } |
| | | //图表数据 |
| | | changeAPIFanChart(document,seriesDatas); |
| | | |
| | | |
| | | //替换图片 |
| | | try{ |
| | | // 获取所有段落 |
| | | List<XWPFParagraph> paragraphs = document.getParagraphs(); |
| | | |
| | | //获取图片路径 |
| | | String selectedPic = EssConfig.getReportTemplates()+"apifanSelected.png"; |
| | | String defaultPic =EssConfig.getReportTemplates()+"apifanDefalut.png"; |
| | | |
| | | //测试用的 |
| | | // String selectedPic = "C:\\测评系统\\home\\reportTemplates\\apifanSelected.png"; |
| | | // String defaultPic ="C:\\测评系统\\home\\reportTemplates\\apifanDefalut.png"; |
| | | |
| | | |
| | | // 遍历段落进行替换操作 |
| | | synchronized (paragraphs) { |
| | | for (XWPFParagraph paragraph : paragraphs) { |
| | | String text = paragraph.getText(); |
| | | |
| | | //用%%号在前面被替换了 |
| | | if (text.contains("{{box0}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 0) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | 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; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果可信,可以直接解读(测试中未发现明显的“伪装好”倾向)"); |
| | | |
| | | } |
| | | if (text.contains("{{box1}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 1) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | 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; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果不可信(测试中有明显的“伪装好”倾向)"); |
| | | |
| | | } |
| | | if (text.contains("{{box2}}")) { // 检查是否包含要替换的文字变量 |
| | | String picturePath = ""; // 图片路径 |
| | | |
| | | String Box = (String) textMap.get("Box"); |
| | | int box = Integer.parseInt(Box); |
| | | if(box== 2) |
| | | { |
| | | picturePath =selectedPic; |
| | | } |
| | | else |
| | | { |
| | | picturePath = defaultPic; |
| | | } |
| | | |
| | | // 创建新的图片段落 |
| | | // XWPFParagraph paragraph = document.createParagraph(); |
| | | // 获取所有运行的列表 |
| | | 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; // 图片类型 |
| | | |
| | | // 将图片插入到段落中 |
| | | int width = Units.toEMU(20); // 图片宽度 |
| | | int height = Units.toEMU(20); // 图片高度 |
| | | run.addPicture(new FileInputStream(picturePath), pictureType, "image.png", width, height); |
| | | // 添加空格或制表符,使文本居中对齐 |
| | | int numberOfSpaces = 3; |
| | | for (int i = 0; i < numberOfSpaces; i++) { |
| | | run.setText(" "); |
| | | } |
| | | // 设置字符间距为居中对齐 |
| | | paragraph.setVerticalAlignment(TextAlignment.CENTER); |
| | | run.setText("结果可能可信,解读时要谨慎(测试中有一定的“伪装好倾向”)"); |
| | | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.out.println(ex.getMessage()); |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |