我正在将JAVA与JXL一起使用,并试图将程序结果写入XLS文件。
我编写的代码如下。
我面临的问题是它仅在XLS文件中写入第一行数据。
所有其他行均未写入文件中。
我可以在控制台中成功显示所有记录,也可以将它们写入HTML文件。因此,未生成记录或循环错误不是这里的问题。
有人可以帮助您确定问题并提供解决方案吗?
WritableWorkbook resultWorkbbook = null;
try {
RESULT_FILE_LOCATION = resultlocation + "-Results-" + df.format(new Date()) + ".xls";
resultWorkbbook = Workbook.createWorkbook(new File(RESULT_FILE_LOCATION));
WritableSheet excelSheet = resultWorkbbook.createSheet("Sheet 1", 0);
for (int i = 1; i < sheet.getRows(); i++) {
//Some other operationshere.
excelSheet.addCell(new Number(0, i, totalCases));
excelSheet.addCell(new Label(1, i, testCase));
excelSheet.addCell(new Label(2, i, requestURL));
excelSheet.addCell(new Label(3, i, requestMethod));
excelSheet.addCell(new Label(4, i, requestContentType));
excelSheet.addCell(new Label(5, i, requestParameters));
excelSheet.addCell(new Label(6, i, expectedMessage));
excelSheet.addCell(new Label(7, i, responseStatusLine));
excelSheet.addCell(new Label(8, i, headers));
excelSheet.addCell(new Label(9, i, responsemsg));
excelSheet.addCell(new Label(10, i, caseStatus));
resultWorkbbook.write();
}
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:0)
似乎您将 write(); 方法放到了循环上。它应该调用 添加单元格 。尝试将 write(); 方法移出循环。
奖励
使用 close(); 方法。
编辑
以下是一些代码:
WritableWorkbook resultWorkbbook = null;
try {
RESULT_FILE_LOCATION = resultlocation + "-Results-" + df.format(new Date()) + ".xls";
resultWorkbbook = Workbook.createWorkbook(new File(RESULT_FILE_LOCATION));
WritableSheet excelSheet = resultWorkbbook.createSheet("Sheet 1", 0);
for (int i = 1; i < sheet.getRows(); i++) {
//Some other operationshere.
excelSheet.addCell(new Number(0, i, totalCases));
excelSheet.addCell(new Label(1, i, testCase));
excelSheet.addCell(new Label(2, i, requestURL));
excelSheet.addCell(new Label(3, i, requestMethod));
excelSheet.addCell(new Label(4, i, requestContentType));
excelSheet.addCell(new Label(5, i, requestParameters));
excelSheet.addCell(new Label(6, i, expectedMessage));
excelSheet.addCell(new Label(7, i, responseStatusLine));
excelSheet.addCell(new Label(8, i, headers));
excelSheet.addCell(new Label(9, i, responsemsg));
excelSheet.addCell(new Label(10, i, caseStatus));
}
resultWorkbbook.write();
resultWorkbbook.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
如果仍然不清楚或仍然无法解决问题,那么我没有其他解决方案。