2010-01-16 17 views
0

我有以下代碼POI錯誤的日期式值來計算

public boolean processCell(boolean hasData, StringBuffer rowData, Cell cell) 
{ 
    switch (cell.getCellType()) 
    { 
    case Cell.CELL_TYPE_FORMULA: 
    { 
    try 
    { 
    this.evaluator.clearAllCachedResultValues(); 
    switch (this.evaluator.evaluateFormulaCell(cell)) 
    { 
     case XSSFCell.CELL_TYPE_NUMERIC: 
     { 
     if (DateUtil.isCellDateFormatted(cell)) 
     { 
     logger.warn(cell.getCellFormula()); 

     rowData.append(dateFormat.format(cell.getDateCellValue())); 
     hasData = true; 
     } 
     else 
     { 
     rowData.append(numberFormat.format(cell.getNumericCellValue())); 
     hasData = true; 
     } 
     break; 
     } 
     case XSSFCell.CELL_TYPE_STRING: 
     { 
     String stringVal = cell.getStringCellValue().trim().replaceAll("\n", ""); 
     if (stringVal.trim().equalsIgnoreCase("Total MoU/active customer")) 
     { 
     logger.warn("Last - KPI ::" + stringVal); 
     this.finalRecord = true; 
     } 
     rowData.append(stringVal); 
     hasData = true; 
     break; 
     } 
     case XSSFCell.CELL_TYPE_BOOLEAN: 
     { 
     rowData.append(cell.getBooleanCellValue()); 
     hasData = true; 
     break; 
     } 
     case XSSFCell.CELL_TYPE_ERROR: 
     { 
     int eval = cell.getErrorCellValue(); 
     if (eval == DIVIDE_BY_ZERO) 
     rowData.append("0"); 
     hasData = true; 
     break; 
     } 
     case XSSFCell.CELL_TYPE_BLANK: 
     { 
     rowData.append(""); 
     hasData = true; 
     break; 
     } 
    } 
    } 
    catch (java.lang.IllegalArgumentException e) 
    { 

    logger.error(" Formula [ " + (cell.getRowIndex() + 1) + "," + (cell.getColumnIndex() + 1) + " ] " 
     + e.getMessage()); 
    rowData.append("CellError"); 
    this.STATUS = FAILURE; 
    hasData = true; 
    break; 
    } 
    catch (java.lang.IllegalStateException e) 
    { 

    logger.error(" Formula [ " + (cell.getRowIndex() + 1) + "," + (cell.getColumnIndex() + 1) + " ] " 
     + e.getMessage()); 
    rowData.append("CellError"); 
    this.STATUS = FAILURE; 
    hasData = true; 
    break; 
    } 
    catch (java.lang.RuntimeException e) 
    { 
    this.STATUS = FAILURE; 
    logger.error(" Formula [ " + (cell.getRowIndex() + 1) + "," + (cell.getColumnIndex() + 1) + " ] " 
     + e.getMessage()); 
    rowData.append("MissingFileError"); 
    hasData = true; 
    break; 
    } 
    break; 
    } 
    case XSSFCell.CELL_TYPE_BLANK: 
    { 
    rowData.append(""); 
    hasData = true; 
    break; 
    } 
    case XSSFCell.CELL_TYPE_NUMERIC: 
    { 
    if (DateUtil.isCellDateFormatted(cell)) 
    { 
    rowData.append(dateFormat.format(cell.getDateCellValue())); 
    hasData = true; 
    } 
    else 
    { 
    rowData.append(numberFormat.format(cell.getNumericCellValue())); 
    hasData = true; 
    } 
    break; 
    } 
    // Formula evaluation ends here 
    case XSSFCell.CELL_TYPE_STRING: 
    { 
    String stringVal = cell.getStringCellValue().trim().replaceAll("\n", ""); 
    if (stringVal.trim().equalsIgnoreCase("Total MoU/active customer")) 
    { 
    logger.warn("Last - KPI ::" + stringVal); 
    this.finalRecord = true; 
    ; 
    } 
    rowData.append(stringVal); 
    hasData = true; 
    break; 
    } 
    case XSSFCell.CELL_TYPE_ERROR: 
    { 
    int eval = cell.getErrorCellValue(); 
    if (eval == DIVIDE_BY_ZERO) 
    rowData.append("0"); 
    hasData = true; 
    break; 
    } 
    case XSSFCell.CELL_TYPE_BOOLEAN: 
    { 
    rowData.append(cell.getBooleanCellValue()); 
    hasData = true; 
    break; 
    } 
    } 
    rowData.append(FIELD_SEPARATOR); 
    return hasData; 
} 

當我運行該程序。在日期列中,我有一系列日期。沒有公式的最後一個日期是2009年12月31日它的單元格參考oj在Excel表格中我有Excel 2010年1月1日但Excel表格中我有29/04/2009我在這個單元格上打印了公式並找到出於此,poi 3.6在這方面有錯誤的單元格引用。而不是OJ + 1個公司給NE + 1

請幫我解決這個問題

回答

3

作爲你的例子是不完整的,我無法重現你的結果。下面是一個sscce產生預期的結果:

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.apache.poi.ss.usermodel.Cell; 
import org.apache.poi.ss.usermodel.CellValue; 
import org.apache.poi.ss.usermodel.DateUtil; 
import org.apache.poi.ss.usermodel.FormulaEvaluator; 
import org.apache.poi.ss.usermodel.Row; 

public class POIExcelReader { 

    public static void main(String[] args) throws IOException { 
     InputStream myxls = new FileInputStream("test.xls"); 
     HSSFWorkbook book = new HSSFWorkbook(myxls); 
     FormulaEvaluator eval = 
      book.getCreationHelper().createFormulaEvaluator(); 
     HSSFSheet sheet = book.getSheetAt(0); 
     for (Row row : sheet) { 
      for (Cell cell : row) { 
       printCell(cell, eval); 
       System.out.print("; "); 
      } 
      System.out.println(); 
     } 
     myxls.close(); 
    } 

    private static void printCell(Cell cell, FormulaEvaluator eval) { 
     switch (cell.getCellType()) { 
      case Cell.CELL_TYPE_BLANK: 
       System.out.print("EMPTY"); 
       break; 
      case Cell.CELL_TYPE_STRING: 
       System.out.print(cell.getStringCellValue()); 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       if (DateUtil.isCellDateFormatted(cell)) { 
        System.out.print(cell.getDateCellValue()); 
       } else { 
        System.out.print(cell.getNumericCellValue()); 
       } 
       break; 
      case Cell.CELL_TYPE_BOOLEAN: 
       System.out.print(cell.getBooleanCellValue()); 
       break; 
      case Cell.CELL_TYPE_FORMULA: 
       System.out.print(cell.getCellFormula()); 
       CellValue cellValue = eval.evaluate(cell); 
       switch (cellValue.getCellType()) { 
        case Cell.CELL_TYPE_NUMERIC: 
         double v = cellValue.getNumberValue(); 
         if (DateUtil.isCellDateFormatted(cell)) { 
          System.out.print(" = " 
           + DateUtil.getJavaDate(v, true)); 
         } else { 
          System.out.print(" = " + v); 
         } 
         break; 
       } 
       break; 
      default: 
       System.out.print("DEFAULT"); 
     } 
    } 
} 
 
Sun Jan 10 00:00:00 EST 2010; 1+1 = 2.0; 123.1; String1; true; 
A1+1 = Mon Jan 11 00:00:00 EST 2010; 2+2 = 4.0; 123.2; String2; false; 
A2+1 = Tue Jan 12 00:00:00 EST 2010; 3+3 = 6.0; 123.3; String3; true; 

附錄:使用Apache POI 3.8b3結果驗證。

+0

當使用XSSFWorkBook時,我看不到任何異常,如此處所述http://poi.apache.org/spreadsheet/converting.html – trashgod 2010-01-18 03:37:49

+2

在sscce中可能難以確定間歇性故障。您可能需要考慮同步問題的可能性。 – trashgod 2010-01-18 03:45:31

+0

是的,它大部分時間都會產生結果,但是當我編寫相同的代碼並將其用於XSSFWorkBook時,我得到了錯誤的結果,但是在大多數情況下我都無法理解這個問題。 – user252128 2010-01-17 18:06:21

1

我有一個類似的問題,當我正在閱讀的工作簿從硬編碼值轉換爲依賴於E7 + 7和下一個單元格F7 + 7的計算日期值時。它會跳5年了一下子......

我通過對每個單元使用evalInCell固定它讀這樣的前值:

if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) 
{ 
    cell = eval.evaluateInCell(cell); 
} 

希望這有助於!