Wednesday, October 28, 2009

Read From Excel File And Write To Excel File

You Can use Apache POI library with JARs file:
- poi-3.5-FINAL-20090928.jar
- poi-contrib-3.5-FINAL-20090928.jar
- poi-ooxml-3.5-FINAL-20090928.jar
- poi-scratchpad-3.5-FINAL-20090928.jar
You can download this from

http://www.filefactory.com/file/a07fc15/n/POIJars_rar

In you backingbean:


/////// ........................ Read From File ................
InputStream myxls = new FileInputStream("c:\\yourExcelFils.xls");
HSSFWorkbook wb = new HSSFWorkbook(myxls);
HSSFSheet sheet = wb.getSheetAt(0); // first sheet
HSSFRow row = sheet.getRow(1); // second row
HSSFCell cell = row.getCell((short)0); // first cell
if (cell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
System.out.println("The Cell was a String with value " + cell.getStringCellValue());
} else {
System.out.println ("The cell was nothing we're interested in");
}

/////// ........................ Write To File ................
HSSFWorkbook wb1 = new HSSFWorkbook();

HSSFSheet sheet = wb1.createSheet();
HSSFRow row = sheet.createRow((short)0);
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);

FileOutputStream fileOut = new FileOutputStream("c:\\yourExcelFils.xls");
wb1.write(fileOut);
fileOut.close();

No comments:

Post a Comment