gpt4 book ai didi

java - 为什么此代码会生成无效的 Excel 文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:40:11 26 4
gpt4 key购买 nike

这会创建一个 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()

在关闭工作簿之前

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

}

关于java - 为什么此代码会生成无效的 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3870558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com