为什么JXL不在循环内工作

时间:2018-01-24 18:21:10

标签: java jxl

这是我的JXL和Jsoup代码

public static final String EXCEL_FILE_LOCATION = "E:\\xll.xls";
public static WritableWorkbook myFirstWbook = null;

myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));

for(i=0; i <= j; i++){
    // create an Excel sheet
    WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);

    // add something into the Excel sheet
    Label label = new Label(1, i+1, "Test Count");

    try {
        excelSheet.addCell(label);
    } catch (WriteException ex) {
        Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
    }


    myFirstWbook.write();
}   

addCell(label)无法循环工作。为什么?如果你没有循环使用它,对于一个单元格,它可以正常工作。

1 个答案:

答案 0 :(得分:1)

也许是因为每次实例化工作表或工作簿时,所有先前的语法 消失 。循环就是这种情况。

尝试一下:

public static final String EXCEL_FILE_LOCATION = "E:\\xll.xls";
public static WritableWorkbook myFirstWbook = null;

myFirstWbook = Workbook.createWorkbook(new File(EXCEL_FILE_LOCATION));

// create an Excel sheet
WritableSheet excelSheet = myFirstWbook.createSheet("Sheet 1", 0);

for(i=0; i <= j; i++){

    // add something into the Excel sheet
    Label label = new Label(1, i+1, "Test Count");

    try {
        excelSheet.addCell(label);
    } catch (WriteException ex) {
        Logger.getLogger(Crawler.class.getName()).log(Level.SEVERE, null, ex);
    }
}
myFirstWbook.write();
myFirstWbook.close();