gpt4 book ai didi

java - 为什么 JXL 不能在循环内工作

转载 作者:行者123 更新时间:2023-12-02 11:42:46 25 4
gpt4 key购买 nike

这是我的 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) 不在循环中工作。为什么?如果您在没有循环的情况下使用它并且对于一个单元格来说它工作得很好。

最佳答案

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

试试这个:

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

关于java - 为什么 JXL 不能在循环内工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48429174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com