我编写了一个程序来创建和编写Excel工作表,将其关闭,然后在不久后访问它。但是,java总是给我同样的jxl.read.biff.BiffException: The input file was not found
记录器消息-即使在给定的文件路径中确实存在一个文件。此外,当我尝试直接在Excel中打开文件时,收到消息:“'filename.xls'的文件格式和扩展名不匹配。该文件可能已损坏或不安全。”我假设这意味着Excel期望使用.xlsx
文件,而接收的是.xls
文件,因此我认为我需要做的最初是使用Windows资源管理器 realizing创建该文件是.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; choiceScore是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();
}
谢谢!
答案 0 :(得分:0)
我尝试复制您的代码并进行一些更改。 但是让我问一下,您在哪里更新分数?在创建文件时,您只创建了一个空白工作表,然后在return语句中,您尝试获取给定文件中第一个excel工作表的特定单元格的内容。
public class ExcelWriter {
private static String xlsPath;
private static String path;
private static String xlsName;
private static File newXls;
private static 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) {
System.out.println("Error: " + ex.getMessage());
}
}
private static String cellData() throws Exception {
Workbook wb = Workbook.getWorkbook(newXls); // jxl.read.biff.BiffException HERE
Sheet sheet = wb.getSheet(0);
//what is this for?
int scoreColumn = sheet.getColumns() - 1;
//Seems you're trying to use col, and row for the contents
//but you did not specify the values for these variables.
//return sheet.getCell(col, row).getContents();
return sheet.getCell(0, 0).getContents();
}
public static void main(String[] args) throws Exception{
//Declare the path and filename, You did not specify this
//I created a dummy folder for this one
path = "D:/test";
xlsName = "score";
createEmptyScore();
//This would return an ArrayIndexOutOfBoundsException because the
//Excel sheet is empty
System.out.println(cellData());
}
}