如何将Excel文件转换为黄瓜数据表

问题描述 投票:0回答:2

嗨,我正在做一个 BDD 黄瓜项目。而不是在 Cucumber 功能文件中提供 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文件数据是这样的

但我无事可做。我到处寻求帮助但得不到。任何帮助将不胜感激。 谢谢

cucumber bdd cucumber-java
2个回答
0
投票

您应该实现 Cucumber 的

Transformer<T>
接口,并将
List<List<String>>
映射到
DataTable
。这个对我有用。 如:

import io.cucumber.cucumberexpression.Transformer


public class ExcelDataToDataTable extends Transformer<DataTable> {

    @Override
    public DataTable transform(String filePath) {
        ExcelReader reader = new ExcelReader.ExcelReaderBuilder()
                .setFileLocation(filePath)
                .setSheet(0)
                .build();
        
        List<List<String>> excelData = getExcelData(reader);
        
        List<DataTableRow> dataTableRows = getDataTableRows(excelData);
        
        DataTable table = getDataTable(dataTableRows);
        
        return table;
    }

0
投票

可以直接使用DataTable.create(excelData)。 为此,你需要

  1. 从Excel中检索数据

  2. 转换为数据表 1: 公共静态列表 readExcelFile(String filePath) { 列表数据 = new ArrayList<>();

     try (FileInputStream file = new FileInputStream(filePath)) {
         Workbook workbook = new XSSFWorkbook(file);
         Sheet sheet = workbook.getSheetAt(0);
    
         Iterator<Row> rowIterator = sheet.iterator();
    
         while (rowIterator.hasNext()) {
             Row row = rowIterator.next();
             Iterator<Cell> cellIterator = row.cellIterator();
    
             List<String> rowData = new ArrayList<>();
             while (cellIterator.hasNext()) {
                 Cell cell = cellIterator.next();
                 switch (cell.getCellTypeEnum()) {
                     case STRING:
                         rowData.add(cell.getStringCellValue());
                         break;
                     case NUMERIC:
                         rowData.add(String.valueOf(cell.getNumericCellValue()));
                         break;
                     case BOOLEAN:
                         rowData.add(String.valueOf(cell.getBooleanCellValue()));
                         break;
                     case BLANK:
                         rowData.add("");
                         break;
                     default:
                         rowData.add("");
                         break;
                 }
             }
             data.add(rowData);
         }
    
         workbook.close();
     } catch (IOException e) {
         e.printStackTrace();
     }
    
     return data;
    

    }

2:

public static DataTable excelDataToDataTable(String filePath) {
        List<List<String>> excelData = ExcelUtils.readExcelFile(filePath);
        DataTable dataTable = DataTable.create(excelData);
        return dataTable;
    }

这样就可以了。

© www.soinside.com 2019 - 2024. All rights reserved.