gpt4 book ai didi

java - 使用 POI 获取嵌入对象的行和列

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:27 29 4
gpt4 key购买 nike

我目前正在处理 Excel 文件 (*.xlsm) 和 Apache POI,并且我一直在绞尽脑汁地完成一项任务。我收到一些嵌入了 PDF 的 Excel 文件,我想提取它们并根据它们所在的行和列重命名它们。这看起来很奇怪,因为我知道嵌入的对象被表示为图像,它们可以占据多个单元格,并且从技术上讲它们并不“在”单元格中。

下面的代码片段让我提取嵌入的 PDF,但它们被命名为 OleObject[1..2..3.etc..],这并没有给我任何线索。

inStream = new FileInputStream(file);
XSSFWorkbook workbook = new XSSFWorkbook(inStream);
for (PackagePart pPart : workbook.getAllEmbedds()) {
String contentType = pPart.getContentType();
if (contentType.equals("application/vnd.openxmlformats-officedocument.oleObject")){
POIFSFileSystem fs = new POIFSFileSystem(pPart.getInputStream());
TikaInputStream stream = TikaInputStream.get(fs.createDocumentInputStream("CONTENTS"));

byte[] bytes = IOUtil.toByteArray(stream);
stream.close();
OutputStream outStream = new FileOutputStream(new File(ROOT_DIRECTORY.getAbsolutePath()+"\\PDF"+i+".pdf"));
IOUtil.copy(bytes, outStream);
outStream.close();
}}

我想知道 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet 是否能让我看到 Excel 工作表的 xml 代码,也许我可以获得我需要的信息。像这样。

<oleObjects><mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"><mc:Choice Requires="x14"><oleObject progId="Acrobat Document" dvAspect="DVASPECT_ICON" shapeId="1028" r:id="rId4"><objectPr defaultSize="0" r:id="rId5"><anchor moveWithCells="1"><from><xdr:col>8</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>11</xdr:row><xdr:rowOff>0</xdr:rowOff></from><to><xdr:col>8</xdr:col><xdr:colOff>1143000</xdr:colOff><xdr:row>13</xdr:row><xdr:rowOff>171450</xdr:rowOff></to></anchor></objectPr></oleObject></mc:Choice><mc:Fallback><oleObject progId="Acrobat Document" dvAspect="DVASPECT_ICON" shapeId="1028" r:id="rId4"/></mc:Fallback></mc:AlternateContent></oleObjects>

--

<objectPr defaultSize="0" r:id="rId5"><anchor moveWithCells="1"><from><xdr:col>8</xdr:col><xdr:colOff>0</xdr:colOff><xdr:row>11</xdr:row><xdr:rowOff>0</xdr:rowOff></from><to><xdr:col>8</xdr:col><xdr:colOff>1143000</xdr:colOff><xdr:row>13</xdr:row><xdr:rowOff>171450</xdr:rowOff></to></anchor></objectPr>

我想使用 anchor 信息是可能的,但我只是无法找到如何获取它。

希望这些信息能让我清楚地了解我想要做什么。

提前致谢。

最佳答案

我查看了当前 poi-ooxml-schemas 源 jar 的源代码,您可以在这里找到它:http://repo1.maven.org/maven2/org/apache/poi/ooxml-schemas/1.3/

org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet 扩展了 org.apache.xmlbeans.XmlObject,它可以使用继承的 .toString() 方法以字符串形式提供 XML。或者,您可以通过在 CTWorksheet 对象上调用 getOleObjects() 来快速访问工作表中的 OLE 对象列表。

/**
* Gets the "oleObjects" element
*/
org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObjects getOleObjects();

/**
* Gets a List of "oleObject" elements
*/
java.util.List<org.openxmlformats.schemas.spreadsheetml.x2006.main.CTOleObject> getOleObjectList();

CTOleObject 似乎没有 getter 方法来获取 XML 元素和子 XML 元素以允许您确定列,因此我认为您需要进行一些 XML 解析或字符串搜索来获取此信息(如果该信息包含在字符串 XML 表示形式中)。

希望这有帮助。

关于java - 使用 POI 获取嵌入对象的行和列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38214878/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com