简体   繁体   English

尝试从Excel中获取数据,然后将其存储在数组中,然后再打印

[英]Trying to get data from excel then store it inside array and later print it

I am trying to read data from an excel file. 我正在尝试从Excel文件中读取数据。 When i run the below code it runs without any error but won't give any data. 当我运行下面的代码时,它可以正常运行,但不会提供任何数据。 When i checked the code i found that the global variable rownum is not initializing and giving the default value which is 0. But inside the getData(int i) method the rownum is giving a value of 14. I am trying to get all string data available in excel file by using getData(int i) method and later trying to store them inside data[] array. 当我检查代码时,我发现全局变量rownum尚未初始化并给出默认值0。但是在getData(int i)方法中,rownum给出的值为14。我试图获取所有字符串数据通过使用getData(int i)方法在excel文件中可用,然后尝试将它们存储在data []数组中。 Again inside the setData method i am trying to copy all item stored inside the data[] array to a string value. 再次在setData方法内,我试图将存储在data []数组内的所有项目复制到字符串值。 Can anyone please help. 谁能帮忙。 I am new to java. 我是Java新手。 package com.selenium; 包com.selenium;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


@SuppressWarnings("unused")
public class DataFromExcel {
  String[] data = new String[15];
  static int rownum;

 public String[] getData(int i) throws InvalidFormatException, IOException{
   File file = new File("C:\\Users\\Admin\\Desktop\\ScreenShot\\Excel.xls");
   FileInputStream input = new FileInputStream(file);
   Workbook excel = WorkbookFactory.create(input);
   Sheet sheet = excel.getSheet("Data");
   rownum = sheet.getLastRowNum();
   System.out.println(rownum);


     for(int j=1;j<=rownum;j++){
         Row row = sheet.getRow(rownum);
         while(row != null){
         if(row.getCell(j) != null){
         String cell = row.getCell(j).getStringCellValue();
         while(cell != null){
             data[j]=cell;
             System.out.println(data[1]);
         }//while cell
        }//if row 
      }//for  
     }//row while
    return data;
   }//getData 
  public String setData(int i){
      String value = null;
      for(i=1; i<=data.length;i++){
          value =data[i];
      }//for setData
    return value;
  }//setData

 public static void main(String[] args) throws InvalidFormatException, IOException{
    DataFromExcel get = new DataFromExcel();
    System.out.println(rownum);
    get.getData(1);
    if(get.data != null){
    System.out.println(get.setData(1));}
 }//main
}// DataFromExcel

您已经知道(我期望如此) ,该程序的执行是从main()方法开始的。现在,当您调用getData(int i)方法时,此时rownum为零(显然没有代码在修改。它),但随后您调用了带有参数的getData(int i) ,该参数随后根据其中的指令更改了rownum的值,请尝试添加另一个System.out.prinltn()作为main()方法的结束行并打印rownum.的值rownum.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 存储阵列中的数据以供以后比较 - Store data from an array for later comparison 尝试从.data文件中获取值并将它们存储到int [] []数组中 - Trying to get values from a .data file and store them into an int[][] array 尝试将数据存储在数组内的文本文件中 - Trying to Store data I have in a text file inside of an Array 如何从 ArrayList 中获取数据,该 ArrayList (Java) 中存储另一个数组? - How can I get the data from an ArrayList that store another array inside of that ArrayList (Java)? 从图库中获取图像的可绘制地址,并将其存储在数组中 - Get images' drawable addresses from the gallery and store them inside an array 如何存储列表中的多个项目的全局数组供以后使用 - How to store global Array of multiple items from a list for later use 如何将对象类型的数组存储为字符串,以便另一个类以后可以调用并打印该字符串。 爪哇 - How to store an array of object types as a string so another class can later call and print that string. java 将excel字符串数据存储到数组列表 - Store excel string data to an Array list 如何使用apache poi从excel读取数据并将数据存储到String [] []数组中? - How to read data from excel using apache poi and store the data into String[][] array? 尝试使用 ElasticSearch 存储和获取一些数据 - Trying to store and get some data using ElasticSearch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM