在Java中使用Java Excel库写入excel不能打开文件

我用jxl来写如下:

WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls")); WritableSheet sheet = workbook.createSheet("First Sheet", 0); Label label = new Label(0, 2, "A label record"); sheet.addCell(label); Number number = new Number(3, 4, 3.1459); sheet.addCell(number); 

当我尝试打开文件时出现错误:

您尝试打开的文件的格式不符合扩展名指定的格式。 在打开文件之前确认文件没有被损坏,并且来自可信的源文件。 你想现在打开文件吗?

当我点击是,它是空的。

您需要添加以下语句:

 workbook.write(); workbook.close(); 

我不能说你的代码,但如果你想写入数据到Excel文件,那么你可以使用这个特定的代码,我已经使用了1000多次,

 import java.io.File; import java.io.IOException; import java.util.Locale; import jxl.CellView; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.format.UnderlineStyle; import jxl.write.Formula; import jxl.write.Label; import jxl.write.Number; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; public class WriteExcel { private WritableCellFormat timesBoldUnderline; private WritableCellFormat times; private String inputFile; private String[][] contentD; private int value; public void setOutputFile(String inputFile) { this.inputFile = inputFile; } public void write() throws IOException, WriteException { File file = new File(inputFile); WorkbookSettings wbSettings = new WorkbookSettings(); wbSettings.setLocale(new Locale("en", "EN")); WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings); workbook.createSheet("Report", 0); WritableSheet excelSheet = workbook.getSheet(0); createLabel(excelSheet); createContent(excelSheet); workbook.write(); workbook.close(); } private void createLabel(WritableSheet sheet) throws WriteException { // Lets create a times font WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10); // Define the cell format times = new WritableCellFormat(times10pt); // Lets automatically wrap the cells times.setWrap(true); // Create create a bold font with unterlines WritableFont times10ptBoldUnderline = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, false, UnderlineStyle.SINGLE); timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline); // Lets automatically wrap the cells timesBoldUnderline.setWrap(true); CellView cv = new CellView(); cv.setFormat(times); cv.setFormat(timesBoldUnderline); //cv.setAutosize(true); // Write a few headers addCaption(sheet, 0, 1, "Images"); addCaption(sheet, 2, 1, "XXXXXXXX"); } private void createContent(WritableSheet sheet) throws WriteException, RowsExceededException { // Lets calculate the sum of it StringBuffer buf = new StringBuffer(); buf.append("SUM(A2:A10)"); Formula f = new Formula(0, 10, buf.toString()); sheet.addCell(f); buf = new StringBuffer(); buf.append("SUM(B2:B10)"); f = new Formula(1, 10, buf.toString()); sheet.addCell(f); // Now a bit of text for (int i = 0; i < contentD.length; i++) { for (int j = 0; j < contentD[0].length; j++) { // First column addLabel(sheet, j, i + 2, contentD[i][j]); } } } private void addCaption(WritableSheet sheet, int column, int row, String s) throws RowsExceededException, WriteException { Label label; label = new Label(column, row, s, timesBoldUnderline); sheet.addCell(label); } private void addNumber(WritableSheet sheet, int column, int row, Integer integer) throws WriteException, RowsExceededException { Number number; number = new Number(column, row, integer, times); sheet.addCell(number); } private void addLabel(WritableSheet sheet, int column, int row, String s) throws WriteException, RowsExceededException { Label label; label = new Label(column, row, s, times); sheet.addCell(label); } public WriteExcel(String fileName, String[][] content) throws WriteException, IOException { contentD = content; setOutputFile(fileName); write(); System.out.println("Please check the result file under c:/temp/lars.xls "); } } 

这样称呼,

WriteExcel writeExcel = new WriteExcel(“d:\ res.xls”,contentData);

其中contentData是一个具有excel表单数据的二维数组。