Exception in thread "main" java.lang.IllegalArgumentException: Blatt index (0) ist außerhalb des gültigen Bereichs (0..-1)

will ich Lesen Sie die excel 2010-Datei in java mit apache poi api ... aber es gibt mir eine Fehlermeldung
Exception in thread "main" java.lang.IllegalArgumentException: Blatt index (0) ist außerhalb des gültigen Bereichs (0..-1)
ich bin mit xssf
aber wenn ich möchte die Daten vom alten format von excel, dann funktioniert es einwandfrei durch singen HSSF ..
ich weiß nicht, was Los ist mit XSSF..hier ist mein code ..plz help me in.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 * This java program is used to read the data from a Excel file and display them
 * on the console output.
 * 
 * @author dhanago
 */
public class xssff {

    /** Creates a new instance of POIExcelReader */
    public xssff() {
    }

    /**
     * This method is used to display the Excel content to command line.
     * 
     * @param xlsPath
     */
    @SuppressWarnings("unchecked")
    public void displayFromExcel(String xlsPath) {
        InputStream inputStream = null;

        try {
            inputStream = new FileInputStream(xlsPath);
        } catch (FileNotFoundException e) {
            System.out.println("File not found in the specified path.");
            e.printStackTrace();
        }


        XSSFWorkbook workBook = new XSSFWorkbook();
        XSSFSheet sheet = workBook.getSheetAt(1);
        Iterator<Row> rows = sheet.rowIterator();

        while (rows.hasNext()) {
            XSSFRow row = (XSSFRow) rows.next();

            //display row number in the console.
            System.out.println();

            //once get a row its time to iterate through cells.
            Iterator<Cell> cells = row.cellIterator();

            while (cells.hasNext()) {
                XSSFCell cell = (XSSFCell) cells.next();

                /*
                 * Now we will get the cell type and display the values
                 * accordingly.
                 */
                switch (cell.getCellType()) {
                case XSSFCell.CELL_TYPE_NUMERIC: {

                    //cell type numeric.
                    System.out.print(cell.getNumericCellValue() + "\t\t\t");

                    break;
                }

                case XSSFCell.CELL_TYPE_STRING: {

                    //cell type string.
                    XSSFRichTextString richTextString = cell
                            .getRichStringCellValue();

                    System.out.print(richTextString.getString() + "\t\t\t");

                    break;
                }

                default: {

                    //types other than String and Numeric.
                    System.out.println("Type not supported.");

                    break;
                }
                }
            }
        }

    }

    /**
     * The main executable method to test displayFromExcel method.
     * 
     * @param args
     */
    public static void main(String[] args) {
        xssff poiExample = new xssff();
        String xlsPath = "c://temp//data.xlsx";

        poiExample.displayFromExcel(xlsPath);
    }
}
Sieht aus wie XSSFSheet sheet = workBook.getSheetAt(1); muss XSSFSheet sheet = workBook.getSheetAt(0);. Überprüfen Sie auch, ob Ihre Excel-Buch hat mehr als 1 Blatt.
noch es hat nicht funktioniert mit XSSFSheet sheet = workBook.getSheetAt(0) und meine excel-Buch hat nur 1 Blatt...

InformationsquelleAutor user1hjgjhgjhggjhg | 2012-07-02

Schreibe einen Kommentar