Hi I get this message 

java.io.IOException: Unable to read entire block; 407 bytes read; expected 512 
bytes
        at org.apache.poi.poifs.storage.RawDataBlock.<init>
(RawDataBlock.java:62)
        at org.apache.poi.poifs.storage.RawDataBlockList.<init>
(RawDataBlockList.java:51)
        at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>
(POIFSFileSystem.java:86)
        at EventExample.main(EventExample.java:137)
Exception in thread "main" 

When i try to open an excel. It works fine if I save the excel after manually 
opening it.

Can some help??



import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.record.DateWindow1904Record;
import org.apache.poi.hssf.record.FormulaRecord;
import org.apache.poi.hssf.record.LabelSSTRecord;
import org.apache.poi.hssf.record.NumberRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.SSTRecord;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/*
 * Created on Oct 13, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

/**
 * @author rveliche
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */

        /**
 * 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 FormulaRecord.sid:
                                FormulaRecord frec = (FormulaRecord) record;
                                Double dn = new Double(frec.getValue());
                                System.out.println("Formula found with value " 
+ frec.getValue()
                                                                        + " at 
row " + frec.getRow() + " and column " + frec.getColumn());
                                System.out.println((frec.toString()));
                                if(dn.equals(new Double(Double.NaN))){
                                        System.out.println("NaN");
                                }
                                break;
                        case DateWindow1904Record.sid:
                                DateWindow1904Record drec = 
(DateWindow1904Record) record;
                                System.out.println("Date Format Encountered");
                                break;
                        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 at "+ 
lrec.getRow() +" and column "+lrec.getColumn()+"with value "
                                                + sstrec.getString
(lrec.getSSTIndex()));
                                break;
                        case StringRecord.sid:
                                StringRecord srec = (StringRecord) record;
                                System.out.println("String found at with value "
                                                + srec.getString());
                                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 fis=new FileInputStream("locks.zip");
                ZipInputStream zin=new ZipInputStream(new BufferedInputStream
(fis));
                ZipEntry ze;
                InputStream is = null;
                while((ze = zin.getNextEntry()) != null){
                        String s = ze.getName();
                        System.out.println(s);
                        if(s.equalsIgnoreCase
("locks/TypeK_New/DATA_TABLE/OKSAUTHTAB.xls")){
                                is = (InputStream) zin;
                                break;
                        }
                }
                FileInputStream fin = new FileInputStream("ap0001a.xls");
                // create a new org.apache.poi.poifs.filesystem.Filesystem
                BufferedInputStream bis = new BufferedInputStream(zin);
                POIFSFileSystem poifs = new POIFSFileSystem(bis);
                
                //HSSFWorkbook wb = new HSSFWorkbook(poifs);
                // get the Workbook (excel part) stream in a InputStream
                //System.out.println(wb.getSheetName(0));
                InputStream din = poifs.createDocumentInputStream("Workbook");
                //POIFSDocument pd = new POIFSDocument("Workbook",zin);
                //DocumentInputStream din = new DocumentInputStream(pd);
                // 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.");
        }
}





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to