今天在做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();
}

Can't obtain the input stream from /docProps/app.xml的更多相关文章

  1. Could not parse mapping document from input stream hibernate配置异常

    十二月 , :: 下午 org.apache.catalina.core.StandardContext listenerStart SEVERE: Exception sending context ...

  2. Xcode8出现AQDefaultDevice (173): skipping input stream 0 0 0x0

    一直不想升级Xcode,但是没办法项目进度只能升级Xcode8,果然不出所料出现了不少bug, Xcode7运行一直没有问题,但是在Xcode8上一直输出AQDefaultDevice (173): ...

  3. 提问:MicrosoftUnderlying input stream returned zero bytes

    报错信息:MicrosoftUnderlying input stream returned zero bytes 报错截图: 查阅资料后,提示 jdbc的bug,不能将一个"NULL&qu ...

  4. 关于hibernate总是报错 配置factory的id找不到,mapping配置文件Could not parse mapping document from input stream

    Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream ...

  5. hive查询遇到java.io.EOFException: Unexpected end of input stream错误

    hive查询遇到java.io.EOFException: Unexpected end of input stream错误 原因基本上有两个: 空文件 不完整的文件 解决办法: 删除对应文件- 参考 ...

  6. tomcat 启动异常 EOFException: Unexpected end of ZLIB input stream

    EVERE: Exception fixing docBase for context [/agdis] java.io .EOFException: Unexpected end of ZLIB i ...

  7. The input stream is not a valid binary format.

    The input stream is not a valid binary format. The starting contents (in bytes) are: 53-79-73-74-65- ...

  8. hadoop java.io.EOFException: Unexpected end of input stream

    执行hadoop 报错 java.io.EOFException: Unexpected end of input stream at org.apache.hadoop.io.compress.De ...

  9. hadoop异常: java.io.EOFException: Unexpected end of input stream

    执行hadoop任务时报错: -- ::, INFO [main] org.apache.hadoop.mapred.MapTask: Processing --//app1@flume23_1000 ...

随机推荐

  1. sublime 修改Xdebug插件快捷键

    最近在用Xdebug插件 感觉挺好用 但是快捷键不太舒服,特别是调试下一步的时候,比较麻烦,按键较多: 所以想DIY下 但是preferences->package setting ->X ...

  2. Eclipse查看源码乱码问题

    在同一个工作区下,依赖的Jar包如果既有GBK编码,又有UTF-8编码,就会出现的问题. 常规解决方法 1.切换工作区编码.麻烦,效率低. 2.设置Jar包编码,只有Eclipse4.2支持,4.0以 ...

  3. ubuntu下安装ffmpeg和X264

    第一步:安装必要的库 $:-dev libtheora-dev libx11-dev zlib1g-dev 第二步:安装SDL(否则可能编译不出ffplay) $:-dev $:-dev libsdl ...

  4. mac 下 sphinx + mysql + php 实现全文搜索(xampp)(3)sphinx 的配置项解析

    source:数据的来源,数据是从什么地方来的. index:索引,当有数据源之后,从数据源处构建索引.索引实际上就是相当于一个字典检索.有了整本字典内容以后,才会有字典检索. searchd:提供搜 ...

  5. C# 将datatable 转换json

    public static string DataTableToJson(DataTable dt) { StringBuilder jsonBuilder = new StringBuilder() ...

  6. codeblocks常用快捷键

    CodeBlocks常用操作快捷键编辑部分:Ctrl + A:全选Ctrl + C:复制Ctrl + X: 剪切Ctrl + V:粘贴Ctrl + Z:撤销Ctrl + S:保存Ctrl + Y / ...

  7. dive into python 读笔(2)

    chapter 4 自省, summary: # 用可选和命名参数定义和调用函数 # 用 str 强制转换任意值为字符串形式 # 用 getattr 动态得到函数和其它属性的引用 # 扩展列表解析语法 ...

  8. eclipse *.vm 文件,语法高亮

    eclipse *.vm 文件,语法高亮按如下方式不起作用,原因不明.设置文件打开时使用的编辑器General>>Editors>>File Associations 下述方试 ...

  9. Notepad++ 书签

      Notepad++,有一个书签功能,指定书签是Ctrl+F2,在书签之间移动是按F2来切换,这个可以在几个想查看的数据之间进行快速切换,所以看起来就很方便.

  10. HTML <iframe> 标签

    参考地址:http://www.w3school.com.cn/tags/tag_iframe.asp ------------------------------------------------ ...