org.apache.poi.openxml4j.exceptions.InvalidOperationException:无法打开指定的文件 [英] org.apache.poi.openxml4j.exceptions.InvalidOperationException: Can't open the specified file

查看:4716
本文介绍了org.apache.poi.openxml4j.exceptions.InvalidOperationException:无法打开指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code不能正常工作,它总是显示上述异常。
但我总能看到正在生成的tmp文件。

这里是code,可有人请建议的内容:

 的FileInputStream FIS = NULL;
        尝试{
        FIS =新的FileInputStream(新文件(/家庭/阿马尔/桌面/新/ abc.xls));        工作簿WB =新org.apache.poi.xssf.usermodel.XSSFWorkbook(FIS);
                INT numOfSheets = wb.getNumberOfSheets();
的System.out.println(片材的bhargo num是+ numOfSheets);
        的for(int i = 0; I< numOfSheets;我++){
            org.apache.poi.ss.usermodel.Sheet片= wb.getSheetAt(ⅰ);
            迭代器<行> rowIterator = sheet.iterator();
            而(rowIterator.hasNext())
            {
                鳞次栉比= rowIterator.next();
                迭代&所述;电池> cellIterator = row.cellIterator();
                而(cellIterator.hasNext()){
                    细胞;细胞=(Cell)的cellIterator.next();
                    如果(cell.getCellType()== cell.CELL_TYPE_STRING){
                        的System.out.println(bhargo单元格值+ cell.getStringCellValue()修剪());
                    }
                }
            }
            }
    }赶上(例外五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    最后{
        的System.out.println(bhargo,关闭流);
    尝试{
        fis.close();
    }赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    }


解决方案

有一些问题在这里:

  FIS =新的FileInputStream(新文件(/家庭/阿马尔/桌面/新/ abc.xls));
    工作簿WB =新org.apache.poi.xssf.usermodel.XSSFWorkbook(FIS);

首先,如在Apache POI文档解释,不要使用,如果你的InputStream有一个文件!它的速度较慢,并使用更多的内存

其次,XSSF是code与的.xlsx 文件的工作,但你的文件是一个的.xls 之一,所以这是行不通的。

第三,Apache的POI有code,它会自动找出你的是什么样的文件,并为您创建相应的工作簿

因此​​,您code应改为为

 工作簿WB = WorkbookFactory.create(新文件(/家庭/阿马尔/桌面/新/ abc.xls));

这将创建正确的工作簿,从文件直接

My code is not working, it always shows the above mentioned exception. but I can always see the tmp file being generated.

here is the code, can someone please suggest something:

FileInputStream fis =null;
        try{
        fis= new FileInputStream(new File("/home/amar/Desktop/new/abc.xls"));

        Workbook wb = new org.apache.poi.xssf.usermodel.XSSFWorkbook(fis);
                int numOfSheets = wb.getNumberOfSheets();
System.out.println("bhargo num of sheets is " + numOfSheets);
        for(int i=0; i<numOfSheets; i++){
            org.apache.poi.ss.usermodel.Sheet sheet = wb.getSheetAt(i);
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext())
            {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();
                while (cellIterator.hasNext()) {
                    Cell cell = (Cell) cellIterator.next();
                    if (cell.getCellType() == cell.CELL_TYPE_STRING) {
                        System.out.println("bhargo cell value is " + cell.getStringCellValue().trim());
                    }
                }
            }
            }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally{
        System.out.println("bhargo, closing the stream");
    try {
        fis.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }

解决方案

There are a number of issues here:

    fis= new FileInputStream(new File("/home/amar/Desktop/new/abc.xls"));
    Workbook wb = new org.apache.poi.xssf.usermodel.XSSFWorkbook(fis);

Firstly, as explained in the Apache POI documentation, don't use an InputStream if you have a file! It's slower and uses more memory

Secondly, XSSF is the code for working with .xlsx files, but your file is a .xls one, so that won't work.

Thirdly, Apache POI has code which will automatically work out what kind of file yours is, and create the appropriate workbook for you

Your code should therefore instead be

Workbook wb = WorkbookFactory.create(new File("/home/amar/Desktop/new/abc.xls"));

This will create the right kind of workbook, direct from the file

这篇关于org.apache.poi.openxml4j.exceptions.InvalidOperationException:无法打开指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆