简体   繁体   English

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

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

I'm using jxl api for editing an existing excel file. 我正在使用jxl api来编辑现有的excel文件。 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. 我能够成功编辑excel的单个单元格并编写该文件。

The above code can run fine in my computer. 上面的代码可以在我的电脑上正常运行。 you'd better to tell us the exception information and put the whole code here. 你最好告诉我们异常信息,并把整个代码放在这里。

Probably the api you are using is not the best one. 可能你使用的api并不是最好的。 I also had this problem (nullpointer exception at the insertcolumn line) and couldnt find any solution until I downloaded jexcelapi . 我也有这个问题(insertcolumn行的nullpointer异常)并且在我下载jexcelapi之前找不到任何解决方案。

HTH HTH

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. 我不清楚insertRow()或insertColumn()方法的作用。 Hope it helps! 希望能帮助到你!

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. 使用jxl,您只能读取或编写工作簿,而不能编辑现有工作簿。 You can also copy workbook and edit newly created. 您还可以复制工作簿并编辑新创建的。 WritableWorkbook newWorkbook = Workbook.createWorkbook(newFile, workbookTemplate, wbSettings); 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. 但是你会丢失很多格式化的所有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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM