来自 Apache POI 的 Excel 文件无法由 Excel 女士打开(损坏) [英] File Excel From Apache POI Cant Open by Ms Excel (corrupt)
本文介绍了来自 Apache POI 的 Excel 文件无法由 Excel 女士打开(损坏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知道为什么我用POI写的文件Ms Excel 2013打不开,但是POI还是可以读取文件的.(单元格值可以更改)
I don't know why the file I write using POI cant be opened by Ms Excel 2013, but the file is still readable by POI. (cell value can be changed)
这个是来自文件的错误
这是代码
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();
感谢您的帮助!!
推荐答案
您的代码有两个问题.首先这个:
There are two issues with your code. Firstly this:
FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri);
其次,这个:
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
这仅适用于 .xls
文件,不适用于 .xlsx
文件.相反,您需要使用 WorkbookFactory 来标识类型并为您提供适合该格式的工作簿
That will only work for .xls
files, not for .xlsx
ones. Instead, you need to use WorkbookFactory which identifies the type and gives you the right workbook for the format
因此,将您的代码更改为
So, change your code to be
File file = new File(fileUri);
Workbook workbook = WorkbookFactory.create(file);
这篇关于来自 Apache POI 的 Excel 文件无法由 Excel 女士打开(损坏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文