如何从Excel文件中读取值并存储到数组中?

7

我希望能够在Java中读取Excel表格的值,并将这些值存储在数组中。

我已经准备好了代码来读取Excel表格,但我无法定制它以将这些值存储在数组中。

这里是我的读取Excel表格的代码:

package com.core.testscripts;

import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;

    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public void read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;
        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);
            // Loop over first 10 column and lines

            for (int j = 0; j < sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    System.out.println(cell.getContents());
                }
            }
        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException 
    {
        NewExcel test = new NewExcel();
        test.setInputFile("D:/hellohowareyou.xls");
        test.read();
    }

}

1
你想如何读取?是将所有行的所有单元格放入一个数组中,还是每行的所有单元格放入一个二维数组中? - Vicky
3个回答

3
import java.io.File;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class NewExcel 
{

    private String inputFile;
    String[][] data = null;
    public void setInputFile(String inputFile) 
    {
        this.inputFile = inputFile;
    }

    public String[][] read() throws IOException  
    {
        File inputWorkbook = new File(inputFile);
        Workbook w;

        try 
        {
            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet


            Sheet sheet = w.getSheet(0);
            data = new String[sheet.getColumns()][sheet.getRows()];
            // Loop over first 10 column and lines
       //     System.out.println(sheet.getColumns() +  " " +sheet.getRows());
            for (int j = 0; j <sheet.getColumns(); j++) 
            {
                for (int i = 0; i < sheet.getRows(); i++) 
                {
                    Cell cell = sheet.getCell(j, i);
                    data[j][i] = cell.getContents();
                  //  System.out.println(cell.getContents());
                }
            }

         /*   for (int j = 0; j < data.length; j++) 
            {
                for (int i = 0; i <data[j].length; i++) 
                {

                    System.out.println(data[j][i]);
                }
            } */

        } 
        catch (BiffException e) 
        {
            e.printStackTrace();
        }
    return data;
    }


}

0

如果您确实想要一个数组,那么在为其分配存储空间时,您必须知道您希望在该数组中有多少元素。您可以在运行时执行此操作--它不必在编译时知道--但是您必须在使用数组之前完成此操作。

在某个声明部分:

String[] dataArray = null;

然后在代码的某个地方

dataArray = new String[numberOfElements];

你可以按照相同的原理创建一个二维(或更高维度)数组。之后,你可以将一个字符串赋值给数组中任何一个索引小于numberOfElements的元素。


我是一名QA,我想将此代码用于我的测试目的。 我有一个包含5个值的Excel表格。 我在另一个类中拥有Google搜索的Selenium命令:selenium.type("css=#gbqfq", "Hello"); 现在在这个命令中,我想使用来自Excel表格的那些值,并使用这些值运行此命令,而不是使用“Hello”。 - Rishil Bhatt

-1
package Utilities;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
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.xssf.usermodel.XSSFWorkbook;

public class ExcellReading {

    // public Workbook workbook= null;
    // public Sheet firstSheet= null;

    public String INPUT_XLS = "/ExcelManipulation/TestExecution.xlsx";

    public ExcellReading() {
    }

    public ExcellReading(String filepath) {
        INPUT_XLS = filepath;
    }

    public Map<Integer, List<String>> ReadExcel() throws IOException {

        FileInputStream inputStream = new FileInputStream(new File("TestExecution.xlsx"));

        Map<Integer, List<String>> data = new HashMap<Integer, List<String>>();

        Workbook workbook = new XSSFWorkbook(inputStream);

        Sheet firstSheet = workbook.getSheetAt(5);

        Iterator<Row> iterator = firstSheet.iterator();

        // Test test=new Test();
        int rowCnt = 0;

        while (iterator.hasNext()) {
            Row nextRow = iterator.next();

            Iterator<Cell> cellIterator = nextRow.cellIterator();
            List<String> obj = new ArrayList<String>();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();

                String cellobj = cell.getStringCellValue();

                if ("".equals(cell.getStringCellValue())) {
                    obj.add("Missing");

                } else if (cellobj.equals(null)) {
                    obj.add("");

                } else {
                    obj.add(cell.getStringCellValue());
                }

            }

            data.put(rowCnt, obj);
            rowCnt++;

        }
        return data;
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接