ソースを参照

[性能验证]添加Excel报告文件下载

liyuan 2 年 前
コミット
6c7ca18281

+ 0 - 5
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/PVExcelImportServer.java

@@ -4,20 +4,15 @@ import com.lc.ibps.components.poi.excel.entity.ImportParams;
 import com.lc.ibps.components.poi.excel.entity.params.ExcelCollectionParams;
 import com.lc.ibps.components.poi.excel.entity.result.ExcelImportResult;
 import com.lc.ibps.components.poi.excel.imports.ExcelImportServer;
-import com.lc.ibps.components.poi.util.PoiPublicUtil;
 import com.lc.ibps.components.verification.excel.model.ExcelSheetRecord;
 import org.apache.commons.lang3.StringUtils;
-import org.apache.poi.hssf.usermodel.HSSFSheet;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.poifs.filesystem.DocumentFactoryHelper;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.ss.formula.functions.T;
 import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.PictureData;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

+ 29 - 27
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/PVExcelUtil.java

@@ -15,7 +15,6 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.math.BigDecimal;
@@ -25,14 +24,13 @@ import java.util.*;
 public class PVExcelUtil {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(PVExcelUtil.class);
+
     /**
      * 根据Map创建对应的Excel
-     * @param entity
-     *            表格标题属性
-     * @param entityList
-     *            Map对象列表
-     * @param dataSet
-     *            Excel对象数据List
+     *
+     * @param entity     表格标题属性
+     * @param entityList Map对象列表
+     * @param dataSet    Excel对象数据List
      */
     public static Workbook exportExcel(ExportParams entity, List<ExcelExportEntity> entityList,
                                        Collection<? extends Map<?, ?>> dataSet) {
@@ -48,30 +46,31 @@ public class PVExcelUtil {
         return workbook;
     }
 
-    public static Workbook exportExcelReport(ExportParams entity, Class<?> pojoClass,
-                                       Collection<?> dataSet) {
+    public static Workbook exportExcelReport(List<Map<String, Object>> list, ExcelType type) {
         Workbook workbook;
-        if (ExcelType.HSSF.equals(entity.getType())) {
+        if (ExcelType.HSSF.equals(type)) {
             workbook = new HSSFWorkbook();
-        } else if (dataSet.size() < 10000) {
-            workbook = new XSSFWorkbook();
         } else {
-            workbook = new SXSSFWorkbook();
+            workbook = new XSSFWorkbook();
+        }
+        for (Map<String, Object> map : list) {
+            ExcelExportServer server = new PVExcelExportReportServer();
+            server.createSheetForMap(workbook, (ExportParams) map.get("params"),
+                    (List<ExcelExportEntity>) map.get("entity"), (Collection<?>) map.get("data"));
         }
-        new PVExcelExportReportServer().createSheet(workbook, entity, pojoClass, dataSet);
         return workbook;
     }
 
     /**
      * 一个excel 创建多个sheet
      *
-     * @param list
-     *            多个Map key title 对应表格Title key entity 对应表格对应实体 key data
-     *            Collection 数据
+     * @param list 多个Map key title 对应表格Title key entity 对应表格对应实体 key data
+     *             Collection 数据
      * @return
      */
-    public static Workbook exportExcelWithMultiSheet(List<ExportParams> entities, List<ExcelExportEntity> entityList,
-                                       Collection<? extends Map<?, ?>> dataSet) {
+    public static Workbook exportExcelTemplateWithMultiSheet(
+            List<ExportParams> entities, List<ExcelExportEntity> entityList,
+            Collection<? extends Map<?, ?>> dataSet) {
         Workbook workbook;
         if (ExcelType.HSSF.equals(entities.get(0).getType())) {
             workbook = new HSSFWorkbook();
@@ -80,8 +79,9 @@ public class PVExcelUtil {
         } else {
             workbook = new SXSSFWorkbook();
         }
-        for (ExportParams entity:entities) {
-            new PVExcelExportTemplateServer().createSheetForMap(workbook, entity, entityList, new ArrayList<>(dataSet));
+        for (ExportParams entity : entities) {
+            new PVExcelExportTemplateServer().createSheetForMap(
+                    workbook, entity, entityList, new ArrayList<>(dataSet));
         }
 
         return workbook;
@@ -111,15 +111,16 @@ public class PVExcelUtil {
             IOUtils.closeQuietly(in);
         }
     }
+
     public static String getCellValueByCell(Sheet sheet, int row, int column) {
         Row rowData = sheet.getRow(row);
         Cell cell = rowData.getCell(column);
         //判断是否为null或空串
-        if (cell==null || cell.toString().trim().equals("")) {
+        if (cell == null || cell.toString().trim().equals("")) {
             return "";
         }
         String cellValue = "";
-        int cellType=cell.getCellType();
+        int cellType = cell.getCellType();
         switch (cellType) {
             case Cell.CELL_TYPE_NUMERIC: // 数字
                 short format = cell.getCellStyle().getDataFormat();
@@ -135,7 +136,7 @@ public class PVExcelUtil {
                         Date date = org.apache.poi.ss.usermodel.DateUtil
                                 .getJavaDate(value);
                         cellValue = sdf.format(date);
-                    }else {// 日期
+                    } else {// 日期
                         sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                     }
                     try {
@@ -146,10 +147,10 @@ public class PVExcelUtil {
                         } catch (Exception e1) {
                             e1.printStackTrace();
                         }
-                    }finally{
+                    } finally {
                         sdf = null;
                     }
-                }  else {
+                } else {
                     BigDecimal bd = new BigDecimal(cell.getNumericCellValue());
                     cellValue = bd.toPlainString();// 数值 这种用BigDecimal包装再获取plainString,可以防止获取到科学计数值
                 }
@@ -158,7 +159,8 @@ public class PVExcelUtil {
                 cellValue = cell.getStringCellValue();
                 break;
             case Cell.CELL_TYPE_BOOLEAN: // Boolean
-                cellValue = cell.getBooleanCellValue()+"";;
+                cellValue = cell.getBooleanCellValue() + "";
+                ;
                 break;
             case Cell.CELL_TYPE_FORMULA: // 公式
                 cellValue = cell.getCellFormula();

+ 9 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/PVTest.java

@@ -8,6 +8,7 @@ import com.lc.ibps.components.poi.util.PoiPublicUtil;
 import com.lc.ibps.components.verification.excel.model.ExcelSheetRecord;
 import com.lc.ibps.components.verification.excel.model.InspectionConfigVO;
 import com.lc.ibps.components.verification.excel.model.InspectionItemVO;
+import com.lc.ibps.components.verification.excel.model.InspectionVO;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.junit.Test;
 
@@ -57,6 +58,14 @@ public class PVTest {
         InspectionItemVO item = new InspectionItemVO(config);
         item.importExcelRecord(in);
 
+        InspectionVO vo = new InspectionVO();
+        vo.setName("TEST");
+        vo.getItem().add(item);
+        final Workbook workbook = vo.exportExcelReport();
+        FileOutputStream fos = new FileOutputStream(String.format("D:/excel/%s.xlsx",vo.getName()));
+        workbook.write(fos);
+        fos.close();
+
 
     }
 }

+ 69 - 6
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/model/InspectionItemVO.java

@@ -9,15 +9,11 @@ import com.lc.ibps.components.poi.excel.entity.vo.BaseEntityTypeConstants;
 import com.lc.ibps.components.verification.excel.PVExcelExportStyler;
 import com.lc.ibps.components.verification.excel.PVExcelUtil;
 import com.lc.ibps.components.verification.funcs.xValue;
-import org.apache.commons.lang3.ArrayUtils;
 import org.apache.poi.ss.usermodel.Workbook;
 
 import java.io.InputStream;
 import java.time.LocalDate;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 /**
  * 每个检测指标对应的数据
@@ -27,6 +23,10 @@ public class InspectionItemVO {
     public static String DISPLAY_NUMBER = "次数";
     public static String DISPLAY_VALUE = "检测值";
 
+    public static String DISPLAY_LOG = "LOG 值";
+
+    public static String DISPLAY_DATE = "实验日期";
+
     private InspectionConfigVO config;
 
     //样本名 : 检查数据
@@ -73,11 +73,74 @@ public class InspectionItemVO {
             startDate = startDate.plusDays(1);
             exportParams.setType(ExcelType.XSSF);
         }
-        Workbook workbook = PVExcelUtil.exportExcelWithMultiSheet(exportParamsList, entity, list);
+        Workbook workbook = PVExcelUtil.exportExcelTemplateWithMultiSheet(exportParamsList, entity, list);
 
         return workbook;
     }
 
+    public Map<String, Object> buildExportReportParams() {
+        Map<String, Object> map = new HashMap<>();
+        //        server.createSheet(workbook, (ExportParams) map.get("params"),
+//                (List<ExcelExportEntity>) map.get("entity"), (Collection<?>) map.get("data"));
+//        List<ExcelExportEntity>
+        ExportParams exportParams = new ExportParams(config.getName() + " 性能验证报告", config.getName());
+        exportParams.setStyle(PVExcelExportStyler.class);
+        exportParams.setType(ExcelType.XSSF);
+        map.put("params", exportParams);
+
+        List<ExcelExportEntity> entity = new ArrayList<>();
+        ExcelExportEntity e1 = new ExcelExportEntity(DISPLAY_DATE, "date");
+        e1.setMergeVertical(true);
+        entity.add(e1);
+        ExcelExportEntity e2 = new ExcelExportEntity(DISPLAY_NUMBER, "number");
+        e2.setType(BaseEntityTypeConstants.DoubleType);
+//        e2.setMergeVertical(true);
+        entity.add(e2);
+
+        List<ExcelExportEntity> temp = new ArrayList<ExcelExportEntity>();
+        ExcelExportEntity e3 = new ExcelExportEntity(DISPLAY_VALUE, "value");
+//            e3.setType(BaseEntityTypeConstants.DoubleType);
+        temp.add(e3);
+        if (config.isConvert()) {
+            ExcelExportEntity e4 = new ExcelExportEntity(DISPLAY_LOG, "log");
+//                e4.setType(BaseEntityTypeConstants.DoubleType);
+            temp.add(e4);
+        }
+        for (int i = 0; i < config.getSpecimensNum(); i++) {
+            ExcelExportEntity excelEntity = new ExcelExportEntity(config.getSpecimensName()[i], "inspection" + i);
+
+            excelEntity.setList(temp);
+            entity.add(excelEntity);
+        }
+        map.put("entity", entity);
+
+        List<Map<String, Object>> data = new ArrayList<>();
+        LocalDate date = config.getStartDate();
+        for (int i = 0; i < config.getDays(); i++) {
+            for (int j = 0; j < config.getRepeatNum(); j++) {
+                Map<String, Object> item = new HashMap<>();
+                item.put("date", date.toString());
+                item.put("number", j + 1);
+                for (int k = 0; k < config.getSpecimensNum(); k++) {
+                    List<Map<String, Object>> sItem = new ArrayList<>();
+                    final RecordCalcVO recordCalcVO = record.get(config.getSpecimensName()[k]);
+                    Map<String, Object> sValue = new HashMap<>();
+                    sValue.put("value", recordCalcVO.getData()[i * config.getRepeatNum() + j].getDoub());
+                    if (config.isConvert()) {
+                        sValue.put("log", recordCalcVO.getConvertedData()[i * config.getRepeatNum() + j].getDoub());
+                    }
+                    sItem.add(sValue);
+                    item.put("inspection" + k, sItem);
+                }
+                data.add(item);
+            }
+            date = date.plusDays(1);
+        }
+        map.put("data", data);
+
+        return map;
+    }
+
     public void importExcelRecord(InputStream inputstream) {
         ImportParams params = new ImportParams();
         params.setTitleRows(2);

+ 34 - 1
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/model/InspectionVO.java

@@ -1,9 +1,42 @@
 package com.lc.ibps.components.verification.excel.model;
 
+import com.lc.ibps.components.poi.excel.entity.enmus.ExcelType;
+import com.lc.ibps.components.verification.excel.PVExcelUtil;
+import org.apache.poi.ss.usermodel.Workbook;
+
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
 
 public class InspectionVO {
 
+    private String name;
+    private List<InspectionItemVO> item = new ArrayList<>();
+
+    public Workbook exportExcelReport() {
+        List<Map<String, Object>> list = new ArrayList<>();
+        for (InspectionItemVO item : item) {
+            list.add(item.buildExportReportParams());
+        }
+
+        Workbook workbook = PVExcelUtil.exportExcelReport(list, ExcelType.XSSF);
+
+        return workbook;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public List<InspectionItemVO> getItem() {
+        return item;
+    }
 
-    private List<InspectionItemVO> data;
+    public void setItem(List<InspectionItemVO> item) {
+        this.item = item;
+    }
 }

+ 7 - 0
ibps-provider-root/modules/provider-business/src/main/java/com/lc/ibps/components/verification/excel/model/RecordCalcVO.java

@@ -57,4 +57,11 @@ public class RecordCalcVO {
         return targetValue - getAverage().getDoub();
     }
 
+    public xValue[] getData() {
+        return data;
+    }
+
+    public xValue[] getConvertedData() {
+        return convertedData;
+    }
 }