main
黄海 9 months ago
parent 2b1b51a2f3
commit 97d25a3629

@ -0,0 +1,93 @@
package com.dsideal.base.Tools.FillData.City;
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.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class C1 {
public static void main(String[] args) throws IOException, InvalidFormatException {
//初始化数据库连接
LocalMysqlConnectUtil.Init();
//实例化
ReadDocxUtil ru = new ReadDocxUtil();
//目标Excel
String excelPath = "D:\\dsWork\\YunNanDsBase\\Doc\\待处理\\市\\【1】学前教育入园总量变化及预测\\【成果】学前教育入园总量变化及预测.xlsx";
//操作目标Excel
XSSFWorkbook outWorkbook = new XSSFWorkbook();
//目标Sheet
XSSFSheet outSheet = outWorkbook.createSheet("Sheet1");
//开始读取市州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;
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();
System.out.println("市州所有文件处理完成!");
}
}
Loading…
Cancel
Save