使用poi读取Excel文件并将其移动到二维数组

时间:2015-03-09 05:35:26

标签: java excel apache-poi

我正在使用POI读取excel文件并将其转换为二维数组。以下是代码部分:

import java.io.FileInputStream;

import org.apache.poi.ss.usermodel.CellValue;
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 XlsxRead_2 {

public XlsxRead_2(){
    getvalue_1();
}



public static void getvalue_1(){
    XSSFRow row;
    XSSFCell cell;
    String [][] value =null;
    double[][] nums =null;

    try {
        FileInputStream inputStream = new FileInputStream("TEST.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

        //get sheet number
        int sheetCn = workbook.getNumberOfSheets();

        for(int cn = 0; cn < sheetCn; cn++){

            //get 0th sheet data
            XSSFSheet sheet = workbook.getSheetAt(cn);

            //get number of rows from sheet
            int rows = sheet.getPhysicalNumberOfRows();

            //get number of cell from row
            int cells = sheet.getRow(cn).getPhysicalNumberOfCells(); 


            for (int r = 0; r < rows; r++) {
                row = sheet.getRow(r); // bring row
                if (row != null) {
                    for (int c = 0; c < cells; c++) {
                        cell = row.getCell(c);
                        value  = new String[rows][cells];
                        nums= new double [rows][cells];

                        if (cell != null) {

                            switch (cell.getCellType()) {                   

                            case XSSFCell.CELL_TYPE_FORMULA:
                                value [r][c]= cell.getCellFormula();
                                break;

                            case XSSFCell.CELL_TYPE_NUMERIC:
                                value [r][c]= "" + cell.getNumericCellValue();
                                break;

                            case XSSFCell.CELL_TYPE_STRING:
                                value [r][c]= "" + cell.getStringCellValue();
                                break;

                            case XSSFCell.CELL_TYPE_BLANK:
                                value [r][c]= "[BLANK]";
                                break;

                            case XSSFCell.CELL_TYPE_ERROR:
                                value [r][c]= "" + cell.getErrorCellValue();
                                break;
                            default:                    
                            }                               
                            System.out.print(value);

                        } else {
                            System.out.print("[null]\t");
                        }
                    } // for(c) 
                    System.out.print("\n");
                }
            } // for(r) 
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}


public class Starter {

public static void main(String[] args) {


    XlsxRead_2 gv=new XlsxRead_2();
}

}

但是,它无法正常工作。附件是JAVA的错误结果。当我不使用数组工作时,它工作正常。任何建议表示赞赏。

Result in java:
[[Ljava.lang.String;@167fdd33[[Ljava.lang.String;@1e965684
[[Ljava.lang.String;@4d95d2a2[[Ljava.lang.String;@53f65459
[[Ljava.lang.String;@3b088d51[[Ljava.lang.String;@1786dec2
[[Ljava.lang.String;@74650e52[[Ljava.lang.String;@15d0c81b
[[Ljava.lang.String;@6acdbdf5[[Ljava.lang.String;@4b1c1ea0
[[Ljava.lang.String;@3712b94[[Ljava.lang.String;@2833cc44
[[Ljava.lang.String;@33f88ab[[Ljava.lang.String;@27a8c74e

1 个答案:

答案 0 :(得分:2)

你应该在第一个for循环之后创建数组。您还应该为每个工作表创建值数组。还有一点,你应该打印值[r] [c]而不是值 我希望能帮助你。

public class XlsxRead_2 {

   public static void main(String[] args) {
       XlsxRead_2 xread2 = new XlsxRead_2();
   }

   public XlsxRead_2() {
       getvalue_1();
   }

   public static void getvalue_1() {
       XSSFRow row;
       XSSFCell cell;
       String[][] value = null;
       double[][] nums = null;

       try {
           FileInputStream inputStream = new FileInputStream("TEST.xlsx");
           XSSFWorkbook workbook = new XSSFWorkbook(inputStream);

           // get sheet number
           int sheetCn = workbook.getNumberOfSheets();

           for (int cn = 0; cn < sheetCn; cn++) {

               // get 0th sheet data
               XSSFSheet sheet = workbook.getSheetAt(cn);

               // get number of rows from sheet
               int rows = sheet.getPhysicalNumberOfRows();

               // get number of cell from row
               int cells = sheet.getRow(cn).getPhysicalNumberOfCells();

               value = new String[rows][cells];

               for (int r = 0; r < rows; r++) {
                   row = sheet.getRow(r); // bring row
                   if (row != null) {
                       for (int c = 0; c < cells; c++) {
                           cell = row.getCell(c);
                           nums = new double[rows][cells];

                           if (cell != null) {

                               switch (cell.getCellType()) {

                               case XSSFCell.CELL_TYPE_FORMULA:
                                   value[r][c] = cell.getCellFormula();
                                   break;

                               case XSSFCell.CELL_TYPE_NUMERIC:
                                   value[r][c] = ""
                                        + cell.getNumericCellValue();
                                   break;

                               case XSSFCell.CELL_TYPE_STRING:
                                   value[r][c] = ""
                                        + cell.getStringCellValue();
                                   break;

                               case XSSFCell.CELL_TYPE_BLANK:
                                  value[r][c] = "[BLANK]";
                                  break;

                               case XSSFCell.CELL_TYPE_ERROR:
                                  value[r][c] = ""+cell.getErrorCellValue();
                                break;
                            default:
                            }
                            System.out.print(value[r][c]);

                        } else {
                            System.out.print("[null]\t");
                        }
                    } // for(c)
                    System.out.print("\n");
                }
            } // for(r)
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}