为什么这段代码产生一个无效的Excel文件?

这创build了一个excel文件,它使文件格式不是有效的错误,当试图处理它,而不是适当的Excel文件添加一个数字:

public static void write () throws IOException, WriteException { WorkbookSettings settings = new WorkbookSettings(); File seurantaraportti = new File("ta.xls"); WritableWorkbook seurw = Workbook.createWorkbook(ta,settings); seurw.createSheet("ta", 0); WritableSheet ws = seurw.getSheet(0); addNumber(ws,0,0,100.0); seurw.close(); } private static void addNumber(WritableSheet sheet, int column, int row, Double d) throws WriteException, RowsExceededException { Number number=new Number(column, row,d); sheet.addCell(number); } 

我究竟做错了什么?

你没有写任何东西到工作簿。 你错过了

seurm.write()

在closures工作簿之前

seurw.close();

以下是工作代码。

 import java.io.File; import java.io.IOException; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.write.Number; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class WriteExcel { public static void write() throws IOException, WriteException { WorkbookSettings settings = new WorkbookSettings(); // settings.setLocale(new Locale("en", "EN")); File ta = new File("ta.xls"); WritableWorkbook seurw = Workbook.createWorkbook(ta, settings); seurw.createSheet("ta", 0); WritableSheet ws = seurw.getSheet(0); addNumber(ws, 0, 0, 100.0); seurw.write(); // You missed this line. seurw.close(); } private static void addNumber(WritableSheet sheet, int column, int row, Double d) throws WriteException, RowsExceededException { Number number = new Number(column, row, d); sheet.addCell(number); } public static void main(String[] args) { try { write(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

writableWorkbook seurw = Workbook.createWorkbook(ta,settings);

一定是

writableWorkbook seurw = Workbook.createWorkbook(“ta”,settings);