reading individual sheets using poi
807597Aug 21 2005 — edited Oct 17 2005Hello Techies,
Iam reading an excel sheet using poi. I want to read sheet by sheet n also i want to change the values in each individual sheet.
Here is my program.
import java.io.*;
import java.net.*;
import org.apache.poi.*;
import org.apache.poi.hssf.*;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
class readingExcelSheet
{
public static void main(String args[])
{
try
{
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream(args[0]));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
HSSFSheet sheet1 = wb.getSheetAt(1);
HSSFRow row1 = sheet1.getRow(3);
HSSFCell cell1 = row1.getCell((short)5);
if (cell1 == null)
cell1 = row.createCell((short)3);
cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell1.setCellValue(222222);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("ramu.xls");
wb.write(fileOut);
fileOut.close();
}
catch (IOException ioe) {
// TODO: handle exception
System.out.println("IOException"+ioe.getMessage());
}
}
}
When i mentioning one sheet it is working fine. when i place the
the following code i.e reading second sheet of excel
HSSFSheet sheet1 = wb.getSheetAt(1);
HSSFRow row1 = sheet1.getRow(3);
HSSFCell cell1 = row1.getCell((short)5);
if (cell1 == null)
cell1 = row.createCell((short)3);
cell1.setCellType(HSSFCell.CELL_TYPE_NUMERIC);
cell1.setCellValue(222222);
Iam getting nullpointer exception.
Why iam getting this nullpointer exception.
Also i want to read all values in a perticular coulmn i.e. in sheet1 i want to read all the values in column B.
How can i do this.
Thanks(inadvance),
ramu.