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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10510543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:22:01  来源:igfitidea点击:

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

javajxl

提问by Jaison

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的单个单元格并写入文件。

采纳答案by PowR

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 行的空指针异常)并且在我下载jexcelapi之前找不到任何解决方案。

HTH

HTH

回答by Mark Yao

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

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

回答by Ishank

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() 方法的作用。希望能帮助到你!

回答by Lukas

It is not possible with jxl, you must use apache poihttp://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); 但是你会失去很多格式化和所有的自动过滤器,这是我在使用 jxl 时的问题。

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

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