2

我正在尝试使用 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());
4

3 回答 3

2

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

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。

于 2013-04-12T19:23:21.057 回答
1

请在此处找到示例代码 - Appache POI 如何

简而言之,您可以是一个监听器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));
于 2012-10-12T14:26:49.973 回答
0

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

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

和 HSLFSlideShowImpl 用于 ppt,
HSSFWorkbook 用于 xls,
HDGFDiagram 用于 vsd。

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

对于 2007 年或以上的办公文件,请参阅@Dragos Catalin Trieanu 的答案

于 2018-08-17T01:58:43.260 回答