Java 如何将值存储在二维数组中以供以后使用?

Java 如何将值存储在二维数组中以供以后使用?,java,arrays,multidimensional-array,apache-poi,Java,Arrays,Multidimensional Array,Apache Poi,我正在使用ApachePOI将数据从excel文件读取到2D数组中。我认为我的错误在于我的数组代码,而不是POI元素。在方法运行后,我不能使用任何数组数据,它都是空的 public class ExcelRead { public static void main(String[] args) throws Exception { File excel = new File ("C:/Users/user/Desktop/Files/Dashboards.xlsx"); Fil

我正在使用ApachePOI将数据从excel文件读取到2D数组中。我认为我的错误在于我的数组代码,而不是POI元素。在方法运行后,我不能使用任何数组数据,它都是空的

public class ExcelRead {

public static void main(String[] args) throws Exception {
    File excel = new File ("C:/Users/user/Desktop/Files/Dashboards.xlsx");
    FileInputStream fis = new FileInputStream(excel);

    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sheet = wb.getSheetAt(0);

    int rowNum = sheet.getLastRowNum()+1;
    int colNum = sheet.getRow(0).getLastCellNum();
    String[][] 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++){
                //this gets the cell and sets it as blank if it's empty.
                XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
                String value = String.valueOf(cell);                             
                System.out.println("Value: " + value);

            }            
       }
    System.out.println("End Value:  " + data[2][2]);
    }
}

因此,它读取的数据很好。但是不存储数据,因为当我试图调用一个单元格供以后使用时,它是空的。我的第一个输出(值:“”)是否打印每次通过数组迭代的结果?我不确定这是否有意义,但它似乎是在读取Excel文件,一个接一个地吐出单元格,但没有存储它们供我以后使用。

您只是在循环项目,而不是将它们保存在数组中

要将其保存到阵列中,您需要执行以下操作:

array[i][j] = value;
就你而言:

for (int i=0; i<rowNum; i++){
        //get the row
        XSSFRow row = sheet.getRow(i);
            for (int j=0; j<colNum; j++){
                //this gets the cell and sets it as blank if it's empty.
                XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
                String value = String.valueOf(cell);                             
                System.out.println("Value: " + value);
                data[i][j] = value;
            }            
       }

for(inti=0;i您只是循环遍历这些项,而不是将它们保存在数组中

要将其保存到阵列中,您需要执行以下操作:

array[i][j] = value;
就你而言:

for (int i=0; i<rowNum; i++){
        //get the row
        XSSFRow row = sheet.getRow(i);
            for (int j=0; j<colNum; j++){
                //this gets the cell and sets it as blank if it's empty.
                XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
                String value = String.valueOf(cell);                             
                System.out.println("Value: " + value);
                data[i][j] = value;
            }            
       }

for(inti=0;i您只是循环遍历这些项,而不是将它们保存在数组中

要将其保存到阵列中,您需要执行以下操作:

array[i][j] = value;
就你而言:

for (int i=0; i<rowNum; i++){
        //get the row
        XSSFRow row = sheet.getRow(i);
            for (int j=0; j<colNum; j++){
                //this gets the cell and sets it as blank if it's empty.
                XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
                String value = String.valueOf(cell);                             
                System.out.println("Value: " + value);
                data[i][j] = value;
            }            
       }

for(inti=0;i您只是循环遍历这些项,而不是将它们保存在数组中

要将其保存到阵列中,您需要执行以下操作:

array[i][j] = value;
就你而言:

for (int i=0; i<rowNum; i++){
        //get the row
        XSSFRow row = sheet.getRow(i);
            for (int j=0; j<colNum; j++){
                //this gets the cell and sets it as blank if it's empty.
                XSSFCell cell = row.getCell(j, Row.CREATE_NULL_AS_BLANK);
                String value = String.valueOf(cell);                             
                System.out.println("Value: " + value);
                data[i][j] = value;
            }            
       }

for(int i=0;i,因为数据数组不包含任何内容。从excel文件读取时,您忘记将该数据添加到数组中。如果未初始化,字符串的默认值为
null

考虑在
System.out.println(“value:+value”);
之后添加
data[i][j]=value;


您将获得所需的输出。

因为数据数组不包含任何内容。从excel文件读取时,您忘记将该数据添加到数组中。如果未初始化,字符串的默认值为
null

考虑在
System.out.println(“value:+value”);
之后添加
data[i][j]=value;


您将获得所需的输出。

因为数据数组不包含任何内容。从excel文件读取时,您忘记将该数据添加到数组中。如果未初始化,字符串的默认值为
null

考虑在
System.out.println(“value:+value”);
之后添加
data[i][j]=value;


您将获得所需的输出。

因为数据数组不包含任何内容。从excel文件读取时,您忘记将该数据添加到数组中。如果未初始化,字符串的默认值为
null

考虑在
System.out.println(“value:+value”);
之后添加
data[i][j]=value;


您将获得所需的输出。

在我看来,您并没有将任何内容放入数组中的任何位置。没有任何东西像
数据[i][j]=…
在你的代码中。你把单元格值放在
值中,而不是放在
数据中。在我看来,你没有把任何东西放在数组的任何地方。没有任何东西像
数据[i][j]=…
在你的代码中。你把单元格值放在
值中,而不是放在
数据中。在我看来,你没有把任何东西放在数组的任何地方。没有任何东西像
数据[i][j]=…
在你的代码中。你把单元格值放在
值中,而不是放在
数据中。在我看来,你没有把任何东西放在数组的任何地方。没有任何东西像
数据[i][j]=…
在你的代码中。你把单元格的值放在
值中,而不是放在
数据中。这起作用了,谢谢一堆我是新来的和Java的新手。这起作用了,谢谢一堆我是新来的和Java的新手。这起作用了,谢谢一堆我是新来的和Java的。这起作用了,谢谢一堆我是新来的和Java的。