• 【Java-jxl插件】【Excel文件读写报错】jxl.read.biff.BiffException: Unable to recognize OLE stream


    问题:

    使用了两种方式读取excel,执行都报错:jxl.read.biff.BiffException: Unable to recognize OLE stream

    以下代码片段:

    package com.hlq.atm.excelCase;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    
    public class ExcelReadWrite {
    
        public static void main(String[] args) {
            ExcelReadWrite erw=new ExcelReadWrite();
            String path=System.getProperty("user.dir");
    //        erw.readExcelByJxl(path+"/data/Students.xls");
            erw.re(path+"/data/Students.xls");
        }
    
        public void re(String pathName) {
            try {
                //创建workbook
                Workbook workbook=Workbook.getWorkbook(new File(pathName));
                //获取第一个工作表sheet
                Sheet sheet=workbook.getSheet(0);
                //获取数据
                for (int i=0;i<sheet.getRows();i++){
                    for (int j=0;j<sheet.getColumns();j++){
                        Cell cell=sheet.getCell(j,i);
                        System.out.print(cell.getContents()+" ");
                    }
                    System.out.println();
                }
    
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (BiffException e) {
                e.printStackTrace();
            }
        }
        public void readExcelByJxl(String fileName) {
    File file=new File(fileName);
    try {
    // FileInputStream fis=new FileInputStream(file);
    Workbook workbook=Workbook.getWorkbook(file);
    Sheet[] sheet=workbook.getSheets();
    for (int i=0;i<sheet.length;i++){
    Sheet rs=workbook.getSheet(i);
    //遍历行
    for (int j=0;j<rs.getRows();j++){
    Cell[] cells=rs.getRow(j);
    //遍历列
    for (int z=0;z<cells.length;z++){
    System.out.print(cells[z].getContents());
    System.out.print(" ");
    }
    System.out.println();
    }
    }
    workbook.close();

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } catch (BiffException e) {
    e.printStackTrace();
    }
    }
    }

    原因:

    在网上查询改报错原因,是文件版本不兼容,jxl只支持excecl03版

    解决办法:

    尝试另存文件,选择Excel 97-2004工作簿,再次执行,执行通过

    执行结果:

  • 相关阅读:
    测序深度和覆盖度(Sequencing depth and coverage)
    让linux中 history显示每条命令的操作时间及操作用户【转】
    python报错IndexError: list index out of range
    优化MySQL的21个建议 – MySQL Life【转】
    Memcached实战之复制----基于repcached的主从【转】
    从xtrabackup备份恢复单表【转】
    binlog2sql的安装及使用
    mydumper安装及使用
    MySQL5.7 GTID在线开启与关闭【转】
    linux动态追踪神器——Strace实例介绍【转】
  • 原文地址:https://www.cnblogs.com/fatCat1/p/13060291.html
Copyright © 2020-2023  润新知