使用jxl api将新列插入到现有的excel文件中

时间:2021-01-02 20:22:40

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 :

我正在使用jxl api来编辑现有的excel文件。但是,当我尝试添加一个新列并写表时,它显示空指针异常。我正在使用的代码如下所示:

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.

我能够成功编辑excel的单个单元格并编写该文件。

4 个解决方案

#1


2  

Probably the api you are using is not the best one. I also had this problem (nullpointer exception at the insertcolumn line) and couldnt find any solution until I downloaded jexcelapi.

可能你使用的api并不是最好的。我也有这个问题(insertcolumn行的nullpointer异常)并且在我下载jexcelapi之前找不到任何解决方案。

HTH

HTH

#2


2  

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

上面的代码可以在我的电脑上正常运行。你最好告诉我们异常信息并将整个代码放在这里。

#3


1  

We can insert a new cell and thus a new row/column like this-

我们可以插入一个新单元格,从而插入一个新的行/列,如下所示

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();

I am not clear on what the insertRow() or insertColumn() methods do. Hope it helps!

我不清楚insertRow()或insertColumn()方法的作用。希望能帮助到你!

#4


0  

It is not possible with jxl, you must use apache poi http://poi.apache.org/ (or something else if there is anything else)

使用jxl是不可能的,你必须使用apache poi http://poi.apache.org/(或者其他什么东西)

With jxl you can only read or write workbooks, not edit existing ones. You can also copy workbook and edit newly created. WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile, workbookTemplate, wbSettings); But you will lose a lot of formating ald all AutoFilters, which was mine problem when I was useing jxl.

使用jxl,您只能读取或编写工作簿,而不能编辑现有工作簿。您还可以复制工作簿并编辑新创建的。 WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile,workbookTemplate,wbSettings);但是你会失去很多格式化和所有AutoFilters,这在我使用jxl时是我的问题。

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

这里有很好的例子http://poi.apache.org/spreadsheet/quick-guide.html http://www.kodejava.org/browse/49.html

#1


2  

Probably the api you are using is not the best one. I also had this problem (nullpointer exception at the insertcolumn line) and couldnt find any solution until I downloaded jexcelapi.

可能你使用的api并不是最好的。我也有这个问题(insertcolumn行的nullpointer异常)并且在我下载jexcelapi之前找不到任何解决方案。

HTH

HTH

#2


2  

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

上面的代码可以在我的电脑上正常运行。你最好告诉我们异常信息并将整个代码放在这里。

#3


1  

We can insert a new cell and thus a new row/column like this-

我们可以插入一个新单元格,从而插入一个新的行/列,如下所示

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();

I am not clear on what the insertRow() or insertColumn() methods do. Hope it helps!

我不清楚insertRow()或insertColumn()方法的作用。希望能帮助到你!

#4


0  

It is not possible with jxl, you must use apache poi http://poi.apache.org/ (or something else if there is anything else)

使用jxl是不可能的,你必须使用apache poi http://poi.apache.org/(或者其他什么东西)

With jxl you can only read or write workbooks, not edit existing ones. You can also copy workbook and edit newly created. WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile, workbookTemplate, wbSettings); But you will lose a lot of formating ald all AutoFilters, which was mine problem when I was useing jxl.

使用jxl,您只能读取或编写工作簿,而不能编辑现有工作簿。您还可以复制工作簿并编辑新创建的。 WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile,workbookTemplate,wbSettings);但是你会失去很多格式化和所有AutoFilters,这在我使用jxl时是我的问题。

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

这里有很好的例子http://poi.apache.org/spreadsheet/quick-guide.html http://www.kodejava.org/browse/49.html