diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/parse/ExcelParse.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/parse/ExcelParse.java index d4cc30b..8601ae1 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/parse/ExcelParse.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/parse/ExcelParse.java @@ -1,11 +1,27 @@ package com.electromagnetic.industry.software.common.parse; +import cn.hutool.core.io.FileUtil; +import com.electromagnetic.industry.software.common.util.OfficeFileUtil; +import lombok.extern.slf4j.Slf4j; + import java.io.InputStream; +@Slf4j public class ExcelParse extends FileParse { @Override public String parseContent(InputStream stream, String fileType) { + String fileTmpPath = createFileTmpPath(fileType); + String res = ""; + try { + FileUtil.writeFromStream(stream, fileTmpPath); + if (fileType.endsWith("xlsx")) { + return OfficeFileUtil.parseXlsx(fileTmpPath); + } + return OfficeFileUtil.parseXls(fileTmpPath); + } catch (Exception e) { + log.error("解析{}格式的excel错误,具体为{}",fileType, e.getMessage(), e); + } return ""; } } 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 415d5a9..03c99f6 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 @@ -1,5 +1,6 @@ package com.electromagnetic.industry.software.common.util; +import cn.hutool.core.io.FileUtil; import cn.hutool.core.lang.Assert; import com.documents4j.api.DocumentType; import com.documents4j.api.IConverter; @@ -9,6 +10,10 @@ import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.hwpf.extractor.WordExtractor; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; @@ -16,6 +21,7 @@ import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; +import java.util.Objects; @Slf4j public class OfficeFileUtil { @@ -81,4 +87,66 @@ public class OfficeFileUtil { fis.close(); return stringBuilder.toString(); } + + public static String parseXlsx(String path) throws IOException { + XSSFWorkbook excel = new XSSFWorkbook(FileUtil.getInputStream(path)); + int activeSheetIndex = excel.getNumberOfSheets(); + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < activeSheetIndex; i++) { + XSSFSheet sheet = excel.getSheetAt(i); + int allRows = sheet.getPhysicalNumberOfRows(); + for (int j = 0; j < allRows; j++) { + XSSFRow row = sheet.getRow(j); + if (Objects.isNull(row)) { + continue; + } + for (Cell cell : row) { + stringBuilder.append(getCellValue(cell)).append("\n"); + } + } + } + return stringBuilder.toString(); + } + + private static String getCellValue(Cell cell) { + if (Objects.isNull(cell)) { + return ""; + } + + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue(); + case NUMERIC: + return String.valueOf(cell.getNumericCellValue()); + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case ERROR: + return String.valueOf(cell.getErrorCellValue()); + case FORMULA: + return String.valueOf(cell.getCellFormula()); + default: + return ""; + } + } + + public static String parseXls(String path) throws IOException { + InputStream inputStream = FileUtil.getInputStream(path); + Workbook sheets = WorkbookFactory.create(inputStream); + int numberOfSheets = sheets.getNumberOfSheets(); + StringBuilder stringBuilder = new StringBuilder(); + for (int i = 0; i < numberOfSheets; i++) { + Sheet sheet = sheets.getSheetAt(i); + int allRows = sheet.getPhysicalNumberOfRows(); + for (int j = 0; j < allRows; j++) { + Row row = sheet.getRow(j); + if (Objects.isNull(row)) { + continue; + } + for (Cell cell : row) { + stringBuilder.append(getCellValue(cell)).append("\n"); + } + } + } + return stringBuilder.toString(); + } }