main
kgdxpr 8 months ago
commit 128ef59928

@ -64,34 +64,10 @@ public class A1 {
System.out.println("正在进行" + cityName + "的数据填充~");
//如果源文件或者示例文件是xls格式则转化为xlsx格式
String sourceExcel = file.getAbsolutePath();
sourceExcel = ExcelKit.ConvertXlsToXlsx(sourceExcel);
//它的最后一个有效数据行,就是这个城市的整体数据汇总
List<List<String>> sheetList = ExcelKit.readSheet(sourceExcel, 4);//此表格有4行表头
//最后一行的数据就是整个市州的数据
List<String> rowData = sheetList.getLast();
List<List<String>> dataList=ExcelKit.readSecondTable(sourceExcel, 0, "自动计算招生数、在校生数",
2, "K");
try {
Row outRow = outSheet.createRow(++rowIndex);
Double allCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("B"))) / 10000;
Double chengZhenCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("D"))) / 10000;
Double xiangChunCount = Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("E"))) / 10000;
Double lv =
Double.parseDouble(rowData.get(ExcelKit.transLetter2Num("D"))) /
Integer.parseInt(rowData.get(ExcelKit.transLetter2Num("B")));
//需要保留两位小数
lv = Double.parseDouble(String.format("%.2f", lv));
ExcelKit.putData(outRow, Arrays.asList(cityName, "总人口",Double.parseDouble(String.format("%.2f", allCount))+"" , "万人", "云南省"), dataStyle);
outRow = outSheet.createRow(++rowIndex);
ExcelKit.putData(outRow, Arrays.asList(cityName, "城镇人口",Double.parseDouble(String.format("%.2f", chengZhenCount))+"", "万人", "云南省"), dataStyle);
outRow = outSheet.createRow(++rowIndex);
ExcelKit.putData(outRow, Arrays.asList(cityName, "乡村人口", Double.parseDouble(String.format("%.2f", xiangChunCount))+"", "万人", "云南省"), dataStyle);
outRow = outSheet.createRow(++rowIndex);
ExcelKit.putData(outRow, Arrays.asList(cityName, "城镇化率", lv + "%", "", "云南省"), dataStyle);
} catch (Exception err) {
System.out.println("错误数据:");
System.out.println(rowData);
System.exit(0);
}
System.out.println(dataList);
}
}
//保存文件

@ -387,6 +387,7 @@ public class ExcelKit {
/**
* excel
*
* @param excelFilePath
* @param skipRows
* @return
@ -482,10 +483,84 @@ public class ExcelKit {
data = ExcelKit.readExcelToList(ExcelKit.excelPath, skipRows);
System.out.println("二次获取数据条目数量:" + data.size() + ",期望数量=" + expectLimit);
}else{
} else {
System.out.println("期望数量>=" + expectLimit + ",现在有" + totalRow + "条理解为数据读取正确直接返回POI获取的数据列表...");
}
System.out.println("==========================================================================================================");
return data;
}
/**
* Sheet2
*
* @param filePath
* @param sheetIndex Sheet
* @param keyword
* @param headRows
* @param letterIndex K
* @return
* @throws IOException
*/
public static List<List<String>> readSecondTable(String filePath, int sheetIndex,
String keyword, int headRows, String letterIndex) throws IOException {
int tableWidth = ExcelKit.transLetter2Num(letterIndex) + 1;
return readSecondTable(filePath, sheetIndex, keyword, headRows, tableWidth);
}
/**
* Sheet2
*
* @param filePath
* @param sheetIndex Sheet
* @param keyword
* @param headRows
* @param tableWidth
* @return
* @throws IOException
*/
public static List<List<String>> readSecondTable(String filePath, int sheetIndex,
String keyword, int headRows, int tableWidth) throws IOException {
filePath = ConvertXlsToXlsx(filePath);
List<List<String>> dataList = new ArrayList<>();
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis);
Sheet sheet = workbook.getSheetAt(sheetIndex);
int rowIndex = 0;
//从rowIndex开始向下面查找直到第一列中出现文字“自动计算招生数、在校生数”此行的再下一行就是真正的第二个表格的开始位置
int start = -1, end = -1;
//找到开始行
for (; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row != null && row.getCell(0) != null
&& row.getCell(0).toString().contains(keyword)) {
if (start == -1)
start = rowIndex;
}
if (start > 0 && row == null) {
end = rowIndex - 1;
break;
}
}
start += headRows;//第二个表的表头有几行
System.out.println("第二个表格开始行索引=" + start + ",结束行索引=" + end);
//输出第二个表格的数据
for (rowIndex = start; rowIndex <= end; rowIndex++) {
Row row = sheet.getRow(rowIndex);
List<String> rowData = new ArrayList<>();
for (int j = 0; j < tableWidth; j++) {
Cell cell = row.getCell(j);
// 如果单元格是公式类型
if (cell.getCellType() == CellType.FORMULA) {
// 计算公式结果
cell.setCellType(CellType.NUMERIC);
}
rowData.add(ExcelKit.readCell(cell));
}
dataList.add(rowData);
}
workbook.close();
fis.close();
return dataList;
}
}

@ -11,11 +11,14 @@ public class Step5_ReaderExcel {
public static void main(String[] args) throws IOException {
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\楚雄州各县市报告10\\1.楚雄市人口变化趋势对基础教育的影响分析报告\\附件2 楚雄市教育发展规模数据收集表2024.06.13.xlsx";
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【1】学前幼儿入园总量预测\\鲁甸县教育发展规模数据收集表.xlsx";
//第一个表格的宽度=23
int firstTableWidth = 23;
int firstTableWidth = ExcelKit.transLetter2Num("W") + 1;
//第二个表格的宽度=11
int secondTableWidth = 11;
int secondTableWidth = ExcelKit.transLetter2Num("K") + 1;
System.out.println(firstTableWidth);
System.out.println(secondTableWidth);
FileInputStream fis = new FileInputStream(filePath);
Workbook workbook = new XSSFWorkbook(fis);
@ -23,17 +26,16 @@ public class Step5_ReaderExcel {
for (int i = 0; i < 4; i++) {
Sheet sheet = workbook.getSheetAt(i);
System.out.println("Sheet Name: " + sheet.getSheetName());
// 创建FormulaEvaluator对象
int skipRows = 2;//放过头两行
int rowIndex;
for (rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
for (rowIndex = skipRows; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
Row row = sheet.getRow(rowIndex);
if (row == null) break;
if (row.getRowNum() == 0) continue; // 跳过标题行, 本项目中有标题行
// 读取表格数据
for (int j = 0; j < firstTableWidth; j++) {
Cell cell = row.getCell(j);
ExcelKit.readCell(cell);
if (cell != null)
System.out.print(ExcelKit.readCell(cell)+"\t");
}
System.out.println();
}

@ -0,0 +1,35 @@
package com.dsideal.base.Tools.Test;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TestRead {
public static void main(String[] args) throws IOException {
//待读取的EXCEL文件
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\区\\【1】学前幼儿入园总量预测\\鲁甸县教育发展规模数据收集表.xlsx";
//表格正文上方的文字
String keyword = "自动计算招生数、在校生数";
//遍历每个Sheet
for (int k = 0; k < 4; k++) {
//为什么headRows=2?答因为有一行是关键字下一行是表头共2行
//letterIndex=K,表示第二个表格最后一列是字母K
List<List<String>> dataList = ExcelKit.readSecondTable(filePath, k, keyword, 2, "K");
//输出数据
for (int i = 0; i < dataList.size(); i++) {
for (int j = 0; j < dataList.get(i).size(); j++) {
System.out.print(dataList.get(i).get(j) + " ");
}
System.out.println();
}
}
}
}
Loading…
Cancel
Save