⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 how-to.xml.orig

📁 Office格式转换代码
💻 ORIG
📖 第 1 页 / 共 2 页
字号:
                    structures.  Its relatively simple to use, but requires a basic                    understanding of the parts of an Excel file (or willingness to                    learn).  The advantage provided is that you can read an XLS with a                    relatively small memory footprint.                    </p>                    <p>To use this API you construct an instance of                    org.apache.poi.hssf.eventmodel.HSSFRequest. Register a class you                    create that supports the                    org.apache.poi.hssf.eventmodel.HSSFListener interface using the                    HSSFRequest.addListener(yourlistener, recordsid).  The recordsid                    should be a static reference number (such as BOFRecord.sid) contained                    in the classes in org.apache.poi.hssf.record.  The trick is you                    have to know what these records are.  Alternatively you can call                    HSSFRequest.addListenerForAllRecords(mylistener).  In order to learn                    about these records you can either read all of the javadoc in the                    org.apache.poi.hssf.record package or you can just hack up a                    copy of org.apache.poi.hssf.dev.EFHSSF and adapt it to your                    needs.  TODO: better documentation on records.</p>                    <p>Once you've registered your listeners in the HSSFRequest object                    you can construct an instance of                    org.apache.poi.poifs.filesystem.FileSystem (see POIFS howto) and                    pass it your XLS file inputstream.  You can either pass this, along                    with the request you constructed, to an instance of HSSFEventFactory                    via the HSSFEventFactory.processWorkbookEvents(request, Filesystem)                    method, or you can get an instance of DocumentInputStream from                    Filesystem.createDocumentInputStream(&quot;Workbook&quot;) and pass                    it to HSSFEventFactory.processEvents(request, inputStream).  Once you                    make this call, the listeners that you constructed receive calls to                    their processRecord(Record) methods with each Record they are                    registered to listen for until the file has been completely read.                    </p>                    <p>A code excerpt from org.apache.poi.hssf.dev.EFHSSF (which is                    in CVS or the source distribution) is reprinted below with excessive                    comments:</p><source><![CDATA[/** * This example shows how to use the event API for reading a file. */public class EventExample        implements HSSFListener{    private SSTRecord sstrec;    /**     * This method listens for incoming records and handles them as required.     * @param record    The record that was found while reading.     */    public void processRecord(Record record)    {        switch (record.getSid())        {            // the BOFRecord can represent either the beginning of a sheet or the workbook            case BOFRecord.sid:                BOFRecord bof = (BOFRecord) record;                if (bof.getType() == bof.TYPE_WORKBOOK)                {                    System.out.println("Encountered workbook");                    // assigned to the class level member                } else if (bof.getType() == bof.TYPE_WORKSHEET)                {                    System.out.println("Encountered sheet reference");                }                break;            case BoundSheetRecord.sid:                BoundSheetRecord bsr = (BoundSheetRecord) record;                System.out.println("New sheet named: " + bsr.getSheetname());                break;            case RowRecord.sid:                RowRecord rowrec = (RowRecord) record;                System.out.println("Row found, first column at "                        + rowrec.getFirstCol() + " last column at " + rowrec.getLastCol());                break;            case NumberRecord.sid:                NumberRecord numrec = (NumberRecord) record;                System.out.println("Cell found with value " + numrec.getValue()                        + " at row " + numrec.getRow() + " and column " + numrec.getColumn());                break;                // SSTRecords store a array of unique strings used in Excel.            case SSTRecord.sid:                sstrec = (SSTRecord) record;                for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)                {                    System.out.println("String table value " + k + " = " + sstrec.getString(k));                }                break;            case LabelSSTRecord.sid:                LabelSSTRecord lrec = (LabelSSTRecord) record;                System.out.println("String cell found with value "                        + sstrec.getString(lrec.getSSTIndex()));                break;        }    }    /**     * Read an excel file and spit out what we find.     *     * @param args      Expect one argument that is the file to read.     * @throws IOException  When there is an error processing the file.     */    public static void main(String[] args) throws IOException    {        // create a new file input stream with the input file specified        // at the command line        FileInputStream fin = new FileInputStream(args[0]);        // create a new org.apache.poi.poifs.filesystem.Filesystem        POIFSFileSystem poifs = new POIFSFileSystem(fin);        // get the Workbook (excel part) stream in a InputStream        InputStream din = poifs.createDocumentInputStream("Workbook");        // construct out HSSFRequest object        HSSFRequest req = new HSSFRequest();        // lazy listen for ALL records with the listener shown above        req.addListenerForAllRecords(new EventExample());        // create our event factory        HSSFEventFactory factory = new HSSFEventFactory();        // process our events based on the document input stream        factory.processEvents(req, din);        // once all the events are processed close our file input stream        fin.close();        // and our document input stream (don't want to leak these!)        din.close();        System.out.println("done.");    }}]]></source>     </section>                <section><title>Low Level APIs</title><p>The low level API is not much to look at. It consists of lots of&quot;Records&quot; in the org.apache.poi.hssf.record.* package,and set of helper classes in org.apache.poi.hssf.model.*. Therecord classes are consistent with the low level binary structuresinside a BIFF8 file (which is embedded in a POIFS file system). Youprobably need the book: &quot;Microsoft Excel 97 Developer's Kit&quot;from Microsoft Press in order to understand how these fit together(out of print but easily obtainable from Amazon's used books). Inorder to gain a good understanding of how to use the low level APIsshould view the source in org.apache.poi.hssf.usermodel.* andthe classes in org.apache.poi.hssf.model.*. You should read thedocumentation for the POIFS libraries as well.</p>     </section>                <section><title>HSSF Class/Test Application</title><p>The HSSF application is nothing more than a test for the highlevel API (and indirectly the low level support). The main body ofits code is repeated above. To run it:</p><ul>    <li>download the poi-alpha build and untar it (tar xvzf    tarball.tar.gz)    </li>    <li>set up your classpath as follows:    <code>export HSSFDIR={wherever you put HSSF's jar files}export LOG4JDIR={wherever you put LOG4J's jar files}export CLASSPATH=$CLASSPATH:$HSSFDIR/hssf.jar:$HSSFDIR/poi-poifs.jar:$HSSFDIR/poi-util.jar:$LOG4JDIR/jog4j.jar</code>    </li><li>type:    <code>java org.apache.poi.hssf.dev.HSSF ~/myxls.xls write</code></li></ul><p></p><p>This should generate a test sheet in your home directory called <code>&quot;myxls.xls&quot;</code>.  </p><ul>    <li>Type:    <code>java org.apache.poi.hssf.dev.HSSF ~/input.xls output.xls</code>    <br/>    <br/>This is the read/write/modify test.  It reads in the spreadsheet, modifies a cell, and writes it back out.Failing this test is not necessarily a bad thing.  If HSSF tries to modify a non-existant sheet then this willmost likely fail.  No big deal.  </li></ul>     </section>    <section><title>Logging facility</title>        <p>Poi can dynamically select it's logging implementation. Poi trys to        create a logger using the System property named "org.apache.poi.util.POILogger".        Out of the box this can be set to one of three values:        </p>        <ul>           <li>org.apache.poi.util.CommonsLogger</li>           <li>org.apache.poi.util.NullLogger</li>           <li>org.apache.poi.util.SystemOutLogger</li>        </ul>        <p>        If the property is not defined or points to an invalid classthen the NullLogger is used.        </p>        <p>        Refer to the commons logging package level javadoc for more information concerning how to        <link href="http://jakarta.apache.org/commons/logging/api/index.html">configure commons logging.</link>        </p>     </section>    <section><title>HSSF Developer's Tools</title><p>HSSF has a number of tools useful for developers to debug/developstuff using HSSF (and more generally XLS files). We've alreadydiscussed the app for testing HSSF read/write/modify capabilities;now we'll talk a bit about BiffViewer. Early on in the development ofHSSF, it was decided that knowing what was in a record, what waswrong with it, etc. was virtually impossible with the availabletools. So we developed BiffViewer. You can find it atorg.apache.poi.hssf.dev.BiffViewer. It performs two basicfunctions and a derivative.</p><p>The first is &quot;biffview&quot;. To do this you run it (assumesyou have everything setup in your classpath and that you know whatyou're doing enough to be thinking about this) with an xls file as aparameter. It will give you a listing of all understood records withtheir data and a list of not-yet-understood records with no data(because it doesn't know how to interpret them). This listing isuseful for several things. First, you can look at the values and SEEwhat is wrong in quasi-English. Second, you can send the output to afile and compare it.</p><p>The second function is &quot;big freakin dump&quot;, just pass afile and a second argument matching &quot;bfd&quot; exactly. Thiswill just make a big hexdump of the file.</p><p>Lastly, there is &quot;mixed&quot; mode which does the same asregular biffview, only it includes hex dumps of certain recordsintertwined. To use that just pass a file with a second argumentmatching &quot;on&quot; exactly.</p><p>In the next release cycle we'll also have something called aFormulaViewer. The class is already there, but its not very usefulyet. When it does something, we'll document it.</p>     </section>                <section><title>What's Next?</title><p>This release contains code that supports &quot;internationalization&quot;or more accurately non-US/UK languages; however, it has not beentested with the new API changes (please help us with this). We'veshifted focus a bit for this release in recognition of theinternational support we've gotten. We're going to focus on westernEuropean languages for our first beta. We're more than happy toaccept help in supporting non-Western European languages if someonewho knows what they're doing in this area is willing to pitch in!(There is next to no documentation on what is necessary to supportsuch a move and its really hard to support a language when you don't evenknow the alphabet).</p><p>This release of HSSF does not yet support Formulas. I've beenfocusing on the requests I've gotten in. That being said, if we getmore user feedback on what is most useful first we'll aim for that.As a general principal, HSSF's goal is to support HSSF-Serializer(meaning an emphasis on write). We would like to hear from you! Howare you using HSSF/POIFS? How would you like to use it? What featuresare most important first?</p>     </section></section></section></body></document>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -