Browse Source

导入封装工具类

ZL 2 days ago
parent
commit
7d9bb9b2c1

+ 98 - 0
jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/util/ExcelImportHandleUtil.java

@@ -0,0 +1,98 @@
+package org.jeecg.modules.system.util;
+
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+public class ExcelImportHandleUtil {
+
+    /**
+     *
+     * @param file (上传的文件的对象)
+     * @param headerRowsNumber (表头有多少行)
+     * @param sheetIndex  (Sheet索引)
+     * @return inputStream (输入流)
+     * @throws IOException
+     */
+    public static InputStream fileStreamProcessing(MultipartFile file, int headerRowsNumber, int SheetIndex) throws IOException {
+        Workbook book = WorkbookFactory.create(file.getInputStream());
+        Sheet tempSheet = book.getSheetAt(SheetIndex);
+        int lastRowNum = tempSheet.getLastRowNum();
+
+        // 创建一个新的Workbook和Sheet
+        Workbook newBook = new XSSFWorkbook();
+        Sheet newSheet = newBook.createSheet("Filtered Data");
+
+        int newRowNum = 0; // 新Sheet的行号
+
+        // 复制前三行
+        for (int i = 0; i < headerRowsNumber; i++) {
+            Row row = tempSheet.getRow(i);
+            if (row != null) {
+                Row newRow = newSheet.createRow(newRowNum++);
+                for (int j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
+                    Cell cell = row.getCell(j);
+                    if (cell != null) {
+                        Cell newCell = newRow.createCell(j);
+                        copyCell(cell, newCell);
+                    }
+                }
+            }
+        }
+
+        for (int i = headerRowsNumber; i <= lastRowNum; i++) {
+            Row row = tempSheet.getRow(i);
+            if (row != null) {
+                short firstCellNum = row.getFirstCellNum();
+                if (firstCellNum != -1) {
+                    // 复制行到新的Sheet
+                    Row newRow = newSheet.createRow(newRowNum++);
+                    for (int j = firstCellNum; j <= row.getLastCellNum(); j++) {
+                        Cell cell = row.getCell(j);
+                        if (cell != null) {
+                            Cell newCell = newRow.createCell(j);
+                            copyCell(cell, newCell);
+                        }
+                    }
+                }
+            }
+        }
+        // 将新的Workbook写入ByteArrayOutputStream
+        ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
+        newBook.write(byteArrayOut);
+        newBook.close();
+        book.close();
+
+        // 获取InputStream
+        InputStream inputStream = new ByteArrayInputStream(byteArrayOut.toByteArray());
+        byteArrayOut.close();
+
+        return inputStream;
+    }
+
+    private static void copyCell(Cell oldCell, Cell newCell) {
+        switch (oldCell.getCellType()) {
+            case STRING:
+                newCell.setCellValue(oldCell.getStringCellValue());
+                break;
+            case NUMERIC:
+                newCell.setCellValue(oldCell.getNumericCellValue());
+                break;
+            case BOOLEAN:
+                newCell.setCellValue(oldCell.getBooleanCellValue());
+                break;
+            case FORMULA:
+                newCell.setCellFormula(oldCell.getCellFormula());
+                break;
+            default:
+                break;
+        }
+    }
+
+
+}