You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
4.4 KiB

9 months ago
package com.dsideal.base.Tools;
9 months ago
9 months ago
import cn.hutool.core.io.FileUtil;
9 months ago
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
9 months ago
import org.apache.poi.openxml4j.util.ZipSecureFile;
9 months ago
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.xssf.usermodel.*;
9 months ago
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
9 months ago
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
9 months ago
import java.util.List;
9 months ago
public class ReadCityDoc {
9 months ago
//https://blog.csdn.net/a346736962/article/details/123037797
public static void main(String[] args) throws IOException, InvalidFormatException {
9 months ago
String directoryPath = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510";
directoryPath="D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\133个县区报告2022";
//遍历workingPath下的所有文件,注意,需要递归所有子目录下的所有文件
System.out.println("开始遍历目录下的所有文件");
// 调用 Hutool 的 FileUtil.loopFiles 方法递归获取所有文件
List<File> files = FileUtil.loopFiles(directoryPath, file -> {
// 这里可以添加你的过滤条件,如果不需要过滤,返回 true 即可
return true;
});
int cnt = 0;
// 打印所有文件的路径
for (File file : files) {
String fileName = file.getName();
9 months ago
//判断是否为docx文件
if (fileName.endsWith(".docx") && !fileName.startsWith("~")) {
9 months ago
cnt++;
9 months ago
//读取文件
9 months ago
String inputUrl = file.getAbsolutePath();
9 months ago
InputStream is = new FileInputStream(inputUrl);
9 months ago
//读取excel报错 Zip bomb detected! The file would exceed the max.
//https://blog.csdn.net/baidu_19473529/article/details/109601558
9 months ago
ZipSecureFile.setMinInflateRatio(-1.0d);
9 months ago
XWPFDocument doc = new XWPFDocument(is);
//图表
List<XWPFChart> charts = doc.getCharts();
9 months ago
9 months ago
System.out.println("图表数量=" + charts.size());
9 months ago
9 months ago
for (XWPFChart chart : charts) {
XSSFWorkbook workbook = chart.getWorkbook();
XSSFSheet sheet = workbook.getSheetAt(0);
//遍历一下sheet
for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
//遍历行
XSSFRow row = sheet.getRow(i);
9 months ago
if (row == null) continue;
9 months ago
for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
//遍历列
XSSFCell cell = row.getCell(j);
9 months ago
// 创建公式计算器
FormulaEvaluator evaluator = new XSSFFormulaEvaluator(workbook);
// 检查单元格是否包含公式
if (cell!=null && cell.getCellType() == CellType.FORMULA) {
// 计算公式并获取结果
CellValue evaluatedValue = evaluator.evaluate(cell);
System.out.println("Calculated Value: " + evaluatedValue.formatAsString());
}
9 months ago
if (cell != null)
System.out.print(cell + " ");
9 months ago
else {
9 months ago
System.out.print("null ");
}
}
System.out.println();
}
}
//段落
List<XWPFParagraph> paragraphs = doc.getParagraphs();
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
9 months ago
if (text.startsWith("图")) {
9 months ago
System.out.println(text);
9 months ago
}
9 months ago
9 months ago
}
}
}
9 months ago
System.out.println("共读取" + cnt + "个文件");
9 months ago
}
}