diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/pojo/PageFile.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/pojo/PageFile.java index b980fc7..9b0953c 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/pojo/PageFile.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/pojo/PageFile.java @@ -1,8 +1,12 @@ package com.electromagnetic.industry.software.common.pojo; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.NoArgsConstructor; @Data +@NoArgsConstructor +@AllArgsConstructor public class PageFile { private int pageNumber; @@ -10,4 +14,6 @@ public class PageFile { private String content; private String fileName; + + } diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java index 22537a4..8d897b1 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/OfficeFileUtil.java @@ -31,6 +31,7 @@ import java.io.InputStream; import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; import java.util.Objects; @@ -229,14 +230,52 @@ public class OfficeFileUtil { return EleCommonUtil.formateString(stringBuilder.toString()); } - public static List parseXlsxByPage(String filePath) { - - return null; + public static List parseXlsxByPage(String filePath) throws IOException { + List pageFiles = new ArrayList<>(); + XSSFWorkbook excel = new XSSFWorkbook(FileUtil.getInputStream(filePath)); + int activeSheetIndex = excel.getNumberOfSheets(); + String fileName = new File(filePath).getName(); + for (int i = 0; i < activeSheetIndex; i++) { + XSSFSheet sheet = excel.getSheetAt(i); + int allRows = sheet.getPhysicalNumberOfRows(); + StringBuilder content = new StringBuilder(); + for (int j = 0; j < allRows; j++) { + XSSFRow row = sheet.getRow(j); + if (Objects.isNull(row)) { + continue; + } + for (Cell cell : row) { + content.append(getCellValue(cell)).append("\n"); + } + } + pageFiles.add(new PageFile(i, content.toString(), fileName)); + } + excel.close(); + return pageFiles; } - public static List parseXlsByPage(String filePath) { - - return null; + public static List parseXlsByPage(String filePath) throws IOException { + List pageFiles = new ArrayList<>(); + Workbook sheets = WorkbookFactory.create(FileUtil.getInputStream(filePath)); + String fileName = new File(filePath).getName(); + int numberOfSheets = sheets.getNumberOfSheets(); + for (int i = 0; i < numberOfSheets; i++) { + Sheet sheet = sheets.getSheetAt(i); + int allRows = sheet.getPhysicalNumberOfRows(); + StringBuilder content = new StringBuilder(); + for (int j = 0; j < allRows; j++) { + Row row = sheet.getRow(j); + if (Objects.isNull(row)) { + continue; + } + for (Cell cell : row) { + content.append(getCellValue(cell)).append("\n"); + } + } + pageFiles.add(new PageFile(i, content.toString(), fileName)); + } + sheets.close(); + return pageFiles; } }