main
黄海 9 months ago
parent 221a56d380
commit 82ac723fa7

@ -5,9 +5,7 @@ import com.dsideal.base.Tools.Util.LocalMysqlConnectUtil;
import com.dsideal.base.Tools.Util.ReadDocxUtil; import com.dsideal.base.Tools.Util.ReadDocxUtil;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.*;
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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont; import org.apache.poi.xssf.usermodel.XSSFFont;
@ -34,9 +32,9 @@ public class C1 {
//目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死 //目标Excel,就是把文件名解析出来后,后面添加上【成果】,需要动态计算获取,不能写死
String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx"); String excelPath = sampleExcelPath.replace(".xlsx", "【成果】.xlsx");
//如果目录文件存在,则删除 //如果目录文件存在,则删除
File file = new File(excelPath); File fi = new File(excelPath);
if (file.exists()) { if (fi.exists()) {
file.delete(); fi.delete();
} }
//目标Excel //目标Excel
@ -47,11 +45,23 @@ public class C1 {
XSSFFont font = outWorkbook.createFont(); XSSFFont font = outWorkbook.createFont();
font.setBold(true); // 设置字体加粗 font.setBold(true); // 设置字体加粗
headerStyle.setFont(font); headerStyle.setFont(font);
// 设置边框样式
headerStyle.setBorderTop(BorderStyle.THIN);
headerStyle.setBorderBottom(BorderStyle.THIN);
headerStyle.setBorderLeft(BorderStyle.THIN);
headerStyle.setBorderRight(BorderStyle.THIN);
// 设置水平和垂直居中
headerStyle.setAlignment(HorizontalAlignment.CENTER);
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
XSSFCellStyle dataStyle = outWorkbook.createCellStyle(); XSSFCellStyle dataStyle = outWorkbook.createCellStyle();
dataStyle.setBorderTop(BorderStyle.THIN); // 上边框 dataStyle.setBorderTop(BorderStyle.THIN); // 上边框
dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框 dataStyle.setBorderBottom(BorderStyle.THIN); // 下边框
dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框 dataStyle.setBorderLeft(BorderStyle.THIN); // 左边框
dataStyle.setBorderRight(BorderStyle.THIN); // 右边框 dataStyle.setBorderRight(BorderStyle.THIN); // 右边框
// 设置水平和垂直居中
dataStyle.setAlignment(HorizontalAlignment.CENTER);
dataStyle.setVerticalAlignment(VerticalAlignment.CENTER);
//先把样例的第一行复制到目标输出Excel的第一行中去 //先把样例的第一行复制到目标输出Excel的第一行中去
Row firstRow = outSheet.createRow(0); Row firstRow = outSheet.createRow(0);
@ -61,85 +71,101 @@ public class C1 {
XSSFSheet sampleSheet = sampleWorkbook.getSheet("Sheet1"); XSSFSheet sampleSheet = sampleWorkbook.getSheet("Sheet1");
Row firstSampleRow = sampleSheet.getRow(0); Row firstSampleRow = sampleSheet.getRow(0);
for (Cell cell : firstSampleRow) { for (Cell cell : firstSampleRow) {
Cell outCell=firstRow.createCell(cell.getColumnIndex()); Cell outCell = firstRow.createCell(cell.getColumnIndex());
outCell.setCellValue(ExcelReader.readCell(cell)); outCell.setCellValue(ExcelReader.readCell(cell));
outCell.setCellStyle(headerStyle); // 应用加粗样式 outCell.setCellStyle(headerStyle); // 应用加粗样式
} }
sampleWorkbook.close(); 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);
//图表
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) {
//遍历row中每一列中间以tab隔开然后换行
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);
// //开始读取市州word文档 //总量数值
// String parentPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510"; columnIndex = 3;
// value = ExcelReader.readCell(row.getCell(4));
// //找到parentPath下一级目录中所有文件 outRow.createCell(columnIndex).setCellValue(value);
// File[] files = new File(parentPath).listFiles(); outRow.getCell(columnIndex).setCellStyle(dataStyle);
// //处理这个目录
// if (files != null) { //区域数值
// for (File file : files) { columnIndex = 4;
// //判断file是不是目录是目录的需要跳过 outRow.createCell(columnIndex).setCellValue("");
// if (file.isDirectory()) continue; outRow.getCell(columnIndex).setCellStyle(dataStyle);
// //城市名称
// String cityName = ru.getCityOrAreaName(file.getName()); //行政区划
// String fileName = file.getName(); columnIndex = 5;
// outRow.createCell(columnIndex).setCellValue(cityName);
// //判断是否为docx文件 outRow.getCell(columnIndex).setCellStyle(dataStyle);
// if (fileName.endsWith(".docx") && !fileName.startsWith("~")) { //上级行政区划
// //读取文件 columnIndex = 6;
// String inputUrl = file.getAbsolutePath(); outRow.createCell(columnIndex).setCellValue("云南省");
// InputStream is = new FileInputStream(inputUrl); outRow.getCell(columnIndex).setCellStyle(dataStyle);
// ZipSecureFile.setMinInflateRatio(-1.0d); }
// XWPFDocument doc = new XWPFDocument(is); workbook.close();
//
// //图表 break;
// List<XWPFChart> charts = doc.getCharts(); //XWPFChart chart28 = charts.get(27);
// }
// //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 //保存Excel
//设置行的高度=28列的宽度为自动 //设置行的高度=28列的宽度为自动
outSheet.setDefaultColumnWidth(20); outSheet.setDefaultColumnWidth(20);
outSheet.setDefaultRowHeight((short) 500); outSheet.setDefaultRowHeight((short) 500);
FileOutputStream fileOut = new FileOutputStream(excelPath); FileOutputStream fileOut = new FileOutputStream(excelPath);
outWorkbook.write(fileOut); outWorkbook.write(fileOut);
outWorkbook.close(); outWorkbook.close();

Loading…
Cancel
Save