You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
2.9 KiB

9 months ago
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;
9 months ago
public class Step5_ReaderExcel {
9 months ago
public static void main(String[] args) throws IOException {
9 months ago
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\楚雄州各县市报告10\\1.楚雄市人口变化趋势对基础教育的影响分析报告\\附件2 楚雄市教育发展规模数据收集表2024.06.13.xlsx";
9 months ago
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis);
9 months ago
//遍历前4个工作表
for (int i = 0; i < 4; i++) {
Sheet sheet = workbook.getSheetAt(i);
System.out.println("Sheet Name: " + sheet.getSheetName());
9 months ago
9 months ago
// 创建FormulaEvaluator对象
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
int rowIndex;
for (rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) break;
if (row.getRowNum() == 0) continue; // 跳过标题行, 本项目中有标题行
// 读取表格数据
for (Cell cell : row) {
// 如果单元格是公式类型
if (cell.getCellType() == CellType.FORMULA) {
// 计算公式结果
cell.setCellType(CellType.NUMERIC);
cell.setCellValue(evaluator.evaluate(cell).getNumberValue());
}
// 打印单元格数据
System.out.print(cell + "\t");
}
System.out.println();
9 months ago
}
9 months ago
//之所以减1是因为是在判断为空时才停止也就是过了真实数据一行后所以要减1
System.out.println("第一个表格开始行索引=2,结束行索引=" + (rowIndex - 1));
//第二个表格
//从rowIndex开始向下面查找直到第一列中出现文字“自动计算招生数、在校生数”此行的再下一行就是真正的第二个表格的开始位置
for (; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null && row.getCell(0) != null && row.getCell(0).toString().contains("自动计算招生数、在校生数")) {
System.out.println("rowIndex=" + rowIndex);
break;
9 months ago
}
9 months ago
}
9 months ago
break;//先调试第一个表格
9 months ago
}
9 months ago
9 months ago
workbook.close();
fis.close();
9 months ago
}
}