我编写了一个程序来创建和编写 Excel 工作表,关闭它,然后很快就可以访问它。但是,java 总是给我相同的jxl.read.biff.BiffException: The input file was not found
记录器消息——即使给定文件路径中确实存在文件。此外,当我尝试直接在 Excel 中打开文件时,我收到消息:“'filename.xls' 的文件格式和扩展名不匹配。文件可能已损坏或不安全。” 我假设这意味着 Excel 期待一个.xlsx
文件并接收一个.xls
文件,所以我相信我需要做的是最初使用 Windows 资源管理器创建文件,实现它是.xls
. 我是否以这种思路朝着正确的方向前进?
这是我的代码
创建文件:
private void createEmptyScore() {
xlsPath = path + "\\" + xlsName + ".xls";
newXls = new File(xlsPath);
try {
WritableWorkbook wwb = Workbook.createWorkbook(newXls);
wwb.createSheet("Sheet1", 0);
newXls.createNewFile();
wwb.write();
wwb.close();
} catch (Exception ex) {
Logger.getLogger(NewScoreMenu.class.getName()).log(Level.SEVERE, null, ex);
}
}
从文件中访问数据:
String cellData() throws Exception {
Workbook wb = Workbook.getWorkbook(xlsFile); // jxl.read.biff.BiffException HERE
Sheet sheet = wb.getSheet(0);
int scoreColumn = sheet.getColumns() - 1;
return sheet.getCell(col, row).getContents();
}
更改单元格数据(giveScore 是一个 JButton;chooseScore 是一个 JComboBox):
private void giveScoreActionPerformed(java.awt.event.ActionEvent evt) {
String score = chooseScore.getSelectedItem().toString();
try {
scoreSet(score);
currScore.setText("SCORE = " + cellData()); // See cellData() above
} catch (Exception ex) {
Logger.getLogger(EditSkeleton.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void scoreSet(String score) throws Exception {
Workbook wb = Workbook.getWorkbook(xlsFile);
WritableWorkbook copy = Workbook.createWorkbook(xlsFile, wb);
WritableSheet sheetToEdit = copy.getSheet(0);
WritableCell cell;
Label l = new Label(sheetToEdit.getColumns() - 1, imgPos + 1, score);
cell = (WritableCell) l;
sheetToEdit.addCell(cell);
copy.write();
copy.close();
wb.close();
}
谢谢!