Przeglądaj źródła

优化Excel导入增加空行判断

RuoYi 4 lat temu
rodzic
commit
819ad04e3e

+ 65 - 48
ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/poi/ExcelUtil.java

@@ -36,6 +36,7 @@ import org.apache.poi.ss.usermodel.VerticalAlignment;
36 36
 import org.apache.poi.ss.usermodel.Workbook;
37 37
 import org.apache.poi.ss.usermodel.WorkbookFactory;
38 38
 import org.apache.poi.ss.util.CellRangeAddressList;
39
+import org.apache.poi.util.IOUtils;
39 40
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
40 41
 import org.apache.poi.xssf.usermodel.XSSFClientAnchor;
41 42
 import org.apache.poi.xssf.usermodel.XSSFDataValidation;
@@ -179,7 +180,8 @@ public class ExcelUtil<T>
179 180
             throw new IOException("文件sheet不存在");
180 181
         }
181 182
 
182
-        int rows = sheet.getPhysicalNumberOfRows();
183
+        // 获取最后一个非空行的行下标,比如总行数为n,则返回的为n-1
184
+        int rows = sheet.getLastRowNum();
183 185
 
184 186
         if (rows > 0)
185 187
         {
@@ -219,10 +221,15 @@ public class ExcelUtil<T>
219 221
                     }
220 222
                 }
221 223
             }
222
-            for (int i = 1; i < rows; i++)
224
+            for (int i = 1; i <= rows; i++)
223 225
             {
224 226
                 // 从第2行开始取数据,默认第一行是表头.
225 227
                 Row row = sheet.getRow(i);
228
+                // 判断当前行是否是空行
229
+                if (isRowEmpty(row))
230
+                {
231
+                    continue;
232
+                }
226 233
                 T entity = null;
227 234
                 for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet())
228 235
                 {
@@ -321,7 +328,7 @@ public class ExcelUtil<T>
321 328
      */
322 329
     public void exportExcel(HttpServletResponse response, List<T> list, String sheetName) throws IOException
323 330
     {
324
-        response.setContentType("application/vnd.ms-excel");
331
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
325 332
         response.setCharacterEncoding("utf-8");
326 333
         this.init(list, sheetName, Type.EXPORT);
327 334
         exportExcel(response.getOutputStream());
@@ -335,7 +342,7 @@ public class ExcelUtil<T>
335 342
      */
336 343
     public void importTemplateExcel(HttpServletResponse response, String sheetName) throws IOException
337 344
     {
338
-        response.setContentType("application/vnd.ms-excel");
345
+        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
339 346
         response.setCharacterEncoding("utf-8");
340 347
         this.init(null, sheetName, Type.IMPORT);
341 348
         exportExcel(response.getOutputStream());
@@ -346,32 +353,12 @@ public class ExcelUtil<T>
346 353
      * 
347 354
      * @return 结果
348 355
      */
349
-    public void exportExcel(OutputStream outputStream)
356
+    public void exportExcel(OutputStream out)
350 357
     {
351 358
         try
352 359
         {
353
-            // 取出一共有多少个sheet.
354
-            double sheetNo = Math.ceil(list.size() / sheetSize);
355
-            for (int index = 0; index <= sheetNo; index++)
356
-            {
357
-                createSheet(sheetNo, index);
358
-
359
-                // 产生一行
360
-                Row row = sheet.createRow(0);
361
-                int column = 0;
362
-                // 写入各个字段的列头名称
363
-                for (Object[] os : fields)
364
-                {
365
-                    Excel excel = (Excel) os[1];
366
-                    this.createCell(excel, row, column++);
367
-                }
368
-                if (Type.EXPORT.equals(type))
369
-                {
370
-                    fillExcelData(index, row);
371
-                    addStatisticsRow();
372
-                }
373
-            }
374
-            wb.write(outputStream);
360
+            writeSheet();
361
+            wb.write(out);
375 362
         }
376 363
         catch (Exception e)
377 364
         {
@@ -379,27 +366,35 @@ public class ExcelUtil<T>
379 366
         }
380 367
         finally
381 368
         {
382
-            if (wb != null)
369
+            IOUtils.closeQuietly(wb);
370
+            IOUtils.closeQuietly(out);
371
+        }
372
+    }
373
+
374
+    /**
375
+     * 创建写入数据到Sheet
376
+     */
377
+    public void writeSheet()
378
+    {
379
+        // 取出一共有多少个sheet.
380
+        double sheetNo = Math.ceil(list.size() / sheetSize);
381
+        for (int index = 0; index <= sheetNo; index++)
382
+        {
383
+            createSheet(sheetNo, index);
384
+
385
+            // 产生一行
386
+            Row row = sheet.createRow(0);
387
+            int column = 0;
388
+            // 写入各个字段的列头名称
389
+            for (Object[] os : fields)
383 390
             {
384
-                try
385
-                {
386
-                    wb.close();
387
-                }
388
-                catch (IOException e1)
389
-                {
390
-                    e1.printStackTrace();
391
-                }
391
+                Excel excel = (Excel) os[1];
392
+                this.createCell(excel, row, column++);
392 393
             }
393
-            if (outputStream != null)
394
+            if (Type.EXPORT.equals(type))
394 395
             {
395
-                try
396
-                {
397
-                    outputStream.close();
398
-                }
399
-                catch (IOException e1)
400
-                {
401
-                    e1.printStackTrace();
402
-                }
396
+                fillExcelData(index, row);
397
+                addStatisticsRow();
403 398
             }
404 399
         }
405 400
     }
@@ -535,8 +530,7 @@ public class ExcelUtil<T>
535 530
         }
536 531
         else if (ColumnType.IMAGE == attr.cellType())
537 532
         {
538
-            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1),
539
-                    cell.getRow().getRowNum() + 1);
533
+            ClientAnchor anchor = new XSSFClientAnchor(0, 0, 0, 0, (short) cell.getColumnIndex(), cell.getRow().getRowNum(), (short) (cell.getColumnIndex() + 1), cell.getRow().getRowNum() + 1);
540 534
             String imagePath = Convert.toStr(value);
541 535
             if (StringUtils.isNotEmpty(imagePath))
542 536
             {
@@ -546,7 +540,7 @@ public class ExcelUtil<T>
546 540
             }
547 541
         }
548 542
     }
549
-    
543
+
550 544
     /**
551 545
      * 获取画布
552 546
      */
@@ -1028,4 +1022,27 @@ public class ExcelUtil<T>
1028 1022
         }
1029 1023
         return val;
1030 1024
     }
1025
+
1026
+    /**
1027
+     * 判断是否是空行
1028
+     * 
1029
+     * @param row 判断的行
1030
+     * @return
1031
+     */
1032
+    private boolean isRowEmpty(Row row)
1033
+    {
1034
+        if (row == null)
1035
+        {
1036
+            return true;
1037
+        }
1038
+        for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++)
1039
+        {
1040
+            Cell cell = row.getCell(i);
1041
+            if (cell != null && cell.getCellType() != CellType.BLANK)
1042
+            {
1043
+                return false;
1044
+            }
1045
+        }
1046
+        return true;
1047
+    }
1031 1048
 }