如何将Excel文件转换成黄瓜DataTable

时间:2021-06-19 04:11:50

标签: cucumber bdd cucumber-java

嗨,我正在做一个 BDD 黄瓜项目。而不是在黄瓜特征文件中提供 DataTable 本身的数据。我正在尝试传递 Excel 文件位置 我的黄瓜特征文件是这样的

Feature: Read data from cucumber Feature

Scenario: Any scenario with different set of excel data
Then Read the data from  excel sheet "C:\Users\Govind\Desktop\MOCK_DATA.xlsx" 

现在我想做的是在我的步骤定义中我想使用某种excel阅读器来读取excel文件的数据并将这些数据转换为Cucumber DataTable。这样我就可以使用 DataTable.asMaps 了。我已经实现了,但是在该 DataTable 上使用 .asMaps 函数时遇到问题。

我的 ExcelReader 类是

package otherutil;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelReaders {

    private String fileName;
    private String sheetName;
    private int sheetIndex;
    private XSSFWorkbook book;

    private ExcelReaders(ExcelReaderBuilder excelReaderBuilder) {
        this.fileName = excelReaderBuilder.fileName;
        this.sheetIndex = excelReaderBuilder.sheetIndex;
        this.sheetName = excelReaderBuilder.sheetName;
    }

    public static class ExcelReaderBuilder {

        private String fileName;
        private String sheetName;
        private int sheetIndex;

        public ExcelReaderBuilder setFileLocation(String location) {
            this.fileName = location;
            return this;
        }

        public ExcelReaderBuilder setSheet(String sheetName) {
            this.sheetName = sheetName;
            return this;
        }

        public ExcelReaderBuilder setSheet(int index) {
            this.sheetIndex = index;
            return this;
        }

        public ExcelReaders build() {
            return new ExcelReaders(this);
        }

    }

    private XSSFWorkbook getWorkBook(String filePath) throws InvalidFormatException, IOException {
        return new XSSFWorkbook(new File(filePath));
    }

    private XSSFSheet getWorkBookSheet(String fileName, String sheetName) throws InvalidFormatException, IOException {
        this.book = getWorkBook(fileName);
        return this.book.getSheet(sheetName);
    }

    private XSSFSheet getWorkBookSheet(String fileName, int sheetIndex) throws InvalidFormatException, IOException {
        this.book = getWorkBook(fileName);
        return this.book.getSheetAt(sheetIndex);
    }

    public List<List<String>> getSheetData() throws IOException{
        XSSFSheet sheet;
        List<List<String>> outerList = new LinkedList<>();
        
        try {
            sheet = getWorkBookSheet(fileName, sheetName);
            outerList = getSheetData(sheet);
        } catch (InvalidFormatException e) {
            throw new RuntimeException(e.getMessage());
        }finally {
            this.book.close();
        }
        return outerList;
    }
    
    public List<List<String>> getSheetDataAt() throws InvalidFormatException, IOException {
        
        XSSFSheet sheet;
        List<List<String>> outerList = new LinkedList<>();
        
        try {
            sheet = getWorkBookSheet(fileName, sheetIndex);
            outerList = getSheetData(sheet);
        } catch (InvalidFormatException e) {
            throw new RuntimeException(e.getMessage());
        }finally {
            this.book.close();
        }
        return outerList;
    }

    private List<List<String>> getSheetData(XSSFSheet sheet) {
        List<List<String>> outerList = new LinkedList<>();
        prepareOutterList(sheet, outerList);
        return Collections.unmodifiableList(outerList);
    }

    private void prepareOutterList(XSSFSheet sheet, List<List<String>> outerList) {
        for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) {
            List<String> innerList = new LinkedList<>();
            XSSFRow xssfRow = sheet.getRow(i);

            for (int j = xssfRow.getFirstCellNum(); j < xssfRow.getLastCellNum(); j++) {
                prepareInnerList(innerList, xssfRow, j);
            }
            outerList.add(Collections.unmodifiableList(innerList));
        }
    }

    private void prepareInnerList(List<String> innerList, XSSFRow xssfRow, int j) {
        switch (xssfRow.getCell(j).getCellType()) {

        case BLANK:
            innerList.add("");
            break;

        case STRING:
            innerList.add(xssfRow.getCell(j).getStringCellValue());
            break;

        case NUMERIC:
            innerList.add(xssfRow.getCell(j).getNumericCellValue() + "");
            break;

        case BOOLEAN:
            innerList.add(xssfRow.getCell(j).getBooleanCellValue() + "");
            break;

        default:
            throw new IllegalArgumentException("Cannot read the column : " + j);
        }
    }
}

这是我的 stepDefn 类代码

ExcelReaders readers = new ExcelReaders.ExcelReaderBuilder().setFileLocation(excelUrl).setSheet(0).build();
        List<List<String>> excelData=readers.getSheetDataAt();
        DataTable data = DataTable.create(excelData);

之后我想使用这样的东西,以便我可以获取数据

for (Map<String, String> users : data.asMaps(String.class, String.class))) {


            System.out.println(users.get("email"));
            
            
        }

而我的Excel文件数据是这样的

enter image description here

但我无能为力。我到处寻找帮助但无法得到。任何帮助将不胜感激。 谢谢

0 个答案:

没有答案
相关问题