Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ApachePOI和java从Excel中获取数据_Java_Excel_Selenium Webdriver_Apache Poi - Fatal编程技术网

使用ApachePOI和java从Excel中获取数据

使用ApachePOI和java从Excel中获取数据,java,excel,selenium-webdriver,apache-poi,Java,Excel,Selenium Webdriver,Apache Poi,我需要通过传递测试用例id从excel中获取测试数据。如果我将测试用例id传递为“TC005”-我需要获取所有列值,如Lab name、Lab Address、City、State、Pincode、Collection center name、CC Address、City、State和Pincode 有人能告诉我怎么做吗 我一直在努力,但我只能得到一个领域 package com.utils; import java.io.BufferedInputStream; import java

我需要通过传递测试用例id从excel中获取测试数据。如果我将测试用例id传递为“TC005”-我需要获取所有列值,如Lab name、Lab Address、City、State、Pincode、Collection center name、CC Address、City、State和Pincode

有人能告诉我怎么做吗

我一直在努力,但我只能得到一个领域

package com.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;


public class ReadExcel {
    static int colTestcase;
     static int rowTestcase;
     static int rownumber ;
     static int colnumber;
     static InputStream input = null;
     static HSSFWorkbook workbook;
     static FileInputStream file ;
     static HSSFSheet sheet;
     static Properties prop = new Properties();


     public static boolean openFile(String fileName) {
            try {
                file = new FileInputStream(fileName);
                workbook = new HSSFWorkbook(file);
                return true;
            }
            catch(Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        public static void main(String args[])
    {
        long startTime = System.currentTimeMillis();
        System.out.println("startTime: "+startTime);
        int rowvalue=readTestCase("TC03");
        int columnvalue=readHeader("Service Title");
        String value=getValuefromExcel(rowvalue,columnvalue);
        System.out.println("Row Value: "+rowvalue);
        System.out.println("Col Value: "+columnvalue);
        System.out.println("Text: "+value);
        long endTime = System.currentTimeMillis();
        System.out.println("endTime"+endTime);
        System.out.println("Took "+(endTime - startTime) + " milliseconds");
    }


    public static String getInputData(String Testcase,String header)
    {
        int rowvalue=readTestCase(Testcase);
        int columnvalue=readHeader(header);
        String value = getValuefromExcel(rowvalue, columnvalue);
        return value;
    }

    public static String getValuefromExcel(int row, int col)
    {
        String value =null;
        String field="Wrongvalue";
        if(!(row==-1||col==-1))
        {
            try {
                System.out.println("Entered");
                input = new FileInputStream("src\\com\\config\\config.properties");
                // load a properties file
                prop.load(input);
                openFile(prop.getProperty("excelTestData_FilePath"));
                //Get first sheet from the workbook
                sheet = workbook.getSheet(prop.getProperty("excelTestData_Sheet"));
                HSSFCell cell = null;
                //Update the value of cell
                cell = sheet.getRow(row).getCell(col);
                value = cell.toString();
                file.close();
            }
            catch(Exception e){
                e.printStackTrace();
            }
            System.out.println("value Done");
            return value;
        }
        else {
            System.out.println("Given data was wrong");
        }
    return field;
    }

    public static int readTestCase(String Testcase)
    {
    try {
        FileInputStream file = new FileInputStream(new File("C:\\Thyagu\\Workspace\\PMModule\\test-input\\PM_ModuleTestData.xls"));

        //Get the workbook instance for XLS file 
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheet("Data");

        int rowNum = sheet.getLastRowNum()+1;
        int colNum = sheet.getRow(0).getLastCellNum();
        for (int i=1; i<rowNum; i++){
            HSSFRow row = sheet.getRow(i);
            int j=0;
                        HSSFCell cell = row.getCell(j);
                        String value = cell.toString();
                                if(value.equalsIgnoreCase(Testcase))
                                {
                                    rowTestcase=row.getRowNum();
                                    colTestcase=cell.getCellNum();
                                    return rowTestcase;
                                }
                            }
        file.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    System.out.println("rownumber Done: " +rowTestcase);
    return -1;
    }

    public static int readHeader(String header)
    {
    try {
        FileInputStream file = new FileInputStream(new File("C:\\Thyagu\\Workspace\\PMModule\\test-input\\PM_ModuleTestData.xls"));

        //Get the workbook instance for XLS file 
        HSSFWorkbook workbook = new HSSFWorkbook(file);

        //Get first sheet from the workbook
        HSSFSheet sheet = workbook.getSheet("Data");

        int rowNum = sheet.getLastRowNum()+1;
        int colNum = sheet.getRow(0).getLastCellNum();
        int i=0;
            HSSFRow row = sheet.getRow(i);
            rownumber =row.getRowNum();
               for (int j=0; j<colNum; j++){
                        HSSFCell cell = row.getCell(j);
                        String value = cell.toString();
                                if(value.equalsIgnoreCase(header))
                                {
                                    rownumber=row.getRowNum();
                                    colnumber=cell.getCellNum();
                                    return colnumber;
                                }
                            }
        file.close(); 
    }
    catch(Exception e){
        e.printStackTrace();
    }
    System.out.println("colnumber Done");
    return -1;
    }
}
package com.utils;
导入java.io.BufferedInputStream;
导入java.io.BufferedOutputStream;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.Iterator;
导入java.util.List;
导入java.util.Properties;
导入org.apache.poi.hssf.usermodel.HSSFCell;
导入org.apache.poi.hssf.usermodel.HSSFRow;
导入org.apache.poi.hssf.usermodel.HSSFSheet;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.ss.usermodel.Row;
公共类ReadExcel{
静态int-colTestcase;
静态int-rowTestcase;
静态整数行数;
静态整数;
静态输入流输入=null;
静态HSSF工作手册;
静态文件输入流文件;
静态HSSF片材;
静态属性prop=新属性();
公共静态布尔openFile(字符串文件名){
试一试{
文件=新文件输入流(文件名);
工作簿=新的HSSF工作簿(文件);
返回true;
}
捕获(例外e){
e、 printStackTrace();
返回false;
}
}
公共静态void main(字符串参数[])
{
long startTime=System.currentTimeMillis();
System.out.println(“startTime:+startTime”);
int rowvalue=readTestCase(“TC03”);
int columnvalue=readHeader(“服务标题”);
字符串值=getValuefromExcel(行值、列值);
System.out.println(“行值:”+rowvalue);
System.out.println(“列值:”+columnvalue);
System.out.println(“文本:+值);
long-endTime=System.currentTimeMillis();
System.out.println(“endTime”+endTime);
println(“take”+(endTime-startTime)+“毫秒”);
}
公共静态字符串getInputData(字符串测试用例,字符串头)
{
int rowvalue=readTestCase(Testcase);
int columnvalue=readHeader(header);
字符串值=getValuefromExcel(行值、列值);
返回值;
}
公共静态字符串getValuefromExcel(int行,int列)
{
字符串值=null;
字符串字段=“错误值”;
如果(!(行==-1 |列==-1))
{
试一试{
系统输出打印项次(“输入”);
输入=新文件输入流(“src\\com\\config\\config.properties”);
//加载属性文件
道具载荷(输入);
openFile(prop.getProperty(“excelTestData_FilePath”);
//从工作簿中获取第一张工作表
sheet=workbook.getSheet(prop.getProperty(“excelTestData_sheet”);
HSSFCell单元=空;
//更新单元格的值
单元格=sheet.getRow(行).getCell(列);
value=cell.toString();
file.close();
}
捕获(例外e){
e、 printStackTrace();
}
系统输出打印项次(“完成值”);
返回值;
}
否则{
System.out.println(“给定数据错误”);
}
返回字段;
}
公共静态int-readTestCase(字符串Testcase)
{
试一试{
FileInputStream file=newfileinputstream(新文件(“C:\\Thyagu\\Workspace\\PMModule\\test input\\PM\u ModuleTestData.xls”);
//获取XLS文件的工作簿实例
HSSF工作簿=新的HSSF工作簿(文件);
//从工作簿中获取第一张工作表
HSSFSheet sheet=workbook.getSheet(“数据”);
int rowNum=sheet.getLastRowNum()+1;
int colNum=sheet.getRow(0.getLastCellNum();

对于(inti=1;iThyagu:我建议一个解决方案……首先,您应该创建一个方法,找到具有所需第一个单元格值的行(在您的示例中,我认为是所需的TestCaseID),并为其返回一个
RowObject

public static HSSFRow findRow(String stringToFind){
            //`hswb` is HSSFWorkbook
    HSSFSheet sheet = hswb.getSheetAt(0);
    Iterator <Row> rowItr = sheet.iterator();
    while(rowItr.hasNext()){
        HSSFRow row = (HSSFRow) rowItr.next();
        if(row.getCell(0).toString().equalsIgnoreCase(stringToFind)){
            return row;
        }
    }
  // If no such row found need to handle NullPointerException for that
    return null;
}

如果有可能在任何行的最后一个单元格值内得到一个空单元格,您需要使用
MissingCellPolicy

您尝试了什么?这是一个百万美元的问题…没有表现出任何努力…@sankumarsing:我尝试过,没有问问题,也没有表现出任何努力谢谢sankumarsing…但我需要列值输出:实验室名称-阿德亚尔;实验室地址-第1条主要道路塞卡尔街24号;城市-钦奈;州-泰米尔纳德邦;Pincode-600001;收集中心名称-梅达瓦卡姆;CC地址,城市-钦奈;州-泰米尔纳德邦;Pincode-6000024;您能告诉我如何使用标题和值进行映射吗?我已经更新了第二种方法。。。希望它能起作用。是的,它工作得很好。很棒的工作Sankumarsingh。我教过它不可能。很棒的工作。
    public static void getAllColumnFromRow(HSSFRow RowObject){
    Iterator<Cell> itr = RowObject.iterator();
    HSSFRow headerRow = RowObject.getSheet().getRow(0);
    String cellValue = "";
    String information = "";
    int headerCellCount = 0;
    //to avoid first column
    itr.next();

    while(itr.hasNext()){
        Cell cell = itr.next();
        Cell headerValue = headerRow.getCell(headerCellCount++);
        switch(cell.getCellType()) {
        case HSSFCell.CELL_TYPE_BOOLEAN:
            cellValue = cell.getBooleanCellValue() +"";
            information = information + " " +headerValue+" - "+cellValue+ "; ";
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            cellValue = cell.getNumericCellValue() + "";
            information = information + " " +headerValue+" - "+cellValue+ "; ";
            break;
        case HSSFCell.CELL_TYPE_STRING:
            cellValue = cell.getStringCellValue();
            information = information + " " +headerValue+" - "+cellValue+ "; ";
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            break;
        }
    }
    System.out.println(information);
}