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

Posted

技术标签:

【中文标题】如何将Excel文件转换为黄瓜数据表【英文标题】:How to convert Excel file into cucumber DataTable 【发布时间】:2021-09-03 16:27:42 【问题描述】:

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

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文件数据是这样的

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

【问题讨论】:

【参考方案1】:

你应该实现黄瓜的Transformer&lt;T&gt;接口并将List&lt;List&lt;String&gt;&gt;映射到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;
    

【讨论】:

以上是关于如何将Excel文件转换为黄瓜数据表的主要内容,如果未能解决你的问题,请参考以下文章

如何将Excel数据转换为SQL脚本

如何将excel文件转换为mysql数据库? [复制]

如何将Excel文件转换成db文件,如下

如何将Excel数据转换为SQL脚本

如何将HTML文件转化成excel的文件,

VFP如何将超大数据的EXCEL表转换为DBF表