|
|
|
@ -12,36 +12,45 @@ import org.docx4j.openpackaging.exceptions.Docx4JException;
|
|
|
|
|
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
public class TestSingle {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws IOException, InvalidFormatException, InterruptedException, Docx4JException {
|
|
|
|
|
String sourceWord = "D:\\dsWork\\YunNanDsBase\\Doc\\全省及州市县区人口与教育报告集20241023\\16个州市报告2022\\分析报告20240510\\昭通市人口变化及其对教育的影响20240416.docx";
|
|
|
|
|
InputStream is = new FileInputStream(sourceWord);
|
|
|
|
|
ZipSecureFile.setMinInflateRatio(-1.0d);
|
|
|
|
|
XWPFDocument doc = new XWPFDocument(is);
|
|
|
|
|
//收集图表
|
|
|
|
|
List<XWPFChart> chartList = new ArrayList<>();
|
|
|
|
|
int a = 0, b = 0;
|
|
|
|
|
Set<XWPFChart> chartSet = new HashSet<>();
|
|
|
|
|
for (IBodyElement element : doc.getBodyElements()) {
|
|
|
|
|
if (element instanceof XWPFChart) {
|
|
|
|
|
chartList.add((XWPFChart) element);
|
|
|
|
|
a++;
|
|
|
|
|
if (element instanceof XWPFChart chart) {
|
|
|
|
|
chartSet.add(chart);
|
|
|
|
|
}
|
|
|
|
|
if (element instanceof XWPFParagraph sourcePara) {
|
|
|
|
|
XWPFChart chart = sourcePara.getDocument().getCharts().getFirst();
|
|
|
|
|
b++;
|
|
|
|
|
if (chart != null && chart.getWorkbook() != null) chartList.add(chart);
|
|
|
|
|
if (element instanceof XWPFParagraph) {
|
|
|
|
|
for (XWPFRun run : ((XWPFParagraph) element).getRuns()) {
|
|
|
|
|
for (XWPFChart chart : run.getDocument().getCharts()) {
|
|
|
|
|
if (chart != null) {
|
|
|
|
|
chartSet.add(chart);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
System.out.println("a=" + a + " b=" + b);
|
|
|
|
|
System.out.println("图表个数=" + chartList.size());
|
|
|
|
|
System.out.println("图表个数=" + chartSet.size());
|
|
|
|
|
//排序后的图表
|
|
|
|
|
List<XWPFChart> chartList = new ArrayList<>(chartSet);
|
|
|
|
|
|
|
|
|
|
chartList = ExcelKit.getSortListForXWPFChart(chartList);
|
|
|
|
|
|
|
|
|
|
//遍历输出
|
|
|
|
|
for (XWPFChart chart : chartList) {
|
|
|
|
|
for (int i = 0; i < chartList.size(); i++) {
|
|
|
|
|
XWPFChart chart = chartList.get(i);
|
|
|
|
|
XSSFWorkbook workbook = chart.getWorkbook();
|
|
|
|
|
XSSFSheet sheet = workbook.getSheetAt(0);
|
|
|
|
|
System.out.println("序号=" + (i + 1) + ",sheet名称=" + sheet.getSheetName());
|
|
|
|
|
for (Row row : sheet) {
|
|
|
|
|
for (Cell cell : row) {
|
|
|
|
|
System.out.print(ExcelKit.readCell(cell) + "\t");
|
|
|
|
|