main
黄海 9 months ago
parent 1d90a5fb65
commit 264860e683

@ -27,8 +27,12 @@ public class C1 {
//实例化
ReadDocxUtil ru = new ReadDocxUtil();
//开始读取市州word文档
String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
//示例Excel
String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\学前教育入园总量变化及预测.xlsx";
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
ExcelKit.delExcel(excelPath);
@ -36,26 +40,12 @@ public class C1 {
//目标Excel
XSSFWorkbook outWorkbook = new XSSFWorkbook();
//目标Sheet
XSSFSheet outSheet = outWorkbook.createSheet("Sheet1");
XSSFSheet outSheet = ExcelKit.CreateSheet(outWorkbook);
//应用样式
XSSFCellStyle headerStyle = ExcelKit.getHeaderStyle(outWorkbook);
XSSFCellStyle dataStyle = ExcelKit.getDataStyle(outWorkbook);
//先把样例的第一行复制到目标输出Excel的第一行中去
Row firstRow = outSheet.createRow(0);
//样例Excel
XSSFWorkbook sampleWorkbook = new XSSFWorkbook(sampleExcelPath);
//样例Sheet
XSSFSheet sampleSheet = sampleWorkbook.getSheet("Sheet1");
Row firstSampleRow = sampleSheet.getRow(0);
for (Cell cell : firstSampleRow) {
Cell outCell = firstRow.createCell(cell.getColumnIndex());
outCell.setCellValue(ExcelReader.readCell(cell));
outCell.setCellStyle(headerStyle); // 应用加粗样式
}
sampleWorkbook.close();
//开始读取市州word文档
String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
//拷贝文件头,第一行
ExcelKit.CopyHead(sampleExcelPath, outSheet, headerStyle);
//找到parentPath下一级目录中所有文件
File[] files = new File(parentPath).listFiles();
@ -80,141 +70,51 @@ public class C1 {
List<XWPFChart> charts = doc.getCharts();
//1号模板数据在图表5和28中
XWPFChart chart5 = charts.get(4);
XSSFWorkbook workbook = chart5.getWorkbook();
XSSFSheet sheet = workbook.getSheet("Sheet1");
//遍历输出sheet的内容
XWPFChart chart = charts.get(4);
XSSFWorkbook workbook = chart.getWorkbook();
// 要读取的数据源
List<List<String>> source1 = ExcelKit.readSheet(workbook);
chart = charts.get(27);
workbook = chart.getWorkbook();
// 要读取的数据源
List<List<String>> source2 = ExcelKit.readSheet(workbook);
//遍历source1
int rowIndex = 0;
Row outRow;
for (Row row : sheet) {
for (int i = 0; i < source1.size(); i++) {
List<String> r = source1.get(i);
rowIndex++;
if (rowIndex == 1) continue;
//年份
int columnIndex = 0;
String value = ExcelReader.readCell(row.getCell(columnIndex));
outRow = outSheet.createRow(rowIndex - 1);
outRow.createCell(columnIndex).setCellValue(value);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量分类
columnIndex = 1;
outRow.createCell(columnIndex).setCellValue("总入园数");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域分类
columnIndex = 2;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量数值
columnIndex = 3;
value = ExcelReader.readCell(row.getCell(4));
outRow.createCell(columnIndex).setCellValue(value);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域数值
columnIndex = 4;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//行政区划
columnIndex = 5;
outRow.createCell(columnIndex).setCellValue(cityName);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//上级行政区划
columnIndex = 6;
outRow.createCell(columnIndex).setCellValue("云南省");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
Row outRow = outSheet.createRow(rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("总入园数");//总量分类
outRow.createCell(2).setCellValue("");//区域分类
outRow.createCell(3).setCellValue(r.get(4));//总量数值
outRow.createCell(4).setCellValue("");//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
//应用一下样式
ExcelKit.applyStyle(outRow, 0, 6, dataStyle);
}
workbook.close();
//==============================================================================
//读取第28个图表
XWPFChart chart28 = charts.get(27);
workbook = chart28.getWorkbook();
sheet = workbook.getSheet("Sheet1");
String jiShu = "";
//遍历输出sheet的内容
int secondRowIndex = 0;
for (Row row : sheet) {
secondRowIndex++;
if (secondRowIndex == 1) continue;//表头都跳过
if (secondRowIndex == 2) {//2022年是重复的取一回就行了
jiShu = ExcelReader.readCell(row.getCell(4));//2022年基数
}
//遍历source2
for (int i = 0; i < source2.size(); i++) {
if (i == 0) continue;//跳过2022年这个年份重复了
List<String> r = source2.get(i);
rowIndex++;
//年份
int columnIndex = 0;
if (row.getCell(columnIndex) == null) break;
String value = ExcelReader.readCell(row.getCell(columnIndex));
outRow = outSheet.createRow(rowIndex - 1);
outRow.createCell(columnIndex).setCellValue(value);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量分类
columnIndex = 1;
outRow.createCell(columnIndex).setCellValue("总入园数");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域分类
columnIndex = 2;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量数值
columnIndex = 3;
value = ExcelReader.readCell(row.getCell(4));
outRow.createCell(columnIndex).setCellValue(value);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域数值
columnIndex = 4;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//行政区划
columnIndex = 5;
outRow.createCell(columnIndex).setCellValue(cityName);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//上级行政区划
columnIndex = 6;
outRow.createCell(columnIndex).setCellValue("云南省");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
Row outRow = outSheet.createRow(rowIndex);
// 需要写死列的对应关系
outRow.createCell(0).setCellValue(r.getFirst());//年份
outRow.createCell(1).setCellValue("总入园数");//总量分类
outRow.createCell(2).setCellValue("");//区域分类
outRow.createCell(3).setCellValue(r.get(4));//总量数值
outRow.createCell(4).setCellValue("");//区域数值
outRow.createCell(5).setCellValue(cityName);//行政区划
outRow.createCell(6).setCellValue("云南省");//上级行政区划
//应用一下样式
ExcelKit.applyStyle(outRow, 0, 6, dataStyle);
}
//重复读取,跳过表头
rowIndex--;
for (Row row : sheet) {
//年份
int columnIndex = 0;
if (row.getCell(columnIndex) == null) break;
String value = ExcelReader.readCell(row.getCell(columnIndex));
if (StrKit.isBlank(value)) continue;
rowIndex++;
outRow = outSheet.createRow(rowIndex - 1);
outRow.createCell(columnIndex).setCellValue(value);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量分类
columnIndex = 1;
outRow.createCell(columnIndex).setCellValue("2022年基数");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域分类
columnIndex = 2;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//总量数值
columnIndex = 3;
value = ExcelReader.readCell(row.getCell(4));
outRow.createCell(columnIndex).setCellValue(jiShu);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//区域数值
columnIndex = 4;
outRow.createCell(columnIndex).setCellValue("");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//行政区划
columnIndex = 5;
outRow.createCell(columnIndex).setCellValue(cityName);
outRow.getCell(columnIndex).setCellStyle(dataStyle);
//上级行政区划
columnIndex = 6;
outRow.createCell(columnIndex).setCellValue("云南省");
outRow.getCell(columnIndex).setCellStyle(dataStyle);
}
workbook.close();
//城区
@ -225,9 +125,6 @@ public class C1 {
}
}
}
//保存Excel
outSheet.setDefaultColumnWidth(20);
outSheet.setDefaultRowHeight((short) 500);
FileOutputStream fileOut = new FileOutputStream(excelPath);
outWorkbook.write(fileOut);
outWorkbook.close();

@ -1,13 +1,17 @@
package com.dsideal.base.Tools.FillData.ExcelKit;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import com.dsideal.base.DataEase.Model.ExcelReader;
import com.jfinal.kit.StrKit;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ExcelKit {
/**
@ -34,6 +38,7 @@ public class ExcelKit {
/**
*
*
* @param workbook 簿
* @return
*/
@ -51,13 +56,147 @@ public class ExcelKit {
/**
* Excel
*
* @param excelPath
*/
public static void delExcel(String excelPath){
public static void delExcel(String excelPath) {
//如果目录文件存在,则删除
File fi = new File(excelPath);
if (fi.exists()) {
fi.delete();
}
}
/**
* Excel
*
* @param sampleExcelPath
* @param outSheet
* @param headerStyle
* @throws IOException
*/
public static void CopyHead(String sampleExcelPath, XSSFSheet outSheet, XSSFCellStyle headerStyle) throws IOException {
//样例Excel
XSSFWorkbook sampleWorkbook = new XSSFWorkbook(sampleExcelPath);
//先把样例的第一行复制到目标输出Excel的第一行中去
Row firstRow = outSheet.createRow(0);
//样例Sheet
XSSFSheet sampleSheet = sampleWorkbook.getSheet("Sheet1");
Row firstSampleRow = sampleSheet.getRow(0);
for (Cell cell : firstSampleRow) {
Cell outCell = firstRow.createCell(cell.getColumnIndex());
outCell.setCellValue(ExcelReader.readCell(cell));
outCell.setCellStyle(headerStyle); // 应用加粗样式
}
sampleWorkbook.close();
}
/**
* Sheet
*
* @param outWorkbook
* @return
*/
public static XSSFSheet CreateSheet(XSSFWorkbook outWorkbook) {
XSSFSheet outSheet = outWorkbook.createSheet("Sheet1");
//保存Excel
outSheet.setDefaultColumnWidth(20);
outSheet.setDefaultRowHeight((short) 500);
return outSheet;
}
/**
* @param workbook
* @return
* @throws IOException
*/
public static List<List<String>> readSheet(XSSFWorkbook workbook) throws IOException {
XSSFSheet sheet = workbook.getSheet("Sheet1");
List<List<String>> array = new ArrayList<>();
//遍历输出sheet的内容
int rowIndex = 0;
// 遍历工作表中的所有行
for (Row row : sheet) {
rowIndex++;
if (rowIndex == 1) continue;//第一行为表头
if (row == null) break;
if (row.getCell(0) == null) break;
if (StrKit.isBlank(readCell(row.getCell(0)))){
break;
}
// 遍历行中的所有单元格
List<String> X = new ArrayList<>();
for (Cell cell : row) {
X.add(ExcelReader.readCell(cell));
}
array.add(X);
}
workbook.close();
return array;
}
/**
*
* @param cell
*/
public static String readCell(Cell cell) {
// 根据单元格的类型来读取值
switch (cell.getCellType()) {
case STRING:
// 字符串类型的单元格
return cell.getStringCellValue();
case NUMERIC:
// 数字类型的单元格
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
// 判断是否为整数
double value = cell.getNumericCellValue();
if (value == (int) value) {
// 整数
return String.valueOf((int) value);
} else {
// 小数,保留两位小数
value = cell.getNumericCellValue();
return String.format("%.2f", value);
}
}
case FORMULA:
// 公式类型的单元格
cell.setCellType(CellType.NUMERIC); // 将公式计算结果转为数字
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
return cell.getDateCellValue().toString();
} else {
double formulaValue = cell.getNumericCellValue();
if (formulaValue == (int) formulaValue) {
// 整数
return String.valueOf((int) formulaValue);
} else {
// 小数,保留两位小数
double value = cell.getNumericCellValue();
return String.format("%.2f", value);
}
}
// 可以添加其他类型的处理,如 BOOLEAN, ERROR 等
default:
// 其他类型的单元格
return cell.toString();
}
}
/**
*
*
* @param outRow
* @param start
* @param end
* @param dataStyle
*/
public static void applyStyle(Row outRow, int start, int end, XSSFCellStyle dataStyle) {
for (int i = start; i <= end; i++) {
outRow.getCell(i).setCellStyle(dataStyle);
}
}
}

@ -1,5 +1,6 @@
package com.dsideal.base.Tools;
import com.dsideal.base.Tools.FillData.ExcelKit.ExcelKit;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -8,55 +9,7 @@ import java.io.IOException;
public class Step5_ReaderExcel {
public static void readCell(Cell cell){
// 根据单元格的类型来读取值
switch (cell.getCellType()) {
case STRING:
// 字符串类型的单元格
String stringValue = cell.getStringCellValue();
System.out.print(stringValue + "\t");
break;
case NUMERIC:
// 数字类型的单元格
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
System.out.print(cell.getDateCellValue() + "\t");
} else {
// 判断是否为整数
double value = cell.getNumericCellValue();
if (value == (int) value) {
// 整数
System.out.print((int) value + "\t");
} else {
// 小数,保留两位小数
System.out.printf("%.2f\t", cell.getNumericCellValue());
}
}
break;
case FORMULA:
// 公式类型的单元格
cell.setCellType(CellType.NUMERIC); // 将公式计算结果转为数字
if (DateUtil.isCellDateFormatted(cell)) {
// 如果是日期类型的单元格
System.out.print(cell.getDateCellValue() + "\t");
} else {
double formulaValue = cell.getNumericCellValue();
if (formulaValue == (int) formulaValue) {
// 整数
System.out.print((int) formulaValue + "\t");
} else {
// 小数,保留两位小数
System.out.printf("%.2f\t", cell.getNumericCellValue());
}
}
break;
// 可以添加其他类型的处理,如 BOOLEAN, ERROR 等
default:
// 其他类型的单元格
System.out.print(cell + "\t");
break;
}
}
public static void main(String[] args) throws IOException {
String filePath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022\\县区研究报告\\楚雄州各县市报告10\\1.楚雄市人口变化趋势对基础教育的影响分析报告\\附件2 楚雄市教育发展规模数据收集表2024.06.13.xlsx";
//第一个表格的宽度=23
@ -80,7 +33,7 @@ public class Step5_ReaderExcel {
// 读取表格数据
for (int j = 0; j < firstTableWidth; j++) {
Cell cell = row.getCell(j);
readCell(cell);
ExcelKit.readCell(cell);
}
System.out.println();
}
@ -117,7 +70,7 @@ public class Step5_ReaderExcel {
// 计算公式结果
cell.setCellType(CellType.NUMERIC);
}
readCell(cell);
ExcelKit.readCell(cell);
}
System.out.println();
}

Loading…
Cancel
Save