java.lang.IllegalArgumentException: Your InputStream was neither an OLE2 stream, nor an OOXML stream

异常信息:
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之前创建多个对同一个文件处理的流对象。
    有错请指正,一同学习进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值