java - 如何使用 Apache POI 和 Java 添加多个不同的 Excel 列

标签 java parsing apache-poi

我需要生成CodeKey这是 two columns 的组合excel 值和这些列中的值可以是 Boolean , String , Numeric或所有这些的组合。

现在我可以通过if/else loop并检查每个条件,但什么是执行此操作的有效方法。

例如:

如果我有ExCode = 7VPrCode = A:那么我的CodeKey应该是7VA:

 ExCode  PrCode
 6D:     A:
 6R      TR
 7V      6K

我想做的就是将 CodeKey 生成为 6D:A: , 6RTR7V6K分别。

不想做这样的事情:

 if(ExCodeCellValue.getCellType() == Cell.CELL_TYPE_STRING &&
           PrCodeCellValue.getCellType() == Cell.CELL_TYPE_STRING){
            System.out.println("Combined String Values: "+ExCodeCellValue.getStringValue()+PrCodeCellValue.getStringValue());
        }

因为会有很多 if/else生成 codeKey 不必要的东西,任何其他有效的解决方案,或者是否有任何 apiPOI这对这种情况有用吗?

最佳答案

我认为你应该能够使用DataFormatter.formatCellValue(cell)这将为您提供一个与 Excel 显示的单元格内容相匹配的字符串。

这样,您可能会看起来像这样(假设 ExCode 是第三列,PrCode 在第四列)

// Do this once
DataFormatter formatter = new DataFormatter();

// Once per row
for (Row row : sheet) {
  String exCode = formatter.formatCellValue( row.getCell(2) );
  String prCode = formatter.formatCellValue( row.getCell(3) );

  Cell code = row.createCell(4, Cell.CELL_TYPE_STRING);
  code.setCellValue(exCode + prCode);
}

关于java - 如何使用 Apache POI 和 Java 添加多个不同的 Excel 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9946338/

相关文章:

xml - 使用 Powershell 解析不一致的 XML 对象

ios - 未触及的 AppDelegate.m 中的编译错误

c++ - 如何显示功能的开始,结束行和功能体?

java - Apache POI setPrintArea 为 A4 页面大小

java - Excel 或文本文件,该使用哪一个?

Java删除对象

java - 如何在HBase中实现分页?

java - 在字符串中的变量周围包含引号

java - 调整基本箭头按钮的大小

java - 使用POI Word API,如何在后续页面上重复表格标题?