2012-03-26 94 views
5

我想讀取Excel表格值並將這些值存儲在Java數組中。如何讀取excel文件中的值並將其存儲在Array中?

我準備好讀取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(); 
    } 

} 
+1

你想怎樣讀?單個數組中所有行的所有單元格?或者二維數組中的每行所有單元格? – Nik 2012-03-26 11:25:29

回答

0

如果你真的想要一個數組,你將必須知道你想要的數組中有多少元素,當你爲它分配存儲空間。您可以在運行時執行此操作 - 在編譯時不必知道它,但在使用數組之前必須先完成此操作。

某處在聲明部分:

String[] dataArray = null; 

,然後在某個地方的代碼

dataArray = new String[numberOfElements]; 

你可以做一個兩個(或更多)維原理相同陣列。之後,您可以將一個字符串分配給數組中任何小於numberOfElements的索引。

+0

我是QA,我想用這個代碼來測試我的測試目的。 我有一個Excel工作表有5個值。我有Selenium命令在另一個類中搜索Google:selenium.type(「css =#gbqfq」,「Hello」);現在在這個命令中,我想使用Excel表格中的這些值,並使用這些值而不是「Hello」來運行此命令。 – 2012-03-27 06:37:02

2
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
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; 
    } 

} 
相關問題