簡體   English   中英

如何解決java.io.IOException:使用apache POI讀取Excel文件時出現讀取錯誤

[英]How to solve java.io.IOException: Read error when reading Excel file using apache POI

我正在嘗試使用Apache POI讀取Excel文件,但出現讀取錯誤異常。

public class ReadExcelFileToList {

public static void main(String[] args) throws IOException {

    InputStream input = null;

    try {

        input = new FileInputStream(
                "C:\\Users\\jeet.chatterjee\\Downloads\\Book1.xls");

        System.out.println("file is found");
        POIFSFileSystem fs = new POIFSFileSystem(input);
        XSSFWorkbook wb = new XSSFWorkbook(input);
        XSSFSheet sheet = wb.getSheetAt(0);

        Iterator rows = sheet.rowIterator();
        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();
            System.out.println("\n");
            Iterator cells = row.cellIterator();
            while (cells.hasNext()) {

                XSSFCell cell = (XSSFCell) cells.next();
                if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");
                else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType())
                    System.out.print(cell.getStringCellValue() + "     ");

                else
                    System.out.print("Unknown cell type");

            }

        }

    } catch (IOException ex) {

        ex.printStackTrace();
    }

    }

}

異常日志是

 file is found
     java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at java.io.FilterInputStream.read(Unknown Source)
at java.io.PushbackInputStream.read(Unknown Source)
at java.util.zip.ZipInputStream.readFully(Unknown Source)
at java.util.zip.ZipInputStream.readLOC(Unknown Source)
at java.util.zip.ZipInputStream.getNextEntry(Unknown Source)
at org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource.<init>(ZipInputStreamZipEntrySource.java:51)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:83)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:228)
at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:187)
at com.mj.test.ReadExcelFileToList.main(ReadExcelFileToList.java:32)

嘗試讀取此Excel文件時收到此錯誤。

您在代碼中使用了相同的輸入流多次。即,您多次讀取了相同的輸入流。 這就是引發此錯誤的原因。 您需要重新創建流。

 input = new FileInputStream("C:\\Users\\jeet.chatterjee\\Downloads\\Book1.xls");
 //Using same inputstream is not correct
 //Comment the below line
 //POIFSFileSystem fs = new POIFSFileSystem(input);
 XSSFWorkbook wb = new XSSFWorkbook(input);

附帶說明,您必須在使用流后關閉流。 我在您的代碼中看不到它。

我也嘗試相同的事情..您應該嘗試這個..我的優勢是data.xlsx。 您應該使用FileInputStream而不是POIFSFileSystem

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Iterator; 
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class JavaApplication3 {

    public static void main(String[] args) {
        try {
           FileInputStream file = new FileInputStream(new File("E:/data.xlsx"));
           XSSFWorkbook wb = new XSSFWorkbook(file);
           XSSFSheet sheet = wb.getSheetAt(0);
           Iterator rows = sheet.rowIterator();
           while (rows.hasNext()) {
             XSSFRow row = (XSSFRow) rows.next();
             System.out.println("\n");
             Iterator cells = row.cellIterator();
             while (cells.hasNext()) {

                XSSFCell cell = (XSSFCell) cells.next();
                if (XSSFCell.CELL_TYPE_NUMERIC == cell.getCellType()) {
                    System.out.print(cell.getNumericCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_STRING == cell.getCellType()) {
                    System.out.print(cell.getStringCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_BOOLEAN == cell.getCellType()) {
                    System.out.print(cell.getBooleanCellValue() + "     ");
                } else if (XSSFCell.CELL_TYPE_BLANK == cell.getCellType()) {
                    System.out.print("BLANK     ");
                } else {
                    System.out.print("Unknown cell type");
                }
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM