如何从excel文件中读取值并存储在Array中?

时间:2012-03-26 11:15:29

标签: java jxl

我想读取excel表值并将这些值存储在Java中的数组中。

我已准备好读取excel表的代码,但我无法自定义它以将这些值存储在Array中。

以下是我阅读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();
    }

}

3 个答案:

答案 0 :(得分: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;
    }


}

答案 1 :(得分:0)

如果您真的想要一个数组,那么在为它分配存储时,您将需要知道数组中需要多少个元素。你可以在运行时这样做 - 它不必在编译时知道 - 但你必须在使用数组之前这样做。

声明部分的某处:

String[] dataArray = null;

然后在代码中的某个地方

dataArray = new String[numberOfElements];

您可以按照相同的原则制作两个(或更多)维数组。之后,您可以在索引小于numberOfElements的情况下将字符串分配给数组的任何元素。

答案 2 :(得分:-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;
    }

}