今天在做poi修改样式时,报了以下错误:

Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:148)
at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:199)
at com.supcon.ChangeXlsxCell.main(ChangeXlsxCell.java:29)
Caused by: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
at org.apache.poi.openxml4j.opc.PackagePart.getInputStream(PackagePart.java:502)
at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:75)
at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:146)
... 2 more

网上查了相关文档,没找到相关资料,也没有给出解决资料,绝望之余,网上查找了poi修改数据的代码,运行了下竟然可以通过,以下为代码:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class ChangeCell { @SuppressWarnings("deprecation")
public static void main(String[] args) {
String fileToBeRead = "C:\\exp.xls"; // excel位置
int coloum = 1; // 比如你要获取第1列
try {
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
fileToBeRead));
HSSFSheet sheet = workbook.getSheet("Sheet1"); for (int i = 0; i <= sheet.getLastRowNum(); i++) {
HSSFRow row = sheet.getRow((short) i);
if (null == row) {
continue;
} else {
HSSFCell cell = row.getCell((short) coloum);
if (null == cell) {
continue;
} else {
System.out.println(cell.getNumericCellValue());
int temp = (int) cell.getNumericCellValue();
cell.setCellValue(temp + 1);
}
}
}
FileOutputStream out = null;
try {
out = new FileOutputStream(fileToBeRead);
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} } }

我的错误代码如下:

String fileToBeRead = "D:\\template.xlsx"; // excel位置
File dataFile=new File(fileToBeRead);
try {
XSSFWorkbook workbook = new XSSFWorkbook(dataFile);
XSSFSheet sheet = workbook.getSheet("Sheet1");
FileOutputStream out = null;
try {
out = new FileOutputStream(fileToBeRead);
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

简单比较以下差异处在XSSFWorkbook初始化的时候:

XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));(正确的)

XSSFWorkbook workbook = new XSSFWorkbook(dataFiel);(错误的)

错误的描述是无法获得输入流: can  not  obtain  input  stream  for  xml....

是不是一个outputStream一定要对应一个InputStream呢?????

以上错误代码修改如下,即可通过:

String fileToBeRead = "D:\\template.xlsx"; // excel位置
File dataFile=new File(fileToBeRead);
try {
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(fileToBeRead));
XSSFSheet sheet = workbook.getSheet("Sheet1");
FileOutputStream out = null;
try {
out = new FileOutputStream(fileToBeRead);
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

最新文章

  1. 使用WKWebView替换UIWebView
  2. xamarin.android之 Android 4.4+ 获取图片真实路径
  3. Oracle11G安装之后
  4. UVALive 4682 XOR Sum (trie)
  5. Objective-C的基础语法总结
  6. Unity3D研究院之静态自动检查代码缺陷与隐患
  7. 使用 PHP 验证表单数据
  8. linux管理网络连接指令
  9. 对有状态bean和无状态bean的理解(转)
  10. iOS 打开系统设置
  11. Vue安装依赖npm install时报错问题解决方法
  12. 1、python基础
  13. Python&#160;基于Python实现的ssh兼sftp客户端(下)
  14. 使用日志服务进行Kubernetes日志采集
  15. JS两个页面通过URL传值
  16. Android-Recyclerview的简单使用
  17. C# 连接 sql server
  18. 音频播放时出现 Uncaught (in promise) DOMException: play() failed because the user didn&#39;t interact with the document first. https://goo.gl/xX8pDD
  19. html5+css3图片旋转特效
  20. Hadoop创建新用户

热门文章

  1. a标签点击后的虚线框问题
  2. WPF 中 TreeListView 的使用
  3. 五、mysql存储引擎
  4. Could not retrieve mirrorlist http://mirrorlist.centos.org || PYCURL ERROR 6
  5. HTML 背景图片自适应
  6. PS 颜色表大全-CMYK颜色表(2)
  7. iOS通过http post上传图片 (转)
  8. 轻仿QQ音乐之音频歌词播放、锁屏歌词-b
  9. UINavigationController 总结
  10. 使用nodejs搭建服务器显示HTML页面