jxl - "The file format and extension don't match"

825 views Asked by At

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!

1

There are 1 answers

2
Ranielle Canlas On

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.

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

}