wangliang 4 gadi atpakaļ
vecāks
revīzija
60e60b6419

+ 28 - 23
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/excel/ExcelWriter.java

@@ -15,6 +15,7 @@ import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.util.Collection;
 import java.util.List;
+import java.util.Objects;
 
 public class ExcelWriter extends ExcelExport {
 
@@ -30,7 +31,7 @@ public class ExcelWriter extends ExcelExport {
 
     private XSSFCellStyle style2;
 
-    public ExcelWriter(){
+    public ExcelWriter() {
         workbook = new XSSFWorkbook();
     }
 
@@ -87,17 +88,19 @@ public class ExcelWriter extends ExcelExport {
     public void write(Collection<?> dataset, OutputStream out) throws Exception {
         List<ColumnSetting> columnSettings = this.createColumnSettings();
         int index = 0;
-        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);
-                String methodName = columnSettings.get(i).getGetMethodName();
-                Method method = this.getDataClass().getMethod(methodName, new Class[]{});
-                Object value = method.invoke(obj, new Object[]{});
-                cell.setCellValue(value == null ? "" : value.toString());
+        if (Objects.nonNull(dataset) && dataset.size() > 0) {
+            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);
+                    String methodName = columnSettings.get(i).getGetMethodName();
+                    Method method = this.getDataClass().getMethod(methodName, new Class[]{});
+                    Object value = method.invoke(obj, new Object[]{});
+                    cell.setCellValue(value == null ? "" : value.toString());
+                }
             }
         }
         workbook.write(out);
@@ -117,17 +120,19 @@ public class ExcelWriter extends ExcelExport {
     public void write(Collection<?> dataset, Sheet sheet) throws Exception {
         List<ColumnSetting> columnSettings = this.createColumnSettings(sheet);
         int index = 0;
-        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);
-                String methodName = columnSettings.get(i).getGetMethodName();
-                Method method = this.getDataClass().getMethod(methodName, new Class[]{});
-                Object value = method.invoke(obj, new Object[]{});
-                cell.setCellValue(value == null ? "" : value.toString());
+        if (Objects.nonNull(dataset) && dataset.size() > 0) {
+            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);
+                    String methodName = columnSettings.get(i).getGetMethodName();
+                    Method method = this.getDataClass().getMethod(methodName, new Class[]{});
+                    Object value = method.invoke(obj, new Object[]{});
+                    cell.setCellValue(value == null ? "" : value.toString());
+                }
             }
         }
 //        workbook.write(out);