Easy way read Excel sheets through Java
Application
In this article I’m going to introduce a easy way to
convert Excel spread sheets to XML files
using Java language. Suppose you are
having a Excel sheet with this template.
Step 1
In my case
I have an application GUI to browse tho location of the Excel sheet. You can
use your own way to import Excel sheets to your appliation and it doesn’t
matter whatever the way you use. Now you have the location of the excel sheet.
Step 2
To read the whole Excel sheet data you can use the
following java (method)code segment.
public Vector readExecl(String fileName) {
Vector cellVector = new
Vector();
try {
FileInputStream myInput
= new FileInputStream(fileName);
POIFSFileSystem
myfileSystem = new POIFSFileSystem(myInput);
HSSFWorkbook myworkbook
= new HSSFWorkbook(myfileSystem);
Workbook myworkbook =
WorkbookFactory.create(new File("file.xlsx"))
HSSFSheet mySheet =
myworkbook.getSheetAt(0);
Iterator rowIter =
mySheet.rowIterator();
while (rowIter.hasNext()) {
HSSFRow myRow =
(HSSFRow) rowIter.next();
Iterator cellIter =
myRow.cellIterator();
Vector cellStoreVector = new Vector();
while
(cellIter.hasNext()) {
HSSFCell myCell =
(HSSFCell) cellIter.next();
{
String
s = myCell.toString();
cellStoreVector.addElement(myCell);
}
}
cellVector.addElement(cellStoreVector);
}
} catch (Exception ex) {
System.out.println(ex);
}
return cellVector;
}
No comments:
Post a Comment