今天在做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. 【Delphi】窗体阴影

    procedure TForm1.FormCreate(Sender: TObject); begin SetClassLong(Handle, GCL_STYLE, GetClassLong(Han ...

  2. makefile:4: *** missing separator. Stop.

    今天在编写蜂鸣器的驱动程序时,makefile文件是这样: CROSS=arm-linux- all: beep beep: beep.c $(CROSS)gcc -o beep beep.c $(C ...

  3. 【转】How to view word document in WPF application

    How to view word document in WPF application (CSVSTOViewWordInWPF) Introduction The Sample demonstra ...

  4. insert into (select...WITH CHECK OPTION) values(...)

    insert into (<subquery> WITH CHECK OPTION) values (...) 语法看起来很特殊,其实是insert进subquery的这张表里: 1. 只 ...

  5. MySQL --log-slave-updates

     官方说明:--log-slave-updates Command-Line Format --log-slave-updates Option-File Format log-slave-updat ...

  6. Xcode界面简介

    创建 Object-C 程序 进入主界面方式 : 打开 Xcode 之后, 可以使用 Xcode 菜单, 但是必须创建一个 Object-C 工程 或者 导入 一个 Object-C 工程才可以进入主 ...

  7. jQuery树结构插件推荐zTree

    JQuery zTree 下载地址http://plugins.jquery.com/zTree.v3/

  8. hdu 4190

    二分求箱子中的票数  然后判是否满足条件     主要为了纪念一下用优先队列9000ms水过 #include<cstdio> #include<climits> #inclu ...

  9. MCM试题原文及翻译 AB题 2014美国数学建模竞赛

    MCM试题原文及翻译 AB题 2014美国数学建模竞赛 原创翻译,如有瑕疵,敬请谅解. 转载请注明:过客小站 » MCM试题原文及翻译 AB题 2014美国数学建模竞赛 PROBLEM A: The  ...

  10. HDU1465+递推

    经典的信封装信问题 f[ n ]  = ( n-1 ) * ( f[ n-1 ]+f[ n-2 ] ) #include<stdio.h> #include<string.h> ...