ai中excel的解析
This commit is contained in:
parent
46d68f8d4b
commit
862eb216e5
|
|
@ -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;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<PageFile> parseXlsxByPage(String filePath) {
|
||||
|
||||
return null;
|
||||
public static List<PageFile> parseXlsxByPage(String filePath) throws IOException {
|
||||
List<PageFile> 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<PageFile> parseXlsByPage(String filePath) {
|
||||
|
||||
return null;
|
||||
public static List<PageFile> parseXlsByPage(String filePath) throws IOException {
|
||||
List<PageFile> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue