2012-03-26 11 views
5

मैं एक्सेल शीट मान पढ़ना चाहता हूं और उन मानों को जावा में सरणी में संग्रहीत करना चाहता हूं।एक्सेल में एक्सेल फ़ाइल और स्टोर से मूल्य कैसे पढ़ा जाए?

मेरे पास एक्सेल शीट पढ़ने के लिए कोड तैयार है लेकिन मैं उन मानों को ऐरे में संग्रहीत करने के लिए इसे अनुकूलित करने में सक्षम नहीं हूं।

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

उत्तर

0

तुम सच में एक सरणी चाहते हैं, आप को पता है कि कितने तत्वों आप सरणी में चाहते हैं जब आप इसके लिए भंडारण का आवंटन करने के लिए जा रहे हैं:

यहाँ एक एक्सेल शीट को पढ़ने के लिए मेरे कोड है। आप रनटाइम पर ऐसा कर सकते हैं - इसे संकलन समय पर जाना नहीं है - लेकिन आपको सरणी का उपयोग करने से पहले ऐसा करना होगा।

एक घोषणाओं अनुभाग में कहीं न कहीं:

String[] dataArray = null; 

और उसके बाद कोड में कहीं

dataArray = new String[numberOfElements]; 

तुम एक दो (या अधिक) आयामी ही सिद्धांत पर सरणी बना सकते हैं। उसके बाद, आप numberOfElements से कम इंडेक्स पर सरणी के किसी भी तत्व को स्ट्रिंग असाइन कर सकते हैं।

+0

मैं एक क्यूए हूं और मैं अपने कोड के उद्देश्य के लिए इस कोड का उपयोग करना चाहता हूं। मेरे पास 5 मान वाले एक्सेल शीट हैं। और मेरे पास दूसरी खोज में Google खोज का सेलेनियम कमांड है: selenium.type ("css = # gbqfq", "हैलो"); अब इस कमांड में मैं एक्सेल शीट से उन मानों का उपयोग करना चाहता हूं और "हैलो" के बजाय उन मानों का उपयोग करके इस कमांड को चलाता हूं। –

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

}