2012-03-30 33 views
0

Excel中的多个不同的列我需要生成CodeKey是在那些列Excel的价值和价值观的two columns组合可以是所有的BooleanStringNumeric或组合。如何添加使用Apache POI和Java的

现在我可以通过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这将是对这种情况有用吗?

回答

1

我认为你应该能够使用DataFormatter.formatCellValue(cell)这将给你一个字符串,匹配Excel显示的单元格。

有了这一点,你可能会看起来像(假设ExCode是第3列,PrCode在4日)

// 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); 
} 
+0

谢谢,这是我一直在寻找。 – Rachel 2012-04-02 13:53:45