【问题标题】:How to get file summary information with Java/Apache POI如何使用 Java/Apache POI 获取文件摘要信息
【发布时间】:2012-10-12 14:18:06
【问题描述】:

我正在尝试使用 JAVA 从文件中获取摘要信息,但我找不到任何东西。我试过 org.apache.poi.hpsf.* 。

我需要作者、主题、评论、关键字和标题。

       File rep = new File("C:\\Cry_ReportERP006.rpt");


        /* Read a test document <em>doc</em> into a POI filesystem. */
        final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(rep));
        final DirectoryEntry dir = poifs.getRoot();
        DocumentEntry dsiEntry = null;
        try
        {
            dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
        }
        catch (FileNotFoundException ex)
        {
            /*
             * A missing document summary information stream is not an error
             * and therefore silently ignored here.
             */
        }

        /*
         * If there is a document summry information stream, read it from
         * the POI filesystem.
         */
        if (dsiEntry != null)
        {
            final DocumentInputStream dis = new DocumentInputStream(dsiEntry);
            final PropertySet ps = new PropertySet(dis);
            final DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps);
            final SummaryInformation si = new SummaryInformation(ps);


            /* Execute the get... methods. */
            System.out.println(si.getAuthor());

【问题讨论】:

  • 你为什么不发布代码,即使它不工作它仍然有助于答案?
  • 你说的是 MS Office 文件,对吗?
  • 现在我发布代码。不是 MS Office 文件,我说的是一般文件。
  • 你的代码有什么问题?它看起来对我来说是正确的?

标签: java apache-poi summary


【解决方案1】:

正如http://poi.apache.org/overview.html 的 POI 概述中所述,文件解析器的类型更多。 以下示例从 2003 年的 office 文件中提取作者/创建者:

public static String parseOLE2FileAuthor(File file) {
String author=null;
try {

    FileInputStream stream = new FileInputStream(file);
    POIFSFileSystem poifs = new POIFSFileSystem(stream);
    DirectoryEntry dir = poifs.getRoot();
    DocumentEntry siEntry =      (DocumentEntry)dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
    DocumentInputStream dis = new DocumentInputStream(siEntry);
    PropertySet ps = new PropertySet(dis);
    SummaryInformation si = new SummaryInformation(ps);

    author=si.getAuthor();
    stream.close();

} catch (IOException ex) {
    ex.getStackTrace();
} catch (NoPropertySetStreamException ex) {
    ex.getStackTrace();
} catch (MarkUnsupportedException ex) {
    ex.getStackTrace();
} catch (UnexpectedPropertySetTypeException ex) {
    ex.getStackTrace();
}
return author;

}

对于 docx、pptx、xlsx,POI 有专门的类。 .docx 文件示例:

public static String parseDOCX(File file){
String author=null;
FileInputStream stream;
try {
    stream = new FileInputStream(file);
    XWPFDocument docx = new XWPFDocument(stream);
    CoreProperties props = docx.getProperties().getCoreProperties();
    author=props.getCreator();
    stream.close();
} catch (FileNotFoundException ex) {
   ex.printStackTrace();
} catch (IOException ex) {
    ex.printStackTrace();
}


return author;
}

用于 PPTX 使用 XMLSlideShow 或 XMLWorkbook 代替 XMLDocument。

【讨论】:

    【解决方案2】:

    请在此处找到示例代码-Appache POI how to

    简单来说,你可以是一个听众MyPOIFSReaderListener:

        SummaryInformation si = (SummaryInformation)
                 PropertySetFactory.create(event.getStream());
        String title = si.getTitle();
        String Author= si.getLastAuthor();
        ......
    

    并将其注册为:

        POIFSReader r = new POIFSReader();
        r.registerListener(new MyPOIFSReaderListener(),
                       "\005SummaryInformation");
        r.read(new FileInputStream(filename));
    

    【讨论】:

    【解决方案3】:

    对于 2003 office 文件,您可以使用从 POIDocument 继承的类。以下是 doc 文件的示例:

    FileInputStream in = new FileInputStream(file);
    HWPFDocument doc = new HWPFDocument(in);
    author = doc.getSummaryInformation().getAuthor();
    

    和 HSLFSlideShowImpl 用于 ppt,
    用于 xls 的 HSSFWorkbook,
    vsd 的 HDGF 图。

    SummaryInformation 类中还有许多其他文件信息。

    对于 2007 年或以上的 office 文件,请参阅@Dragos Catalin Trieanu 的回答

    【讨论】:

      猜你喜欢
      • 2013-12-20
      • 2018-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      相关资源
      最近更新 更多