Browse Source

fix:代码覆盖其他模块有改好的

ly 10 months ago
parent
commit
b6c329d7f7
1 changed files with 2 additions and 77 deletions
  1. 2 77
      src/main/java/cn/ezhizao/common/utils/poi/ExcelUtil.java

+ 2 - 77
src/main/java/cn/ezhizao/common/utils/poi/ExcelUtil.java

@@ -150,8 +150,6 @@ public class ExcelUtil<T>
      */
     public String[] excludeFields;
 
-    public String[] includeFields;
-
     public ExcelUtil(Class<T> clazz)
     {
         this.clazz = clazz;
@@ -170,10 +168,6 @@ public class ExcelUtil<T>
 
     public void init(List<T> list, String sheetName, String title, Type type)
     {
-        this.init(list, sheetName, title, type, new String[0]);
-    }
-
-    public void init(List<T> list, String sheetName, String title, Type type, String[] exportField) {
         if (list == null)
         {
             list = new ArrayList<T>();
@@ -182,8 +176,6 @@ public class ExcelUtil<T>
         this.sheetName = sheetName;
         this.type = type;
         this.title = title;
-        this.includeFields = exportField;
-
         createExcelField();
         createWorkbook();
         createTitle();
@@ -209,9 +201,7 @@ public class ExcelUtil<T>
             Cell titleCell = titleRow.createCell(0);
             titleCell.setCellStyle(styles.get("title"));
             titleCell.setCellValue(title);
-            if (titleLastCol != 0) {
-                sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
-            }
+            sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), titleRow.getRowNum(), titleLastCol));
         }
     }
 
@@ -502,13 +492,6 @@ public class ExcelUtil<T>
         this.init(list, sheetName, title, Type.EXPORT);
         exportExcel(response);
     }
-    public void exportExcel(HttpServletResponse response, List<T> list, String sheetName, String title, String [] columns)
-    {
-        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-        response.setCharacterEncoding("utf-8");
-        this.init(list, sheetName, title, Type.EXPORT, columns);
-        exportExcel(response);
-    }
 
     /**
      * 对list数据源将其里面的数据导入到excel表单
@@ -560,13 +543,6 @@ public class ExcelUtil<T>
         exportExcel(response);
     }
 
-    public void importTemplateExcel(HttpServletResponse response, String sheetName, String title, String []exportFields) {
-        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
-        response.setCharacterEncoding("utf-8");
-        this.init(null, sheetName, title, Type.IMPORT, exportFields);
-        exportExcel(response);
-    }
-
     /**
      * 对list数据源将其里面的数据导入到excel表单
      *
@@ -581,7 +557,6 @@ public class ExcelUtil<T>
         }
         catch (Exception e)
         {
-            e.printStackTrace();
             log.error("导出Excel异常{}", e.getMessage());
         }
         finally
@@ -676,7 +651,6 @@ public class ExcelUtil<T>
             row = sheet.createRow(rowNo);
             // 得到导出对象.
             T vo = (T) list.get(i);
-//            System.out.println("i: " + i + ", vo: " + vo.toString());
             Collection<?> subList = null;
             if (isSubList())
             {
@@ -692,10 +666,8 @@ public class ExcelUtil<T>
                 }
             }
             int column = 0;
-            int columnNum = 0;
             for (Object[] os : fields)
             {
-                columnNum++;
                 Field field = (Field) os[0];
                 Excel excel = (Excel) os[1];
                 if (Collection.class.isAssignableFrom(field.getType()) && StringUtils.isNotNull(subList))
@@ -717,7 +689,6 @@ public class ExcelUtil<T>
                                 subField.setAccessible(true);
                                 Excel attr = subField.getAnnotation(Excel.class);
                                 this.addCell(attr, row, (T) obj, subField, column + subIndex);
-
                             }
                             subIndex++;
                         }
@@ -728,9 +699,7 @@ public class ExcelUtil<T>
                 else
                 {
                     this.addCell(excel, row, vo, field, column++);
-
                 }
-                setSizeColumn(sheet,columnNum-1);
             }
         }
     }
@@ -875,53 +844,14 @@ public class ExcelUtil<T>
         {
             // 填充默认样式,防止合并单元格样式失效
             sheet.setDefaultColumnStyle(column, styles.get(StringUtils.format("data_{}_{}_{}", attr.align(), attr.color(), attr.backgroundColor())));
-
             if (attr.needMerge())
             {
                 sheet.addMergedRegion(new CellRangeAddress(rownum - 1, rownum, column, column));
-
             }
         }
         return cell;
     }
 
-    /**
-     * 自适应宽度(中文支持)
-     *
-     * @param sheet sheet
-     * @param size  因为for循环从0开始,size值为 列数-1
-     */
-    public  void setSizeColumn(Sheet sheet, int size) {
-        for (int columnNum = 0; columnNum <= size; columnNum++) {
-            int columnWidth = sheet.getColumnWidth(columnNum) / 256;
-            for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
-                Row currentRow;
-                //当前行未被使用过
-                if (sheet.getRow(rowNum) == null) {
-                    currentRow = sheet.createRow(rowNum);
-                } else {
-                    currentRow = sheet.getRow(rowNum);
-                }
-
-                if (currentRow.getCell(columnNum) != null) {
-                    Cell currentCell = currentRow.getCell(columnNum);
-                    if (currentCell.getCellType() == CellType.STRING) {
-                        int length = currentCell.getStringCellValue().getBytes().length;
-                        if (columnWidth < length) {
-                            columnWidth = length;
-                        }
-                    }
-                }
-            }
-            if(30 * 256 * 3 > columnWidth * 256){
-                sheet.setColumnWidth(columnNum, columnWidth * 256);
-            }else{
-                sheet.setColumnWidth(columnNum, 30 * 256 * 3);
-            }
-
-        }
-    }
-
     /**
      * 设置单元格信息
      *
@@ -1098,7 +1028,7 @@ public class ExcelUtil<T>
      * @param endCol 结束列
      */
     public void setPromptOrValidation(Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow,
-            int firstCol, int endCol)
+                                      int firstCol, int endCol)
     {
         DataValidationHelper helper = sheet.getDataValidationHelper();
         DataValidationConstraint constraint = textlist.length > 0 ? helper.createExplicitListConstraint(textlist) : helper.createCustomConstraint("DD1");
@@ -1426,9 +1356,6 @@ public class ExcelUtil<T>
     {
         this.fields = getFields();
         this.fields = this.fields.stream().sorted(Comparator.comparing(objects -> ((Excel) objects[1]).sort())).collect(Collectors.toList());
-        if (this.includeFields.length > 0) {
-            this.fields = this.fields.stream().filter(v -> Arrays.stream(this.includeFields).anyMatch(e -> e.equals(((Field)v[0]).getName()))).collect(Collectors.toList());
-        }
         this.maxHeight = getRowHeight();
     }
 
@@ -1761,7 +1688,6 @@ public class ExcelUtil<T>
         }
         return method;
     }
-
     //读取表头
     public List<String> readExcelHeaders(InputStream inputStream) throws IOException {
         List<String> headers = new ArrayList<>();
@@ -1776,7 +1702,6 @@ public class ExcelUtil<T>
             Cell cell = cellIterator.next();
             headers.add(cell.getStringCellValue());
         }
-
         return headers;
     }
 }