Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java/Apache POI获取文件摘要信息_Java_Apache Poi_Summary - Fatal编程技术网

如何使用Java/Apache POI获取文件摘要信息

如何使用Java/Apache POI获取文件摘要信息,java,apache-poi,summary,Java,Apache Poi,Summary,我正试图用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

我正试图用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());
File rep=新文件(“C:\\Cry\u ReportERP006.rpt”);
/*将测试文档文档读入POI文件系统*/
final poifsffilesystem poifs=new poifsffilesystem(new FileInputStream(rep));
final DirectoryEntry dir=poifs.getRoot();
DocumentEntry dsiEntry=null;
尝试
{
dsiEntry=(DocumentEntry)dir.getEntry(DocumentSummaryInformation.DEFAULT\u STREAM\u NAME);
}
捕获(FileNotFoundException ex)
{
/*
*缺少文档摘要信息流不是错误
*因此在这里默默地被忽略了。
*/
}
/*
*如果有文档摘要信息流,请从
*POI文件系统。
*/
if(dsiEntry!=null)
{
最终文档输入流dis=新文档输入流(dsiEntry);
最终属性集ps=新属性集(dis);
最终文件汇总信息dsi=新文件汇总信息(ps);
最终汇总信息si=新汇总信息(ps);
/*执行get…方法*/
System.out.println(si.getAuthor());

请在此处找到示例代码-

简而言之,您可以创建一个监听器
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));

如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。

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

FileInputStream in = new FileInputStream(file);
HWPFDocument doc = new HWPFDocument(in);
author = doc.getSummaryInformation().getAuthor();
以及用于ppt的HSLFSlideShowImpl,
HSSF xls工作手册,
vsd的HDG图

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


对于2007年或更高版本的office文件,请参见@Dragos Catalin Trieanu的答案

为什么不发布代码,即使它不起作用,它仍然会有助于回答问题?所以你说的是MS office文件,对吗?现在我发布代码。不是关于MS Office文件,我指的是一般文件。你的代码有什么问题?我看差不多对吧?谢谢,我会读这些的。我也读过这篇文章,但我不明白。很好。如果这有帮助,请不要忘记接受答案。