使用jxl写入Excel时,出现indexOutofBoundException错误

时间:2018-08-30 08:13:16

标签: java indexoutofboundsexception jxl

public static void main(String args[]) throws BiffException, IOException, RowsExceededException, WriteException{


WritableWorkbook workbookCP;
    try {       
        Workbook workbook =  Workbook.getWorkbook(new File("C:/Users/collections.xls"));
        workbookCP = Workbook.createWorkbook(new File("C:/Users/collectionsCopy.xls"),workbook);
        WritableSheet sheet = workbookCP.getSheet("collections");
        Label label = new Label(10, 0, "Name");
        sheet.addCell(label);
        Label label1 = new Label(11, 0, "Surname");
        sheet.addCell(label1);
        Label label2 = new Label(13, 0, "Ammount");
        sheet.addCell(label2);
        workbookCP.write();
        workbookCP.close();
        workbook.close();

            }
            catch(Exception e)
            {
                e.printStackTrace();
            }

我正在尝试使用以上代码将数据写入现有的excel文件,但是出现以下错误 谢谢

   java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

1 个答案:

答案 0 :(得分:0)

尝试更改这两行:

Workbook workbook =  Workbook.getWorkbook(new File("C:/Users/collections.xls"));
workbookCP = Workbook.createWorkbook(new File("C:/Users/collectionsCopy.xls"),workbook);

对此:

Workbook workbook =  Workbook.getWorkbook(new File("C:\\Users\\collections.xls"));
workbookCP = Workbook.createWorkbook(new File("C:\\Users\\collectionsCopy.xls"),workbook);