java– 来自Apache的文件Excel POI由Excel女士打开(腐败)

java– 来自Apache的文件Excel POI由Excel女士打开(腐败),第1张

概述我不知道为什么使用POI编写的文件不能由Excel2013打开,但POI仍然可以读取该文件.(单元格值可以更改)this是文件中的错误这是代码FileInputStreamfis=null;try{fis=newFileInputStream(fileUri);/oterroratfileUri}catch(FileNotFoundExcep

我不知道为什么使用POI编写的文件不能由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的文件Excel POI由Excel女士打开(腐败)全部内容,希望文章能够帮你解决java – 来自Apache的文件Excel POI由Excel女士打开(腐败)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1099909.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存