异常信息:
java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:75)
at com.eshore.framework.utils.html2word.HtmlDelUtil.resolveOnlyTextKlp(HtmlDelUtil.java:229)
at com.eshore.framework.utils.html2word.HtmlDelUtil.main(HtmlDelUtil.java:280)
在创建Workbook时使用HSSFWork读取xls文件后缀时出现过,切换为WorkbookFactory.creat(new FileInputStream("xx"));
异常信息:
Unable to read entire header; 0 bytes read; expected 512 bytes
方法代码如下:
private static void resolveOnlyTextKlp(){
String localFilePath = "E:\\extractKlp\\bak\\onlyTextKlp.xlsx";
int circleNum = 10;
int excelIndex = 1;
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream(new File(localFilePath));
output = new FileOutputStream(new File(localFilePath));
Workbook workbook = WorkbookFactory.create(input);
Sheet klpSheet = workbook.getSheetAt(0);
for(int i=0;i<circleNum;i++){
//追加至excel文档中
Row writeRow = klpSheet.createRow(excelIndex++);
Cell cell_1 = writeRow.createCell(0);
Cell cell_2 = writeRow.createCell(1);
Cell cell_3 = writeRow.createCell(2);
//知识标题
cell_1.setCellValue("标题oo"+i);
//知识类型
cell_2.setCellValue("类型oo"+i);
//知识内容
cell_3.setCellValue("内容oo"+i);
}
// output = new FileOutputStream(new File(localFilePath));
workbook.write(output);
output.flush();
}catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(input!=null)input.close();
if(output!=null)output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上面我同时创建输入输入流就会出现该错误Your InputStream was neither an OLE2 stream, nor an OOXML stream
解决办法:
将上面的output注释打开下面的output;即不在创建Workbook之前创建多个对同一个文件处理的流对象。
有错请指正,一同学习进步!