阅读背景:

从excel XSSFCell = row.getCell(j)读取空指针异常java;

来源:互联网 
private String[][] data;

// to read from excel file

public void readFile() throws IOException {

    // Path to the excel file
    // File datafile = new File("C:\Users\atomar\Documents\test.xlsx");
    // A Buffered File Input Stream to read the data
    FileInputStream f = new FileInputStream(
            "C:\Users\atomar\Documents\test.xlsx");
    System.out.println("file found");
    // InputStream fis = new BufferedInputStream(new FileInputStream("f"));
    // We create a workbook which represents the excel file
    XSSFWorkbook book = new XSSFWorkbook(f);

    // Next a sheet which represents the sheet within that excel file
    XSSFSheet sheet = book.getSheet("Sheet1");

    // No of rows in the sheet
    int rowNum = sheet.getLastRowNum() + 1;
    System.out.println("rowNum");

    // No of columns in the sheet
    int colNum = sheet.getRow(0).getLastCellNum();

    // A Two dimensional array of Strings which represents the data in the
    // sheet
    data = new String[rowNum][colNum];
    {

        for (int i = 0; i < rowNum; i++) {
            // Get the row
            XSSFRow row = sheet.getRow(i);

        for (int j = 0; j < colNum; j++) {
            // Get the columns or cells for the first row and keep
                // looping
                // for the other rows
        XSSFCell cell = row.getCell(j);

        // Make a call to the method cellToString which actually
                // converts the cell contents to String
                data[i][j] = cellToString(cell);

            //  data[i][j] = value;

    // Here is where you write the logic to handle the data.I am
                // just printing out the contents here.

                //System.out.println("The value is " + data[i][j]);


            }
        }
private String[][] data;

// to read from excel



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: