parent
0633abfc24
commit
263b423870
@ -0,0 +1,47 @@
|
||||
package com.dsideal.base.DataEase.Model;
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExcelReader {
|
||||
public List<ExcelRow> readXlsxFile(String filePath, List<String> cols) {
|
||||
List<ExcelRow> rows = new ArrayList<>();
|
||||
try (FileInputStream fis = new FileInputStream(filePath);
|
||||
Workbook workbook = new XSSFWorkbook(fis)) {
|
||||
|
||||
Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
|
||||
int rowNum = 0;
|
||||
for (Row row : sheet) {
|
||||
List<String> rowData = new ArrayList<>();
|
||||
for (Cell cell : row) {
|
||||
rowData.add(getCellValue(cell));
|
||||
}
|
||||
if (rowNum == 0) {
|
||||
// 如果文件中没有列名,可以在这里手动指定
|
||||
rows.add(new ExcelRow(cols)); // 根据您的实际列名进行修改
|
||||
rowNum++;
|
||||
continue;
|
||||
}
|
||||
rows.add(new ExcelRow(rowData));
|
||||
rowNum++;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private String getCellValue(Cell cell) {
|
||||
return switch (cell.getCellType()) {
|
||||
case STRING -> cell.getStringCellValue();
|
||||
case NUMERIC -> String.valueOf(cell.getNumericCellValue());
|
||||
case BOOLEAN -> String.valueOf(cell.getBooleanCellValue());
|
||||
case FORMULA -> cell.getCellFormula();
|
||||
default -> "";
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.dsideal.base.DataEase.Model;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
public class ExcelRow {
|
||||
private List<String> data;
|
||||
|
||||
public ExcelRow(List<String> data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue