main
黄海 8 months ago
parent 01f7341e6f
commit e24e9f248b

@ -1,11 +1,16 @@
package com.dsideal.base.Tools.FillData.City;
import com.dsideal.base.DataEase.Model.ExcelReader;
import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
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 org.apache.poi.xwpf.usermodel.XWPFChart;
@ -24,67 +29,117 @@ public class C1 {
//实例化
ReadDocxUtil ru = new ReadDocxUtil();
//示例Excel
String sampleExcelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\学前教育入园总量变化及预测.xlsx";
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
//如果目录文件存在,则删除
File file = new File(excelPath);
if (file.exists()) {
file.delete();
}
//目标Excel
String excelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\【成果】学前教育入园总量变化及预测.xlsx";
//操作目标Excel
XSSFWorkbook outWorkbook = new XSSFWorkbook();
//目标Sheet
XSSFSheet outSheet = outWorkbook.createSheet("Sheet1");
XSSFCellStyle headerStyle = outWorkbook.createCellStyle();
XSSFFont font = outWorkbook.createFont();
font.setBold(true); // 设置字体加粗
headerStyle.setFont(font);
XSSFCellStyle dataStyle = outWorkbook.createCellStyle();
dataStyle.setBorderTop(BorderStyle.THIN); // 上边框
dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框
dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框
dataStyle.setBorderRight(BorderStyle.THIN); // 右边框
//先把样例的第一行复制到目标输出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";
//找到parentPath下一级目录中所有文件
File[] files = new File(parentPath).listFiles();
//处理这个目录
if (files != null) {
for (File file : files) {
//判断file是不是目录是目录的需要跳过
if (file.isDirectory()) continue;
//城市名称
String cityName = ru.getCityOrAreaName(file.getName());
String fileName = file.getName();
//判断是否为docx文件
if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
//读取文件
String inputUrl = file.getAbsolutePath();
InputStream is = new FileInputStream(inputUrl);
ZipSecureFile.setMinInflateRatio(-1.0d);
XWPFDocument doc = new XWPFDocument(is);
// //开始读取市州word文档
// String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
//
// //找到parentPath下一级目录中所有文件
// File[] files = new File(parentPath).listFiles();
// //处理这个目录
// if (files != null) {
// for (File file : files) {
// //判断file是不是目录是目录的需要跳过
// if (file.isDirectory()) continue;
// //城市名称
// String cityName = ru.getCityOrAreaName(file.getName());
// String fileName = file.getName();
//
// //判断是否为docx文件
// if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
// //读取文件
// String inputUrl = file.getAbsolutePath();
// InputStream is = new FileInputStream(inputUrl);
// ZipSecureFile.setMinInflateRatio(-1.0d);
// XWPFDocument doc = new XWPFDocument(is);
//
// //图表
// List<XWPFChart> charts = doc.getCharts();
//
// //1号模板数据在图表5和28中
// XWPFChart chart5 = charts.get(4);
// XSSFWorkbook workbook = chart5.getWorkbook();
// XSSFSheet sheet = workbook.getSheet("Sheet1");
// //遍历输出sheet的内容
// int rowIndex = 0;
// Row outRow;
// for (Row row : sheet) {
// rowIndex++;
// if (rowIndex == 1) {
// int columnIndex = 0;
// outRow = outSheet.createRow(rowIndex);
// //将表头也复制过去
// for (Cell cell : row) {
// outRow.createCell(columnIndex).setCellValue(ExcelReader.readCell(cell));
// columnIndex++;
// }
// continue;
// }
// //年份
// int columnIndex = 0;
// String value = ExcelReader.readCell(row.getCell(columnIndex));
// outRow = outSheet.createRow(rowIndex);
// outRow.createCell(columnIndex).setCellValue(value);
// //行政区划
// columnIndex = 6;
// outRow.createCell(columnIndex).setCellValue(cityName);
// //上级行政区划
// columnIndex = 7;
// outRow.createCell(columnIndex).setCellValue("云南省");
// }
// workbook.close();
//
// break;
// //XWPFChart chart28 = charts.get(27);
// }
// }
// }
//保存Excel
//设置行的高度=28列的宽度为自动
outSheet.setDefaultColumnWidth(20);
outSheet.setDefaultRowHeight((short) 500);
//图表
List<XWPFChart> charts = doc.getCharts();
//1号模板数据在图表5和28中
XWPFChart chart5 = charts.get(4);
XSSFWorkbook workbook = chart5.getWorkbook();
XSSFSheet sheet = workbook.getSheet("Sheet1");
//遍历输出sheet的内容
int rowIndex = 0;
for (Row row : sheet) {
rowIndex++;
if (rowIndex == 1) continue;//放过表头
//年份
int columnIndex = 0;
double value = row.getCell(columnIndex).getNumericCellValue();
Row outRow = outSheet.createRow(rowIndex);
outRow.createCell(columnIndex).setCellValue(value);
//行政区划
columnIndex = 6;
outRow.createCell(columnIndex).setCellValue(cityName);
//上级行政区划
columnIndex = 7;
outRow.createCell(columnIndex).setCellValue("云南省");
}
workbook.close();
break;
//XWPFChart chart28 = charts.get(27);
}
}
}
//保存Excel
FileOutputStream fileOut = new FileOutputStream(excelPath);
outWorkbook.write(fileOut);
outWorkbook.close();

Loading…
Cancel
Save