|
|
|
@ -0,0 +1,69 @@
|
|
|
|
|
package com.dsideal.base.Tools;
|
|
|
|
|
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
|
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
|
|
public class ExcelReader {
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\楚雄州各县市报告10\\1.楚雄市人口变化趋势对基础教育的影响分析报告\\附件2 楚雄市教育发展规模数据收集表(2024.06.13).xlsx";
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(filePath);
|
|
|
|
|
Workbook workbook = new XSSFWorkbook(fis)) {
|
|
|
|
|
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(0); // 读取第一个工作表
|
|
|
|
|
Iterator<Row> rowIterator = sheet.iterator(); // 创建行迭代器
|
|
|
|
|
// 创建FormulaEvaluator对象
|
|
|
|
|
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
|
|
|
|
|
|
|
|
|
|
boolean tableStarted = false;
|
|
|
|
|
int rowCount = 0;
|
|
|
|
|
|
|
|
|
|
while (rowIterator.hasNext()) {
|
|
|
|
|
Row row = rowIterator.next();
|
|
|
|
|
if (row.getRowNum() == 0) continue; // 跳过标题行
|
|
|
|
|
|
|
|
|
|
// 检查是否开始读取表格数据
|
|
|
|
|
if (!tableStarted) {
|
|
|
|
|
// 这里你需要根据实际情况来设置开始读取的条件
|
|
|
|
|
// 例如,检查这一行是否包含特定标题或者特定数量的数据
|
|
|
|
|
if (row.getCell(0) != null && row.getCell(0).getStringCellValue().startsWith("年份")) {
|
|
|
|
|
tableStarted = true;
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tableStarted) {
|
|
|
|
|
// 读取表格数据
|
|
|
|
|
if (rowCount >= 0 && rowCount < 10) { // 假设我们只读取前10行数据
|
|
|
|
|
for (Cell cell : row) {
|
|
|
|
|
// 如果单元格是公式类型
|
|
|
|
|
if (cell.getCellType() == CellType.FORMULA) {
|
|
|
|
|
// 计算公式结果
|
|
|
|
|
cell.setCellType(CellType.NUMERIC);
|
|
|
|
|
cell.setCellValue(evaluator.evaluate(cell).getNumberValue());
|
|
|
|
|
}
|
|
|
|
|
// 打印单元格数据
|
|
|
|
|
System.out.print(cell.toString() + "\t");
|
|
|
|
|
}
|
|
|
|
|
System.out.println();
|
|
|
|
|
rowCount++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 检查是否读取到表格的末尾
|
|
|
|
|
// 这里你也需要根据实际情况来设置结束条件
|
|
|
|
|
String cellValue = new DataFormatter().formatCellValue(row.getCell(0));
|
|
|
|
|
if (cellValue.startsWith("年份")) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|