I've written a program to create and write an Excel sheet, close it, and then access it soon after. However, java always gives me the same jxl.read.biff.BiffException: The input file was not found
logger message - even though a file does exist at the given file path. Additionally, when I try to open the file directly in Excel, I get the message: "The file format and extension of 'filename.xls' don't match. The file could be corrupted or unsafe." I'm assuming this means that Excel is expecting a .xlsx
file and receiving a .xls
file instead, so I believe what I need to do is originally create the file with Windows Explorer realizing it's .xls
. Am I headed in the right direction with this train of thought?
Here's my code
Creating the file:
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);
}
}
Accessing data from the file:
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();
}
Changing the cell data (giveScore is a JButton; chooseScore is a 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();
}
Thanks!
I tried replicating your code and do some changes. But let me ask this, where are you updating the scores? in your creation of file, you just created a blank sheet, then in your return statement, you are trying to get the content of specific cell of the first excel sheet in the given file.