POI读取Word文件头信息

来源:互联网 发布:淘宝购物车怎么代付 编辑:程序博客网 时间:2024/05/16 04:52

新建java工程,添加 poi-3.10-FINAL-20140208.jar 文件和poi-scratchpad-3.10-FINAL-20140208.jar文件,编译运行下面代码:

package com.example.poidemo;import java.io.BufferedInputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Iterator;import java.util.List;import org.apache.poi.hpsf.NoPropertySetStreamException;import org.apache.poi.hpsf.Property;import org.apache.poi.hpsf.PropertySet;import org.apache.poi.hpsf.PropertySetFactory;import org.apache.poi.hpsf.Section;import org.apache.poi.hpsf.SummaryInformation;import org.apache.poi.poifs.eventfilesystem.POIFSReader;import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;import org.apache.poi.util.*;public class DocReader {public static class MyPOIFSReaderListener implements POIFSReaderListener{    public void processPOIFSReaderEvent(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.");        */    PropertySet ps = null;        try        {            ps = PropertySetFactory.create(event.getStream());        }        catch (NoPropertySetStreamException ex)        {        System.out.println("No property set stream: \"" + event.getPath() +                event.getName() + "\"");            return;        }        catch (Exception ex)        {            throw new RuntimeException                ("Property set stream \"" +                 event.getPath() + event.getName() + "\": " + ex);        }        /* Print the name of the property set stream: */        System.out.println("Property set stream \"" + event.getPath() +            event.getName() + "\":");                /* Print the number of sections: */        final long sectionCount = ps.getSectionCount();        System.out.println("   No. of sections: " + sectionCount);        /* Print the list of sections: */        List sections = ps.getSections();        int nr = 0;        for (Iterator i = sections.iterator(); i.hasNext();)        {            /* Print a single section: */            Section sec = (Section) i.next();            // See below for the complete loop body.            System.out.println("   Section " + nr++ + ":");            String s = HexDump.toHex(sec.getFormatID().getBytes());            s = s.substring(0, s.length() - 1);            System.out.println("      Format ID: " + s);            /* Print the number of properties in this section. */            int propertyCount = sec.getPropertyCount();            System.out.println("      No. of properties: " + propertyCount);            /* Print the properties: */            Property[] properties = sec.getProperties();            for (int i2 = 0; i2 < properties.length; i2++)            {                /* Print a single property: */                Property p = properties[i2];                int id = (int)p.getID();                long type = p.getType();                Object value = p.getValue();                System.out.println("      Property ID: " + id + ", type: " + type +                    ", value: " + value);            }        }    }}public static void main(String[] args) {final String filename = "D:\\Backup\\小学数学第十二册圆柱与圆锥典型习题集.doc";    POIFSReader r = new POIFSReader();    //r.registerListener(new MyPOIFSReaderListener(), "\005SummaryInformation");    /* Register a listener for *all* documents. */    r.registerListener(new MyPOIFSReaderListener());    try {r.read(new FileInputStream(filename));} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}  }}

输出信息如下:

Property set stream "\SummaryInformation":   No. of sections: 1   Section 0:      Format ID: [F2, 9F, 85, E0, 4F, F9, 10, 68, AB, 91, 08, 00, 2B, 27, B3, D9      No. of properties: 17      Property ID: 1, type: 2, value: 936      Property ID: 2, type: 30, value: 第十二册第一单元试卷      Property ID: 3, type: 30, value:       Property ID: 4, type: 30, value: thtfpc user      Property ID: 5, type: 30, value:       Property ID: 6, type: 30, value:       Property ID: 7, type: 30, value: Normal      Property ID: 8, type: 30, value: 微软中国      Property ID: 9, type: 30, value: 9      Property ID: 18, type: 30, value: Microsoft Office Word      Property ID: 10, type: 64, value: Mon Jan 01 08:31:00 CST 1601      Property ID: 12, type: 64, value: Wed Mar 11 21:09:00 CST 2009      Property ID: 13, type: 64, value: Thu Mar 12 21:58:00 CST 2009      Property ID: 14, type: 3, value: 1      Property ID: 15, type: 3, value: 231      Property ID: 16, type: 3, value: 1320      Property ID: 19, type: 3, value: 0Property set stream "\DocumentSummaryInformation":   No. of sections: 1   Section 0:      Format ID: [D5, CD, D5, 02, 2E, 9C, 10, 1B, 93, 97, 08, 00, 2B, 2C, F9, AE      No. of properties: 10      Property ID: 1, type: 2, value: 936      Property ID: 15, type: 30, value: thtfpc      Property ID: 5, type: 3, value: 11      Property ID: 6, type: 3, value: 3      Property ID: 17, type: 3, value: 1548      Property ID: 23, type: 3, value: 730895      Property ID: 11, type: 11, value: false      Property ID: 16, type: 11, value: false      Property ID: 19, type: 11, value: false      Property ID: 22, type: 11, value: falseNo property set stream: "\WordDocument"No property set stream: "\1Table"No property set stream: "\ObjectPool\_1206942592CompObj"No property set stream: "\ObjectPool\_1206942592ObjInfo"No property set stream: "\ObjectPool\_1206942592Equation Native"No property set stream: "\ObjectPool\_1206942592Ole"No property set stream: "\ObjectPool\_1171713183CompObj"No property set stream: "\ObjectPool\_1171713183Ole10Native"No property set stream: "\ObjectPool\_1171713183Ole10ItemName"No property set stream: "\ObjectPool\_1171713183ObjInfo"No property set stream: "\ObjectPool\_1171713183Ole"No property set stream: "\CompObj"No property set stream: "\Data"


0 0