2012-03-26 39 views
5

Tôi muốn đọc các giá trị bảng tính excel và lưu trữ các giá trị đó trong một mảng trong Java.Cách đọc các giá trị từ tệp excel và lưu trữ trong Array?

Tôi có mã sẵn sàng để đọc bảng excel nhưng tôi không thể tùy chỉnh nó để lưu trữ các giá trị đó trong Mảng.

Đây là mã của tôi để đọc một tờ 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

Làm thế nào để bạn muốn đọc? Tất cả các ô của tất cả các hàng trong một mảng? Hoặc tất cả các ô trên mỗi hàng trong một mảng hai chiều ?? – Nik

Trả lời

0

Nếu bạn thực sự muốn có một mảng, bạn sẽ phải biết có bao nhiêu yếu tố mà bạn muốn trong mảng khi bạn phân bổ lưu trữ cho nó. Bạn có thể làm điều đó trong thời gian chạy - nó không phải được biết tại thời gian biên dịch - nhưng bạn phải làm điều đó trước khi bạn có thể sử dụng mảng.

Một nơi nào đó trong phần khai báo:

String[] dataArray = null; 

và sau đó ở đâu đó trong các mã

dataArray = new String[numberOfElements]; 

Bạn có thể làm cho một (hoặc nhiều hơn) mảng hai chiều trên cùng một nguyên tắc. Sau đó, bạn có thể gán một chuỗi cho bất kỳ phần tử nào của mảng tại một chỉ mục nhỏ hơn numberOfElements.

+0

Tôi là một QA và tôi muốn sử dụng mã này cho mục đích thử nghiệm của mình. Tôi có một trang tính Excel có 5 giá trị. Và tôi có lệnh Selenium tìm kiếm của Google trong một lớp khác: selenium.type ("css = # gbqfq", "Hello"); bây giờ trong lệnh này tôi muốn sử dụng các giá trị đó từ bảng excel và chạy lệnh này bằng cách sử dụng các giá trị đó thay vì "Hello". –

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

} 
Các vấn đề liên quan