Insert a new column into an existing excel file using jxl api

I am using jxl api to edit an existing excel file. But when I try to add a new column and write a leaflet, it will show a null pointer exception. The code I'm using is 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 solve this problem and insert a new column.

I can successfully edit individual excel cells and write the file.

+3
source share
4 answers

The api you are using is probably not the best. I also had this problem (nullpointer exception in insertcolumn line) and could not find any solution until I downloaded jexcelapi .

NTN

+2
source

. .

+2

, , /, -

Workbook aWorkBook = Workbook.getWorkbook(new File("Originalfile.xls"));
        WritableWorkbook aCopy = Workbook.createWorkbook(new File("Originalfile.xls"), aWorkBook);
        WritableSheet aCopySheet = aCopy.getSheet(0);//index of the needed sheet
        WritableCell aWritableCell = aCopySheet.getWritableCell(1,1);//no need!
        jxl.write.Label anotherWritableCell =  new jxl.write.Label(1,12 ,"SUN");
                //position of the new cell in column,row
            //can be a new Label() or new Number() or new Formula

            aCopySheet.addCell(anotherWritableCell);

        aCopy.write();
        aCopy.close();

, insertRow() insertColumn(). , !

+1

jxl, apache poi http://poi.apache.org/ ( - , - )

jxl , . . WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile, workbookTemplate, wbSettings); AutoFilters, , jxl.

http://poi.apache.org/spreadsheet/quick-guide.html http://www.kodejava.org/browse/49.html

0

All Articles