Well that very much depends upon EXACTLY what you want to do. The simplest
way to read a file is to do something like the following;
import org.apache.poi.xssf.usermodel.*;
import java.util.Iterator;
Iterator<org.apache.poi.ss.usermodel.Row> rowIterator = null;
Iterator<org.apache.poi.ss.usermodel.Cell> cellIterator = null;
//
// Enter the name and location of your file into the
// quotes below.
//
XSSFWorkbook workbook = new XSSFWorkbook("");
XSSFSheet sheet = null;
XSSFRow row = null;
XSSFCell cell = null;
int numSheets = workbook.getNumberOfSheets();
// For loop to iterate over the sheets in the workbook
for(int i = 0; i < numSheets; i++) {
sheet = workbook.getSheetAt(i);
rowIterator = sheet.iterator();
// While loop to iterate over the rows on the sheet
while(rowIterator.hasNext()) {
row = rowIterator.next();
cellIterator = row.iterator();
// While loop to iterate over the cells in the row.
while(cellIterator.hasNext()) {
cell = cellIterator.next();
// And then it all depends what you want to do with the cell.
// This will show the contents of the cell as a String
System.out.println(cell.getStringValue());
}
}
}
Try this code, it will print out the contents of each cell as a String. I do
not use the OpenXML version of Excel so cannot test the code, but I am
fairly confident it works. Am leaving for home now so will not be logging on
again this evening in all likelihood, good luck and I hope this helps.
Roberto Santini wrote:
>
> Thanks for your answer.
> The code the i found on the link, is to write a file, and I
> need to read a file.
> So, can you post me an example?? I have very few time to
> finish the application (1, 2 hours) and I haven't time to
> read all the docs... :D
>
> Thanks a lot
> Roberto
> ----- Segue Messaggio Originale -----
> Da : MSB <[email protected]>
> A : [email protected]
> oggetto : Re: Error with a XLS file
> Data : Thu, 28 May 2009 07:49:44 -0700 (PDT)
>
>> Hello Roberto,
>>
>> I have been following your conversation with Fillipo and
>> would like to ask a couple of questions please.
>>
>> In one of your replies, you said;
>>
>> Hi, I don't know how the file is saved (is automatically
>> generated by an ASPX page, with excel xml format) because
>> i download it from the web....
>>
>> If the file is in the xml format then you do not want to
>> be using HSSF to process it but XSSF. HSSF is designed to
>> work with the older binary file format not the new OpenXML
>> based file format. You need to change the code that you
>> have written to use XSSFWorkbook, XSSFSheet, XSSFRow,
>> XSSFCell, etc. I do not think that the file's extension
>> should be a problem but you could easily change it from
>> .xls to .xlsx.
>>
>> If you think that you may need to process both BIFF8 and
>> OpenXML (binary and xml) files then you can use the
>> techniques described here;
>>
>> http://poi.apache.org/spreadsheet/converting.html
>>
>> under the heading; 'New, generic SS Usermodel Code'
>>
>> Hopefully, that should solve the problem though you do
>> need to be aware that Microsoft made an earlier attempt
>> with an xml based file format - in Office 2003. If the web
>> site has created a file using this format then POI cannot
>> open it - at least not as far as I am aware.
>>
>>
>> Roberto Santini wrote:
>> >
>> > Hi to all, I0m a new ITALIAN member.
>> > I've a problem reading an XLS file; this file is
>> > automatically generated by an ASPX page.
>> > The error I'm getting is:
>> >
>> > java.io.IOException: Invalid header signature; read
>> > 7813033141555149807, expected -2226271756974174256
>> >
>> > There is a way to solve or the file is generated with an
>> > error so I can't read it?
>> > If i try to open the same file with Office, it run!
>> >
>> > Bye
>> > Roberto
>> >
>> >
>> ----------------------------------------------------------
>> > --------- Roberto Santini
>> > www.lasfidacontinua.it
>> >
>> > MSN: [email protected]
>> > Skype: cent89
>> >
>> >
>> ----------------------------------------------------------
>> > ----------- To unsubscribe, e-mail:
>> > [email protected] For additional commands,
>> > e-mail: [email protected]
>> >
>> >
>>
>> --
>> View this message in context:
>>
> http://www.nabble.com/Error-with-a-XLS-file-tp23757478p23763310.html
>> Sent from the POI - User mailing list archive at
>> Nabble.com.
>>
>>
>> ----------------------------------------------------------
>> ----------- To unsubscribe, e-mail:
>> [email protected] For additional commands,
>> e-mail: [email protected]
>>
>
> -------------------------------------------------------------------
> Roberto Santini
> www.lasfidacontinua.it
>
> MSN: [email protected]
> Skype: cent89
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
>
--
View this message in context:
http://www.nabble.com/Error-with-a-XLS-file-tp23757478p23765811.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]