[英]Read Data from Excel and Convert into HashMap for Cucumber framework
我正在尝试阅读 Excel 电子表格,其中我维护所有属性文件详细信息,例如:浏览器名称、url、用户名和密码等。
我将数据存储在键值对中,所以当我尝试运行这个 class 时,我没有得到任何结果。 我的 class 显示在控制台中终止。
如果有人可以查看此代码并让我知道您发现了哪些错误,我将予以纠正。
我正在尝试在我的 cucumber 项目中运行这个 excel 示例。
public class ExcelUtil {
public static String ExcelSheetName = "./src/test/java/com/codeElan/TestData/Config.xlsx";
public static FileInputStream fis;
public static XSSFWorkbook workbook;
public static XSSFSheet sheet;
public static XSSFRow row;
public static void loadExcel() {
System.out.println("Load Excel Sheet.........");
File file = new File(ExcelSheetName);
try {
fis = new FileInputStream(file);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("LoginData");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//sheet = workbook.getSheet("LoginData");
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String,Map<String,String>> getDataMap() {
if(sheet==null){
loadExcel();
}
Map<String, Map<String,String> > superMap= new HashMap<String,
Map<String,String> >();
Map<String,String> map = new HashMap<String,String>();
for(int i=1; i<sheet.getLastRowNum()+1; i++){
row = sheet.getRow(i);
String keyCell = row.getCell(0).getStringCellValue();
int collNum = row.getLastCellNum();
for(int j=1;j<collNum;j++){
String value=row.getCell(j).getStringCellValue();
map.put(keyCell, value);
}
superMap.put("MASTERDATA", map);
}
return superMap;
}
public static String getValue(String key) {
Map<String,String> mapValue = getDataMap().get("MASTERDATA");
String retValue = mapValue.get(key);
return retValue;
}
public static void main(String[] args){
System.out.println(getValue("Browser"));
}
}
这是我为解决您的问题而创建的示例脚本。如果您打算在实时项目中使用这个东西,那么我需要在这里添加几点,否则您需要验证来自 excel 单元格的数据类型如果数据是不同类型的,那么它可能会在运行时抛出错误。 如果你真的想用键值对数据之类的东西来参数化你的项目,那么你也应该查看属性文件。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtil {
public static String ExcelSheetName = "Book1.xlsx";
public static FileInputStream fis;
public static XSSFWorkbook workbook;
public static XSSFSheet sheet;
public static XSSFRow row;
public static void loadExcel() {
System.out.println("Load Excel Sheet.........");
File file = new File(ExcelSheetName);
try {
fis = new FileInputStream(file);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheet("LoginData");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String,Map<String,String>> getDataMap() {
if(sheet==null){
loadExcel();
}
Map<String, Map<String,String>> parentMap = new HashMap<String, Map<String,String>>();
Map<String, String> childMap = new HashMap<String, String>();
Iterator<Row> rowIterator = sheet.iterator();
while( rowIterator.hasNext() )
{
Row row = rowIterator.next();
childMap.put(row.getCell(0).getStringCellValue(), row.getCell(1).getStringCellValue());
}
parentMap.put("MASTERDATA", childMap);
return parentMap;
}
public static String getValue(String key) {
Map<String,String> mapValue = getDataMap().get("MASTERDATA");
String retValue = mapValue.get(key);
return retValue;
}
public static void main(String[] args){
System.out.println(getValue("Password"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.