读取 excel 个包含空白的文件以进行 Junit 测试
Read excel file with blanks to Junit Tests
我需要帮助来更改 class 我用来读取单元参数化测试 class 的 excel。非常感谢您的帮助。
下面的 class 不读取空白单元格值。它会跳过空白单元格,因此传递给构造函数的参数数量是错误的。当单元格值为空时,我需要构造函数接受空值。
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;
}
}
我通过实施 MissingCellPolicy 更新了 loadFromSpreadsheet 函数。
for (int column = 0; column < numberOfColumns; column++) {
Cell cell = row.getCell(column, Row.CREATE_NULL_AS_BLANK);
rowData.add(objectFrom(workbook, cell));
}
我需要帮助来更改 class 我用来读取单元参数化测试 class 的 excel。非常感谢您的帮助。
下面的 class 不读取空白单元格值。它会跳过空白单元格,因此传递给构造函数的参数数量是错误的。当单元格值为空时,我需要构造函数接受空值。
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;
}
}
我通过实施 MissingCellPolicy 更新了 loadFromSpreadsheet 函数。
for (int column = 0; column < numberOfColumns; column++) {
Cell cell = row.getCell(column, Row.CREATE_NULL_AS_BLANK);
rowData.add(objectFrom(workbook, cell));
}