Inserting new column to an already existing excel file using jxl api

Jaison picture Jaison · May 9, 2012 · Viewed 13.1k times · Source

I'm using jxl api for editing an existing excel file. But when i try to add a new column and write the sheet its showing null pointer exception. The code that I'm using is as shown below :

File file = new File("d:\\test.xls");
Workbook workbook;
WritableWorkbook copy = null;
if (file.exists()) {

    try {
        workbook = Workbook.getWorkbook(file);
        copy = Workbook.createWorkbook(new File("C:\\TEMP\\temp.xls"),
                workbook);
    } catch (BiffException e) {

        e.printStackTrace();
    } catch (FileNotFoundException fnf) {
        fnf.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();
    }

}   
WritableSheet sheet = copy.getSheet(0);

sheet.insertColumn(2); //this statement causes error  
                      //if I comment it the code works fine

try {
    copy.write();
    copy.close();
}
catch(Exception e)
{

    e.printStackTrace();
}

Please help me to solve this problem and insert new column .

I'm able to edit the single cells of excel successfully and write the file.

Answer

Mark Yao picture Mark Yao · May 9, 2012

The above code can run fine in my computer. you'd better to tell us the exception information and put the whole code here.