I tried reading property sets from Office 2007+ documents (docx, xlsx). Found an amazing solution at http://poi.apache.org/hpsf/how-to.html . There is an example for Office 2003 and early format (doc, xls, without "x").
public class ReadSummaryInformation {
public static void main(final String[] args) throws IOException {
final String filename = "C://file.docx";
POIFSReader r = new POIFSReader();
r.registerListener(new MyPOIFSReaderListener(),
"\005SummaryInformation");
r.read(new FileInputStream(filename));
}
static class MyPOIFSReaderListener implements POIFSReaderListener {
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
SummaryInformation si = null;
try {
si = (SummaryInformation)
PropertySetFactory.create(event.getStream());
}
catch (Exception ex){
throw new RuntimeException
("Property set stream \"" +
event.getPath() + event.getName() + "\": " + ex);
}
final String title = si.getTitle();
if (title != null)
System.out.println("Title: \"" + title + "\"");
else
System.out.println("Document has no title.");
}
}
}
I tried to open docx and xlsx (which means that I was trying to read "\ 005SummaryInformation" from the docs) using this code, and guess what? I got an exception:
Exception in thread "main" org.apache.poi.poifs.filesystem.OfficeXmlFileException:
The supplied data appears to be in the Office 2007+ XML. [b]You are calling the part
of POI that deals with OLE2 Office Documents.[/b] You need to call a different part of
POI to process this data (eg XSSF instead of HSSF)
Mister http://poi.apache.org/ says loudly and clearly that:
Office OpenXML Format - XML-, Microsoft Office 2007 2008. XLSX, DOCX PPTX. API Open Packaging , openxml4j.
poi api, , HPSF PropertySet, , , , XSSF . , .
: "\ 005SummaryInformation" Office 2007+ POI?
, api , Office 2007.
!
, :
try {
OPCPackage pkg = OPCPackage.open(new FileInputStream(new File("D:\\file.docx")));
POIXMLProperties props;
props = new POIXMLProperties(pkg);
System.out.println("The title is " + props.getCoreProperties().getTitle());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:154)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
at ReadSummaryInformation.main(ReadSummaryInformation.java:38)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 6 more
:
.;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;D:\kituri\Java\JDBC
driver\mysql-connector-java-5.1.22\mysql-connector-java-5.1.22-bin.jar;%JAVA_HOME%
\lib;%XMLBEANS_HOME%\lib\xbean.jar;D:\work\Workspace\document_archive01-2212
\src\RunClass.java;D:\work\Workspace\document_archive01-2212\poi-3.9\ooxml-
lib\dom4j-1.6.1.jar
:
C:\oraclexe\app\oracle\product\11.2.0\server\bin;;C:\Oracle11g\product\11.2.0\dbhome_1
\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%
\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE
\Core-Static;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\WIDCOMM
\Bluetooth Software\syswow64;C:\Program Files (x86)\QuickTime\QTSystem\;C:\Program
Files (x86)\Java\apache-maven-3.0.4\bin;C:\Program Files (x86)\Java\jdk1.7.0_07\bin;D:
\ChromeDriver;%XMLBEANS_HOME%\bin
- -3.9-20121203.jar
- xbean.jar
- -OOXML-3.9-20121203.jar
.
4 ( ), , , , , , , ( , .class ).