我需要生成CodeKey
这是 two columns
的组合excel 值和这些列中的值可以是 Boolean
, String
, Numeric
或所有这些的组合。
现在我可以通过if/else loop
并检查每个条件,但什么是执行此操作的有效方法。
例如:
如果我有ExCode = 7V
和PrCode = A:
那么我的CodeKey应该是7VA:
ExCode PrCode
6D: A:
6R TR
7V 6K
我想做的就是将 CodeKey 生成为 6D:A:
, 6RTR
和7V6K
分别。
不想做这样的事情:
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 不必要的东西,任何其他有效的解决方案,或者是否有任何 api
在POI
这对这种情况有用吗?
最佳答案
我认为你应该能够使用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/