将带有空白的 excel 文件读取到 Junit 测试

Posted

技术标签:

【中文标题】将带有空白的 excel 文件读取到 Junit 测试【英文标题】:Read excel file with blanks to Junit Tests 【发布时间】:2015-03-11 10:37:33 【问题描述】:

我需要帮助来更改我用来读取单元参数化测试类的 excel 的类。非常感谢您的帮助。

下面的类不读取空白单元格值。它会跳过空白单元格,因此传递给构造函数的参数数量是错误的。当单元格值为空白时,我需要构造函数接受空值。

public class SpreadsheetData 

    /**
     * The contents of the spreadsheet, in a form compatible with JUnit 4 parameterized tests.
     */
    private transient Collection<Object[]> data = null;

    public SpreadsheetData(final InputStream excelInputStream) throws IOException 
        this.data = loadFromSpreadsheet(excelInputStream);
    

    public Collection<Object[]> getData() 
        return data;
    

    private Collection<Object[]> loadFromSpreadsheet(final InputStream excelFile)
            throws IOException 
        HSSFWorkbook workbook = new HSSFWorkbook(excelFile);

        data = new ArrayList<Object[]>();
        Sheet sheet = workbook.getSheetAt(0);

        int numberOfColumns = countNonEmptyColumns(sheet);


        List<Object[]> rows = new ArrayList<Object[]>();
        List<Object> rowData = new ArrayList<Object>();


        int numberOfRows = sheet.getPhysicalNumberOfRows();
        //for (Row row : sheet) 
        for (int y=1;y<numberOfRows;y++)
            Row row=sheet.getRow(y);
            if (isEmpty(row)) 
                break;
             else 
                rowData.clear();
                for (int column = 0; column < numberOfColumns; column++) 
                    Cell cell = row.getCell(column);
                    rowData.add(objectFrom(workbook, cell));
                
                rows.add(rowData.toArray());
            
        

        return rows;
    

    private boolean isEmpty(final Row row) 
        Cell firstCell = row.getCell(0);
        boolean rowIsEmpty = (firstCell == null)
                || (firstCell.getCellType() == Cell.CELL_TYPE_BLANK);
        return rowIsEmpty;
    

    /**
     * Count the number of columns, using the number of non-empty cells in the
     * first row.
     */
    private int countNonEmptyColumns(final Sheet sheet) 
        Row firstRow = sheet.getRow(1);
        return firstEmptyCellPosition(firstRow);
    

    private int firstEmptyCellPosition(final Row cells) 
        int columnCount = 0;
        for (Cell cell : cells) 
            if (cell.getCellType() == Cell.CELL_TYPE_BLANK) 
                break;
            
            columnCount++;
        
        return columnCount;
    


    private Object objectFrom(final HSSFWorkbook workbook, final Cell cell) 
        Object cellValue = null;

        if (cell.getCellType() == Cell.CELL_TYPE_STRING) 
            cellValue = cell.getRichStringCellValue().getString();
         else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) 
            cellValue = getNumericCellValue(cell);
         else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
            cellValue = cell.getBooleanCellValue();
         else if (cell.getCellType()  ==Cell.CELL_TYPE_FORMULA) 
            cellValue = evaluateCellFormula(workbook, cell);
        

        return cellValue;

    

    private Object getNumericCellValue(final Cell cell) 
        Object cellValue;
        if (DateUtil.isCellDateFormatted(cell)) 
            cellValue = new Date(cell.getDateCellValue().getTime());
         else 
            cellValue = cell.getNumericCellValue();
        
        return cellValue;
    

    private Object evaluateCellFormula(final HSSFWorkbook workbook, final Cell cell) 
        FormulaEvaluator evaluator = workbook.getCreationHelper()
                .createFormulaEvaluator();
        CellValue cellValue = evaluator.evaluate(cell);
        Object result = null;

        if (cellValue.getCellType() == Cell.CELL_TYPE_BOOLEAN) 
            result = cellValue.getBooleanValue();
         else if (cellValue.getCellType() == Cell.CELL_TYPE_NUMERIC) 
            result = cellValue.getNumberValue();
         else if (cellValue.getCellType() == Cell.CELL_TYPE_STRING) 
            result = cellValue.getStringValue();   
        
          else if (cellValue.getCellType()==Cell.CELL_TYPE_BLANK)
          
              result =cellValue.getStringValue();; 
          


        return result;
    


【问题讨论】:

来自Ria的评论:我不确定你的问题是否正确。据我所知,如果单元格为空白,则通过以“中断”结束循环来停止读取数据(isEmpty 检查行中的第一个单元格是否为空白)。这是你的问题吗? 【参考方案1】:

我通过实现 MissingCellPolicy 更新了 loadFromSpreadsheet 函数。

 for (int column = 0; column < numberOfColumns; column++) 
                Cell cell = row.getCell(column, Row.CREATE_NULL_AS_BLANK);
                rowData.add(objectFrom(workbook, cell));
            

【讨论】:

以上是关于将带有空白的 excel 文件读取到 Junit 测试的主要内容,如果未能解决你的问题,请参考以下文章

从硒中读取空白细胞时无法处理空白细胞?

由于数字格式为文本,将 Excel 文件读取到 Python 失败

从访问表中复制表并粘贴到空白的 Excel 文件中

Excel 数据透视表空白类别未正确显示

将空白 Excel 单元格保存到 Rails Postgres 数据库

复制带有颜色的单元格 - Excel