package com.junfun.pms.controller.excelImport;
import com.microsoft.schemas.vml.CTShape;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObject;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadWord {
public static void main(String[] args) {
try {
// 读取 Word 文档
FileInputStream fis = new FileInputStream("D:\\tu\\测试自己加图片.docx");
XWPFDocument document = new XWPFDocument(fis);
// 获取文档中的所有表格
List<XWPFTable> tables = document.getTables();
// 遍历每个表格
for (int tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
XWPFTable table = tables.get(tableIndex);
// 获取表格的行
List<XWPFTableRow> rows = table.getRows();
// 遍历每一行
for (int rowIndex = 0; rowIndex < rows.size(); rowIndex++) {
XWPFTableRow row = rows.get(rowIndex);
// 获取行中的单元格
List<XWPFTableCell> cells = row.getTableCells();
// 遍历每个单元格
for (int colIndex = 0; colIndex < cells.size(); colIndex++) {
XWPFTableCell cell = cells.get(colIndex);
String cellText = cell.getText();
// 输出单元格的文本内容
System.out.print(cellText + "\t");
// 获取单元格中的所有段落
for (XWPFParagraph paragraph : cell.getParagraphs()) {
//输出段落的文本内容
System.out.println("段落中的文本内容==="+paragraph.getText());
List<String> strings = ReadWord.readImageInParagraph(paragraph);
if (!strings.isEmpty()) {
System.out.println("段落中的图片索引==="+strings);
}
// 检查段落中的图片
for (XWPFRun run : paragraph.getRuns()) {
// 检查是否有图片
if (run.getEmbeddedPictures() != null) {
// 获取图片数据
List<XWPFPictureData> pictures = document.getAllPictures();
for (XWPFPictureData pictureData : pictures) {
// 获取图片数据
byte[] imageData = pictureData.getData();
String ext = pictureData.suggestFileExtension();
// 生成文件名,包含表格索引、行索引和列索引
String fileName = String.format("D:\\tu\\image_table%d_row%d_col%d_%s_%s.%s",
tableIndex + 1, rowIndex + 1, colIndex + 1,cellText,
System.currentTimeMillis(), ext);
// 将图片写入文件
try (FileOutputStream fos = new FileOutputStream(fileName)) {
fos.write(imageData);
System.out.println("图片已保存: " + fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
System.out.println(); // 换行
}
System.out.println(); // 表格间换行
}
// 关闭文件流
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//获取某一个段落中的所有图片索引
public static List<String> readImageInParagraph(XWPFParagraph paragraph) {
//图片索引List
List<String> imageBundleList = new ArrayList<String>();
//段落中所有XWPFRun
List<XWPFRun> runList = paragraph.getRuns();
for (XWPFRun run : runList) {
//XWPFRun是POI对xml元素解析后生成的自己的属性,无法通过xml解析,需要先转化成CTR
CTR ctr = run.getCTR();
//对子元素进行遍历
XmlCursor c = ctr.newCursor();
//这个就是拿到所有的子元素:
c.selectPath("./*");
while (c.toNextSelection()) {
XmlObject o = c.getObject();
//如果子元素是<w:drawing>这样的形式,使用CTDrawing保存图片
if (o instanceof CTDrawing) {
CTDrawing drawing = (CTDrawing) o;
CTInline[] ctInlines = drawing.getInlineArray();
for (CTInline ctInline : ctInlines) {
CTGraphicalObject graphic = ctInline.getGraphic();
//
XmlCursor cursor = graphic.getGraphicData().newCursor();
cursor.selectPath("./*");
while (cursor.toNextSelection()) {
XmlObject xmlObject = cursor.getObject();
if (xmlObject instanceof CTPicture) {
org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture picture = (org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture) xmlObject;
//拿到元素的属性
imageBundleList.add(picture.getBlipFill().getBlip().getEmbed());
}
}
}
}
//使用CTObject保存图片
if (o instanceof CTObject) {
CTObject object = (CTObject) o;
System.out.println(object);
XmlCursor w = object.newCursor();
w.selectPath("./*");
while (w.toNextSelection()) {
XmlObject xmlObject = w.getObject();
if (xmlObject instanceof CTShape) {
CTShape shape = (CTShape) xmlObject;
imageBundleList.add(shape.getImagedataArray()[0].getId2());
}
}
}
}
}
return imageBundleList;
}
}
java读取表格获取其段落内的图片保存在本地
最新推荐文章于 2024-08-30 18:40:43 发布