java - 使用 Apache POI 确认 Excel 中的正确单元格
问题描述
我需要验证 Excel 单元格是否具有正确的值才能继续,我对许多字符串和单元格进行了验证。我需要验证四个单元格。它正在工作。
有什么方法可以用更少的线做同样的事情吗?最好的方法是什么?
单元格必须有 b3=Número do Cartão ,c3= Número do Pedido ,d3= Motivo do Cancelamento ,e3 = Observação
像这样: Excel 行
private boolean validaLinha(InputStream arquivoCancelamento) throws IOException{
XSSFWorkbook wb = new XSSFWorkbook(arquivoCancelamento);
Sheet sheet = wb.getSheetAt(0);
FormulaEvaluator evaluator = wb.getCreationHelper().createFormulaEvaluator();
String celulaB3 = "N\u00FAmero do cart\u00E3o ";
String celulaC3 = "N\u00FAmero Pedido";
String celulaD3 = "Motivo de Cancelamento";
String celulaE3 = "observa\u00E7\u00E3o";
boolean val = false;
CellReference cellReferenceB3 = new CellReference("B3");
CellReference cellReferenceC3= new CellReference("C3");
CellReference cellReferenceD3 = new CellReference("D3");
CellReference cellReferenceE3 = new CellReference("E3");
CellReference cellReferenceB4 = new CellReference("B4");
Row row = sheet.getRow(cellReferenceC3.getRow());
Cell cellB3 = row.getCell(cellReferenceB3.getCol());
Cell cellC3 = row.getCell(cellReferenceC3.getCol());
Cell cellD3 = row.getCell(cellReferenceD3.getCol());
Cell cellE3 = row.getCell(cellReferenceE3.getCol());
Cell cellB4 = row.getCell(cellReferenceB4.getCol());
if (cellReferenceB3!=null && celulaB3.equals(cellB3.getStringCellValue() )) {
return true;}
if (cellReferenceC3!=null && celulaC3.equals(cellC3.getStringCellValue() )) {
return true;}
if (cellReferenceD3!=null && celulaC3.equals(cellD3.getStringCellValue() )) {
return true;}
if (cellReferenceE3!=null && celulaC3.equals(cellE3.getStringCellValue() )) {
return true;}
if (cellReferenceB4!=null || cellB4.getCellType() != Cell.CELL_TYPE_BLANK) {
return true;}
else {
return false;
}
}
解决方案
推荐阅读
- python - python中Drive API的问题
- typescript - 为什么数组中的 Observable 声明没有展平?
- javascript - 自定义 HTML 视频播放器中的全屏功能无法正常工作
- css - mat-icon 不在 mat-button 中心
- visual-studio - OpenCppCoverage 插件没有为 C++ 提供代码覆盖
- ksh - 如何扩展从 ksh 脚本中的文件读取的字符串中的宏?
- git - Visual Studio 一直在我的项目中显示 bin 文件夹
- c# - .NET 框架 EF 代码优先数据库连接问题
- amazon-web-services - AWS CDK 从图像创建 Lambda
- arrays - 如何按特定顺序分布数组的元素