소스 검색

excel导出BUG修复

wangliang 2 년 전
부모
커밋
929efc0f2f
1개의 변경된 파일16개의 추가작업 그리고 7개의 파일을 삭제
  1. 16 7
      teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelWriter.java

+ 16 - 7
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelWriter.java

@@ -30,8 +30,6 @@ public class ExcelWriter extends ExcelExport {
 
     private XSSFCellStyle style;
 
-    private XSSFCellStyle style2;
-
     public ExcelWriter() {
         workbook = new XSSFWorkbook();
     }
@@ -58,7 +56,7 @@ public class ExcelWriter extends ExcelExport {
 
     private List<ColumnSetting> createColumnSettings() {
         List<ColumnSetting> columnSettings = this.getColumnSettings();
-        // 产生表格标题行
+        /* 产生表格标题行 */
         row = sheet.createRow(0);
         style = workbook.createCellStyle();
         ExcelHeaderStyle excelHeaderStyle = this.getDataClass().getAnnotation(ExcelHeaderStyle.class);
@@ -135,15 +133,20 @@ public class ExcelWriter extends ExcelExport {
         List<ColumnSetting> columnSettings = this.createColumnSettings();
         int index = 0;
         if (Objects.nonNull(dataset) && dataset.size() > 0) {
+            CellStyle style = workbook.createCellStyle();
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints((short) 10);
+            font.setFontName("宋体");
+            font.setColor(IndexedColors.BLACK1.getIndex());
+            style.setFont(font);
             for (Object obj : dataset) {
                 index++;
                 row = sheet.createRow(index);// 创建行
                 // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
                 for (short i = 0; i < columnSettings.size(); i++) {
                     cell = row.createCell(i);// 创建列
-                    Field field = columnSettings.get(i).getField();
-
-                    cell.setCellStyle(this.getCellStyle(workbook.createCellStyle(), field));
+//                    Field field = columnSettings.get(i).getField();
+                    cell.setCellStyle(style);
                     String methodName = columnSettings.get(i).getGetMethodName();
                     Method method = this.getDataClass().getMethod(methodName, new Class[]{});
                     Object value = method.invoke(obj, new Object[]{});
@@ -169,13 +172,19 @@ public class ExcelWriter extends ExcelExport {
         List<ColumnSetting> columnSettings = this.createColumnSettings(sheet);
         int index = 0;
         if (Objects.nonNull(dataset) && dataset.size() > 0) {
+            CellStyle style = workbook.createCellStyle();
+            Font font = workbook.createFont();
+            font.setFontHeightInPoints((short) 10);
+            font.setFontName("宋体");
+            font.setColor(IndexedColors.BLACK1.getIndex());
+            style.setFont(font);
             for (Object obj : dataset) {
                 index++;
                 row = sheet.createRow(index);// 创建行
                 // 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值
                 for (short i = 0; i < columnSettings.size(); i++) {
                     cell = row.createCell(i);// 创建列
-                    cell.setCellStyle(style2);
+                    cell.setCellStyle(style);
                     String methodName = columnSettings.get(i).getGetMethodName();
                     Method method = this.getDataClass().getMethod(methodName, new Class[]{});
                     Object value = method.invoke(obj, new Object[]{});