gpt4 book ai didi

java - 来自 Apache POI 的文件 Excel 无法由 Ms Excel 打开(损坏)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:36:55 25 4
gpt4 key购买 nike

不知道为什么我用POI写的文件用Ms Excel 2013打不开,但是POI还是可以读取的。 (单元格值可以更改)

this是来自文件的错误

这是代码

FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri); //not error at fileUri
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String urii = fileUri.replace(".xls", "0.xls"); //not error
File fisx = new File(urii);

Workbook workbook = null;
workbook = new HSSFWorkbook(fis);

Sheet sheet = workbook.getSheetAt(0);

Row row = sheet.getRow(0);

Cell cell = row.getCell(0);

String p = cell.getStringCellValue();

TextView a = (TextView) findViewById(R.id.txtUri);

cell.setCellValue(new String("popo"));
String x = cell.getStringCellValue();

TextView b = (TextView) findViewById(R.id.txtFile);

a.setText(p);
b.setText(x);

OutputStream fos = null;

fos = new FileOutputStream(fisx);
workbook.write(fos); //main problem
fos.flush();
fos.close();

感谢您的帮助!

最佳答案

您的代码有两个问题。首先是:

FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri);

Apache POI Docs, don't use an InputStream if you have a File! 中所述

其次,这个:

 Workbook workbook = null;
workbook = new HSSFWorkbook(fis);

这仅适用于 .xls 文件,不适用于 .xlsx 文件。相反,您需要使用 WorkbookFactory它标识类型并为您提供适合该格式的正确工作簿

所以,把你的代码改成

File file = new File(fileUri);
Workbook workbook = WorkbookFactory.create(file);

关于java - 来自 Apache POI 的文件 Excel 无法由 Ms Excel 打开(损坏),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30397347/

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