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に使用するには、XMLDocumentの代わりにXMLSlideShowまたはXMLWorkbookを使用します。

于 2013-04-12T19:23:21.057 に答える
1

ここでサンプルコードを見つけてください-AppachePOIの方法

簡単に言えば、あなたはリスナーになることができます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オフィスファイルの場合、POIDocumentから継承されたクラスを使用できます。docファイルの例を次に示します。

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

pptの場合は
HSLFSlideShowImpl、xlsの場合はHSSFWorkbook、
vsdの場合はHDGFDiagram。

SummaryInformationクラス内には他にも多くのファイル情報があります。

2007年以降のオフィスファイルについては、@ DragosCatalinTrieanuの回答を参照してください。

于 2018-08-17T01:58:43.260 に答える