selenium - 使用 Apache POI 以特定格式读取 excel 'Formula Date cell' 值(excel 具有 STRING FORMULA 和 INT FORMULA 类型)
问题描述
作为 Selenium 自动化的一部分,我们需要读取 Excel(Apache POI 和 xls 格式文件,HSSF)文件,其中包含 STRING FORMULA 和 INTEGER FORMULA 单元格、日期值、NUMERIC 和 STRING 单元格。
当尝试以下方法时,遇到需要将 DateFormat 更新为 (yyyy-mm-dd) 的问题,但它以 dd/mm/yy 的格式给出。NUMERIC 值也读取为 STRING。
我们已经定义了一个返回 String 的方法,以便可以读取该值并将其传递给 JSON Payload。
public static String getData(String strSheetName, String strColumnIdentifier, int strRowIdentifier) throws IOException {
String strAbsFilePath = getAbsolutePath("testdata" + File.separator + "New.xls");
FileInputStream fis = new FileInputStream(strAbsFilePath);
workbook = new HSSFWorkbook(fis);
fis.close();
sheet = workbook.getSheet(strSheetName);
row = sheet.getRow(3);//// Making the object of excel row
DataFormatter formatter = new DataFormatter();
int col_Num = -1;
for (int i = 0; i < row.getLastCellNum(); i++) {
if (row.getCell(i).getStringCellValue().trim().equals(strColumnIdentifier.trim()))
col_Num = i;
}
if (col_Num == -1)
return "";
row = sheet.getRow(strRowIdentifier);
if (row == null)
return "";
cell = row.getCell(col_Num);
formatter.formatCellValue(cell);
if(cell.getCellType() == CellType.FORMULA) {
PrintStream text = null;
switch(cell.getCachedFormulaResultType()) {
case STRING:
HSSFRichTextString str = cell.getRichStringCellValue();
if(str != null && str.length() > 0) {
String cellValue = str.toString();
return cellValue;
}
break;
case NUMERIC:
HSSFCellStyle style = cell.getCellStyle();
if(style == null) {
text.append( (char) cell.getNumericCellValue() );
} else {
// cell.setCellType(CellType.NUMERIC);
short df = workbook.createDataFormat().getFormat("dd/mm/yyy");
// style.setDataFormat(df);
// cell.setCellStyle(style);
DateFormat datetemp = new SimpleDateFormat("dd/mm/yyy");
Date date = cell.getDateCellValue();
String cellValue1 = datetemp.format(date);
//style.setDataFormat(df);//If used this, all Numeric cell values are set to date format
cellValue1=formatter.formatRawCellContents(
cell.getNumericCellValue(),
style.getDataFormat(),
style.getDataFormatString()
);
return cellValue1;
}
break;
case BOOLEAN:
cell.getBooleanCellValue();
}
}
String cellStringValue = formatter.formatCellValue(cell);
return cellStringValue;
}
输出:
stringFieldValue value is: TestingExcelCell //Correctly getting as String
IntFieldValue value is: 11 //It is going as String in the Payload
ReportDate value is: 6/7/19 //This should come in the format of yyyy-mm-dd
请建议是否需要在 yyyy-mm-dd 而不是 dd/mm/yy 中获取 DateFormat,并且 Integer 值作为 Integer 而不是 String (11)。
解决方案
问题是检查单元格是否为日期格式,然后设置日期格式,在对条件进行以下更改后,能够以预期格式获取值。感谢所有的支持。
case NUMERIC:
// HSSFCellStyle style = cell.getCellStyle();
CellStyle style = cell.getCellStyle();
if(style == null) {
cell.getNumericCellValue();
//text.append( (char) cell.getNumericCellValue() );
} else if(DateUtil.isCellDateFormatted(cell)) {
cell.setCellStyle(style);
SimpleDateFormat datetemp = new SimpleDateFormat("yyyy-MM-dd");
//String formattedCell = datetemp.format(cell.getDateCellValue());
Date date = cell.getDateCellValue();
String formattedCell = datetemp.format(date);
return formattedCell;
}
推荐阅读
- excel - 不同 Excel 应用程序/实例中的 .find 和 Date 问题
- refactoring - 微层:不要在顶部添加功能,而是简化整体依赖关系
- angular - Plesk - Angular 8 托管
- generate - StyleGAN 如何使用 A 源图像生成 B 图像
- python - 使用 python 连续读取/监视串行端口(如果端口未打开连续运行脚本)
- python - 函数的代码错误
- c# - 如果这些像素具有某种颜色,如何有效地计算从 MonoGame 中某个角度可以看到的像素数
- javascript - 强制刷新 node.js 中的数据更改
- javascript - VueJS:使用基于提供的索引的 splice() 方法删除数组中动态渲染的组件,不起作用
- typescript - 对 vscode 扩展的配置进行类型检查的最佳方法是什么?