完成excel文件的解析

This commit is contained in:
chenxudong 2025-01-21 10:01:44 +08:00
parent 34708604de
commit 9603bff042
2 changed files with 84 additions and 0 deletions

View File

@ -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 "";
}
}

View File

@ -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();
}
}