Selaa lähdekoodia

导出工具类修改

zhoutg 4 vuotta sitten
vanhempi
commit
5c87379a7d

+ 32 - 63
cdssman-service/src/main/java/com/diagbot/facade/XLSXCovertCSVReader.java

@@ -1,7 +1,6 @@
 package com.diagbot.facade;
 
 import com.diagbot.entity.Ex;
-import com.diagbot.util.ListUtil;
 import com.diagbot.util.ReflectUtil;
 import com.diagbot.util.StringUtil;
 import com.google.common.collect.Lists;
@@ -107,14 +106,10 @@ public class XLSXCovertCSVReader {
         /**
          * Accepts objects needed while parsing.
          *
-         * @param styles
-         *            Table of styles
-         * @param strings
-         *            Table of shared strings
-         * @param cols
-         *            Minimum number of columns to show
-         * @param target
-         *            Sink for output
+         * @param styles  Table of styles
+         * @param strings Table of shared strings
+         * @param cols    Minimum number of columns to show
+         * @param target  Sink for output
          */
         public MyXSSFSheetHandler(StylesTable styles,
                                   ReadOnlySharedStringsTable strings, int cols, PrintStream target) {
@@ -163,26 +158,27 @@ public class XLSXCovertCSVReader {
                 this.formatString = null;
                 String cellType = attributes.getValue("t");
                 String cellStyleStr = attributes.getValue("s");
-                if ("b".equals(cellType))
+                if ("b".equals(cellType)) {
                     nextDataType = xssfDataType.BOOL;
-                else if ("e".equals(cellType))
+                } else if ("e".equals(cellType)) {
                     nextDataType = xssfDataType.ERROR;
-                else if ("inlineStr".equals(cellType))
+                } else if ("inlineStr".equals(cellType)) {
                     nextDataType = xssfDataType.INLINESTR;
-                else if ("s".equals(cellType))
+                } else if ("s".equals(cellType)) {
                     nextDataType = xssfDataType.SSTINDEX;
-                else if ("str".equals(cellType))
+                } else if ("str".equals(cellType)) {
                     nextDataType = xssfDataType.FORMULA;
-                else if (cellStyleStr != null) {
+                } else if (cellStyleStr != null) {
                     // It's a number, but almost certainly one
                     // with a special style or format
                     int styleIndex = Integer.parseInt(cellStyleStr);
                     XSSFCellStyle style = stylesTable.getStyleAt(styleIndex);
                     this.formatIndex = style.getDataFormat();
                     this.formatString = style.getDataFormatString();
-                    if (this.formatString == null)
+                    if (this.formatString == null) {
                         this.formatString = BuiltinFormats
                                 .getBuiltinFormat(this.formatIndex);
+                    }
                 }
             }
 
@@ -211,20 +207,20 @@ public class XLSXCovertCSVReader {
                         break;
 
                     case ERROR:
-                        thisStr = "\"ERROR:" + value.toString() + '"';
+                        thisStr = "ERROR:" + value.toString();
                         break;
 
                     case FORMULA:
                         // A formula could result in a string value,
                         // so always add double-quote characters.
-                        thisStr = '"' + value.toString() + '"';
+                        thisStr = value.toString();
                         break;
 
                     case INLINESTR:
                         // TODO: have seen an example of this, so it's untested.
                         XSSFRichTextString rtsi = new XSSFRichTextString(
                                 value.toString());
-                        thisStr = '"' + rtsi.toString() + '"';
+                        thisStr = rtsi.toString();
                         break;
 
                     case SSTINDEX:
@@ -233,7 +229,7 @@ public class XLSXCovertCSVReader {
                             int idx = Integer.parseInt(sstIndex);
                             XSSFRichTextString rtss = new XSSFRichTextString(
                                     sharedStringsTable.getEntryAt(idx));
-                            thisStr = '"' + rtss.toString() + '"';
+                            thisStr = rtss.toString();
                         } catch (NumberFormatException ex) {
                             output.println("Failed to parse SST index '" + sstIndex
                                     + "': " + ex.toString());
@@ -245,14 +241,15 @@ public class XLSXCovertCSVReader {
                         // 判断是否是日期格式
                         if (HSSFDateUtil.isADateFormat(this.formatIndex, n)) {
                             Double d = Double.parseDouble(n);
-                            Date date=HSSFDateUtil.getJavaDate(d);
-                            thisStr=formateDateToString(date);
-                        } else if (this.formatString != null)
+                            Date date = HSSFDateUtil.getJavaDate(d);
+                            thisStr = formateDateToString(date);
+                        } else if (this.formatString != null) {
                             thisStr = formatter.formatRawCellContents(
                                     Double.parseDouble(n), this.formatIndex,
                                     this.formatString);
-                        else
+                        } else {
                             thisStr = n;
+                        }
                         break;
 
                     default:
@@ -271,11 +268,11 @@ public class XLSXCovertCSVReader {
                 }
                 record[thisColumn] = thisStr;
                 // Update column
-                if (thisColumn > -1)
+                if (thisColumn > -1) {
                     lastColumnNumber = thisColumn;
+                }
 
             } else if ("row".equals(name)) {
-
                 // Print out any missing commas if needed
                 if (minColumns > 0) {
                     // Columns are 0 based
@@ -311,8 +308,9 @@ public class XLSXCovertCSVReader {
          */
         public void characters(char[] ch, int start, int length)
                 throws SAXException {
-            if (vIsOpen)
+            if (vIsOpen) {
                 value.append(ch, start, length);
+            }
         }
 
         /**
@@ -348,12 +346,9 @@ public class XLSXCovertCSVReader {
     /**
      * Creates a new XLSX -> CSV converter
      *
-     * @param pkg
-     *            The XLSX package to process
-     * @param output
-     *            The PrintStream to output the CSV to
-     * @param minColumns
-     *            The minimum number of columns to output, or -1 for no minimum
+     * @param pkg        The XLSX package to process
+     * @param output     The PrintStream to output the CSV to
+     * @param minColumns The minimum number of columns to output, or -1 for no minimum
      */
     public XLSXCovertCSVReader(OPCPackage pkg, PrintStream output,
                                String sheetName, int minColumns) {
@@ -414,25 +409,15 @@ public class XLSXCovertCSVReader {
                 ++index;
             }
         }
-        if (ListUtil.isNotEmpty(list)) {
-            for (String[] arr : list) {
-                for (int i = 0; i < arr.length; i++) {
-                    arr[i] = getObject(arr[i]);
-                }
-            }
-        }
         return list;
     }
 
     /**
      * 读取Excel
      *
-     * @param path
-     *            文件路径
-     * @param sheetName
-     *            sheet名称
-     * @param minColumns
-     *            列总数
+     * @param path       文件路径
+     * @param sheetName  sheet名称
+     * @param minColumns 列总数
      * @return
      * @throws SAXException
      * @throws ParserConfigurationException
@@ -463,7 +448,7 @@ public class XLSXCovertCSVReader {
      * @throws SAXException
      */
     private static List<String[]> readerExcel(InputStream inputStream, String sheetName,
-                                             int minColumns) throws IOException, OpenXML4JException,
+                                              int minColumns) throws IOException, OpenXML4JException,
             ParserConfigurationException, SAXException {
         OPCPackage p = OPCPackage.open(inputStream);
         XLSXCovertCSVReader xlsx2csv = new XLSXCovertCSVReader(p, System.out,
@@ -473,22 +458,6 @@ public class XLSXCovertCSVReader {
         return list;
     }
 
-    /**
-     * 去除前后的""
-     *
-     * @param s
-     * @return
-     */
-    public static String getObject(String s) {
-        if (StringUtil.isBlank(s)) {
-            return s;
-        }
-        if (s.startsWith("\"") && s.endsWith("\"")) {
-            s = s.substring(1, s.length() -1);
-        }
-        return s;
-    }
-
     /**
      * 读取数据
      *

+ 0 - 293
cdssman-service/src/main/java/com/diagbot/util/ExcelBigUtil.java

@@ -1,293 +0,0 @@
-package com.diagbot.util;
-
-import com.diagbot.entity.Ex;
-import com.diagbot.vo.KlDrugRegisterTestVO;
-import com.google.common.collect.Lists;
-import org.apache.commons.collections4.map.LinkedMap;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellType;
-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.xssf.usermodel.XSSFWorkbook;
-import org.springframework.web.multipart.MultipartFile;
-
-import java.io.FileInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.lang.reflect.Field;
-import java.util.ArrayList;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * @description:
- * @author: zhoutg
- * @time: 2021/5/24 19:58
- */
-public class ExcelBigUtil {
-
-    /**
-     * 读取数据
-     *
-     * @param file
-     * @param sheetName
-     * @param titleNo 标题所在行
-     * @param c
-     * @param <T>
-     * @return
-     */
-    public static <T> List<T> readData(MultipartFile file, String sheetName, int titleNo, Class<T> c) {
-        List<T> list = Lists.newArrayList();
-        try {
-            Sheet sheet = getSheet(file, sheetName);
-            List<String[]> stringList = getListData(sheet, titleNo);
-            return dealData(stringList, c);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return list;
-    }
-
-    /**
-     * 读取数据
-     *
-     * @param path
-     * @param sheetName
-     * @param titleNo
-     * @param c
-     * @param <T>
-     * @return
-     */
-    public static <T> List<T> readData(String path, String sheetName, int titleNo, Class<T> c) {
-        List<T> list = Lists.newArrayList();
-        try {
-            Sheet sheet = getSheet(path, sheetName);
-            List<String[]> stringList = getListData(sheet, titleNo);
-            return dealData(stringList, c);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return list;
-    }
-
-    /**
-     * 获取Sheet
-     *
-     * @param filePath
-     * @param sheetName
-     * @return
-     */
-    private static Sheet getSheet(String filePath, String sheetName) {
-        Sheet sheet = null;
-        FileInputStream fileInputStream = null;
-        try {
-            fileInputStream = new FileInputStream(filePath);
-            XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream);
-            if (StringUtil.isBlank(sheetName)) {
-                sheet = sheets.getSheetAt(0);
-            } else {
-                sheet = sheets.getSheet(sheetName);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return sheet;
-    }
-
-    /**
-     * 获取Sheet
-     *
-     * @param file
-     * @param sheetName
-     * @return
-     */
-    private static Sheet getSheet(MultipartFile file, String sheetName) {
-        Sheet sheet = null;
-        try {
-            // XSSFWorkbook sheets = new XSSFWorkbook(file.getInputStream());
-            Workbook workbook = getWorkBook(file);
-            //获取sheet
-            if (StringUtil.isBlank(sheetName)) {
-                sheet = workbook.getSheetAt(0);
-            } else {
-                sheet = workbook.getSheet(sheetName);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return sheet;
-    }
-
-    /**
-     * 得到Workbook对象
-     * @param file
-     * @return
-     * @throws IOException
-     */
-    public static Workbook getWorkBook(MultipartFile file) throws IOException {
-        //这样写  excel 能兼容03和07
-        InputStream is = file.getInputStream();
-        Workbook hssfWorkbook = null;
-        try {
-            hssfWorkbook = new HSSFWorkbook(is);
-        } catch (Exception ex) {
-            is =file.getInputStream();
-            hssfWorkbook = new XSSFWorkbook(is);
-        }
-        return hssfWorkbook;
-    }
-
-    /**
-     * 将sheet数据放入List
-     *
-     * @param sheet
-     * @param title
-     * @return
-     */
-    private static List<String[]> getListData(Sheet sheet, Integer title) {
-        List<String[]> list = new ArrayList<>();
-        int colLength = sheet.getRow(title).getLastCellNum();
-        for (int j = title; j <= sheet.getLastRowNum(); j++) {
-            Row row = sheet.getRow(j);
-            if (row != null) { // 空行数据
-                String[] str = new String[colLength];
-                for (int i = 0; i < colLength; i++) {
-                    Cell cell = row.getCell(i);
-                    if (cell == null) {
-                        str[i] = null;
-                    } else {
-                        cell.setCellType(CellType.STRING);
-                        str[i] = cell.toString();
-                    }
-                }
-                list.add(str);
-            }
-        }
-        return list;
-    }
-
-    /**
-     * 数据处理
-     *
-     * @param list
-     * @param c
-     * @param <T>
-     * @return
-     */
-    public static <T> List<T> dealData(List<String[]> list, Class<T> c) {
-        List<T> res = Lists.newArrayList();
-        try {
-            // 标题
-            String[] titleRow = list.get(0);
-            Map<Integer, String> colMap = new LinkedMap();
-            for (int i = 0; i < titleRow.length; i++) {
-                if (StringUtil.isBlank(titleRow[i])) {
-                    break;
-                }
-                colMap.put(i, titleRow[i]);
-            }
-            // System.out.println(colMap);
-
-            // 字段映射
-            Field[] fields = c.getDeclaredFields();
-            // 属性和表格列对应
-            Map<String, Map<String, String>> classMap = new LinkedHashMap();
-            for (Field field : fields) {
-                if (field.isAnnotationPresent(Ex.class)) {
-                    Ex annotation = field.getAnnotation(Ex.class);
-                    Map<String, String> propertyMap = new LinkedHashMap<>();
-                    propertyMap.put("field", field.getName());
-                    propertyMap.put("convertType", field.getGenericType().toString());
-                    classMap.put(annotation.name(), propertyMap);
-                }
-                field.getClass();
-            }
-            // System.out.println(classMap);
-
-            // 有效数据读取
-            int rowNum = 1;
-            for (String[] row : list) {
-                if (rowNum++ == 1) {
-                    continue;
-                }
-                T obj = c.newInstance();
-                for (int i = 0; i < row.length; i++) {
-                    if (classMap.get(colMap.get(i)) == null) {
-                        continue;
-                    }
-                    String property = classMap.get(colMap.get(i)).get("field");
-                    String convertType = classMap.get(colMap.get(i)).get("convertType");
-                    // System.out.println(property + ":" + row[i]);
-                    if (StringUtil.isNotBlank(property)) {
-                        Object value = convertValue(row[i], convertType);
-                        if (value != null) {
-                            ReflectUtil.setFieldValue(obj, property, value);
-                        }
-                    }
-                }
-                res.add(obj);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return res;
-    }
-
-    /**
-     * 根据反射转成不同的类型
-     *
-     * @param object
-     * @param convertType
-     * @return
-     */
-    public static Object convertValue(String object, String convertType) {
-        Object res = null;
-        if (object == null) {
-            return null;
-        }
-        try {
-            switch (convertType) {
-                case "class java.lang.Integer":
-                case "int":
-                    res = Integer.parseInt(object);
-                    break;
-                case "class java.lang.Long":
-                case "long":
-                    res = Long.parseLong(object);
-                    break;
-                case "class java.lang.Float":
-                case "float":
-                    res = Float.parseFloat(object);
-                    break;
-                case "class java.lang.Double":
-                case "double":
-                    res = Double.parseDouble(object);
-                    break;
-                case "class java.lang.Boolean":
-                case "boolean":
-                    res = Boolean.parseBoolean(object);
-                    break;
-                case "class java.lang.String":
-                    res = object;
-                    break;
-                default:
-                    res = object;
-                    break;
-            }
-        } catch (Exception e) {
-            // e.printStackTrace();
-            return null;
-        }
-        return res;
-    }
-
-    public static void main(String[] args) {
-        String path = "D:\\newSVN\\2020新版CDSS\\05.其他资料\\数据\\其他数据\\【注册药品导入模板】 - 副本.xlsx";
-        List<KlDrugRegisterTestVO> klDrugRegisterTestVOList = readData(path, null, 0, KlDrugRegisterTestVO.class);
-        System.out.println(klDrugRegisterTestVOList);
-    }
-}
-