jxl.read.biff。当读取.xls和.xlsx格式的excel文件时,会出现BiffException异常

时间:2022-08-25 20:25:48

Using below code, i'm getting jxl.read.biff.BiffException: Unable to recognize OLE stream. I want to use .xls and .xlsx format files. how to solve this?

使用下面的代码,我得到了jx .read.biff。不能识别OLE流。我想使用.xls和.xlsx格式文件。如何解决呢?

Service.java

Service.java

@Override
public boolean facultyDump(String path, HttpSession httpSession) {
    Session session=sessionFactory.openSession();
    session.beginTransaction();
    File inputWorkbook = new File(path);
    Workbook w;
    try{
        w = Workbook.getWorkbook(inputWorkbook);
        Sheet sheet = w.getSheet(0);
        for (int i = 1; i < sheet.getRows(); i++) {
            for (int j = 0; j < sheet.getColumns(); j++) {
                Cell cell = sheet.getCell(j, i);
                if (j == 0) {
                    String name= cell.getContents().trim();
                } 
            }
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        session.close();sessionFactory.close();
    }
    return false;
}

Exception at Console:

在控制台异常:

jxl.read.biff.BiffException: Unable to recognize OLE stream
at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
at jxl.read.biff.File.<init>(File.java:127)
at jxl.Workbook.getWorkbook(Workbook.java:221)
at jxl.Workbook.getWorkbook(Workbook.java:198)
at com.slv.daoimpl.RegistrationDaoImpl.facultyDump(RegistrationDaoImpl.java:2845)
at com.slv.controller.SuperAdminController.facultyDumpExcel(SuperAdminController.java:327)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)

2 个解决方案

#1


1  

@Override
public boolean facultyDump(String path, HttpSession httpSession) {
    Session session=sessionFactory.openSession();
    session.beginTransaction();
    File inputWorkbook = new File(path);
    try{
        FileInputStream fis = new FileInputStream(inputWorkbook);
        org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(fis);
        org.apache.poi.ss.usermodel.Sheet mySheet = workbook.getSheetAt(0);
         Iterator<Row> rowIterator = mySheet.iterator();
         while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue()+ "\t") ;
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default :
                    }
                }
            }   
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        session.close();sessionFactory.close();
    }
    return false;
}

#2


0  

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;

try{
    FileInputStream file = new FileInputStream(new File("C:\\test.xlsx"));

    //Get the workbook instance for XLS file 
    XSSFWorkbook workbook = new XSSFWorkbook (file);

    //Get first sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);

    //Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = sheet.iterator();

    //Get iterator to all cells of current row
    Iterator<Cell> cellIterator = row.cellIterator();

     while(cellIterator.hasNext()) {
            Cell cell = cellIterator.next();    
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
       }
    file.close();
}catch(Exception e){
    e.printStackTrace();
}

#1


1  

@Override
public boolean facultyDump(String path, HttpSession httpSession) {
    Session session=sessionFactory.openSession();
    session.beginTransaction();
    File inputWorkbook = new File(path);
    try{
        FileInputStream fis = new FileInputStream(inputWorkbook);
        org.apache.poi.ss.usermodel.Workbook workbook = WorkbookFactory.create(fis);
        org.apache.poi.ss.usermodel.Sheet mySheet = workbook.getSheetAt(0);
         Iterator<Row> rowIterator = mySheet.iterator();
         while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();
                    switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_STRING:
                        System.out.print(cell.getStringCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        System.out.print(cell.getNumericCellValue()+ "\t") ;
                        break;
                    case Cell.CELL_TYPE_BOOLEAN:
                        System.out.print(cell.getBooleanCellValue() + "\t");
                        break;
                    default :
                    }
                }
            }   
    }catch(Exception ex){
        ex.printStackTrace();
    }finally{
        session.close();sessionFactory.close();
    }
    return false;
}

#2


0  

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFSheet;

try{
    FileInputStream file = new FileInputStream(new File("C:\\test.xlsx"));

    //Get the workbook instance for XLS file 
    XSSFWorkbook workbook = new XSSFWorkbook (file);

    //Get first sheet from the workbook
    XSSFSheet sheet = workbook.getSheetAt(0);

    //Get iterator to all the rows in current sheet
    Iterator<Row> rowIterator = sheet.iterator();

    //Get iterator to all cells of current row
    Iterator<Cell> cellIterator = row.cellIterator();

     while(cellIterator.hasNext()) {
            Cell cell = cellIterator.next();    
            switch(cell.getCellType()) {
                case Cell.CELL_TYPE_BOOLEAN:
                    System.out.print(cell.getBooleanCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_NUMERIC:
                    System.out.print(cell.getNumericCellValue() + "\t\t");
                    break;
                case Cell.CELL_TYPE_STRING:
                    System.out.print(cell.getStringCellValue() + "\t\t");
                    break;
            }
       }
    file.close();
}catch(Exception e){
    e.printStackTrace();
}