ket*_*eng 10 java excel android apache-poi
我不知道为什么使用POI编写的文件不能由Excel 2013打开,但POI仍然可以读取该文件.(单元格值可以更改)
这是文件中的错误
这是代码
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();
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!!
您的代码有两个问题.首先是:
FileInputStream fis = null;
try {
fis = new FileInputStream(fileUri);
Run Code Online (Sandbox Code Playgroud)
如Apache POI Docs中所述,如果您有文件,请不要使用InputStream!
其次,这个:
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
Run Code Online (Sandbox Code Playgroud)
这只适用于.xls文件,而不适用于文件.xlsx.相反,您需要使用WorkbookFactory来识别类型并为您提供格式的正确工作簿
所以,改变你的代码
File file = new File(fileUri);
Workbook workbook = WorkbookFactory.create(file);
Run Code Online (Sandbox Code Playgroud)
我在这里看到的主要问题是:
Workbook workbook = null;
workbook = new HSSFWorkbook(fis);
Run Code Online (Sandbox Code Playgroud)
相反,你必须使用:
Workbook workbook = null;
workbook = new XSSFWorkbook(fis);
Run Code Online (Sandbox Code Playgroud)
MS EXCEL 2013 可读。
| 归档时间: |
|
| 查看次数: |
11791 次 |
| 最近记录: |