2015-12-10 227 views
-1

在處理數字單元我想讀取Excel電子表格的格式文本,不串單元格值,但當細胞類型是數字異常被拋出,我使用cell.getRichStringCellValue () 方法 。處理這個問題的好方法是什麼?使用Apache POI將單元格內容讀爲富文本。使用cell.getRichStringCellValue()方法

+1

你可能想閱讀[如何提問](HTTP:// stackoverflow.com/help/how-to-ask)部分。請發佈您的代碼和例外情況。我們不介意讀者知道發生了什麼。 :) – tftd

回答

1

你需要follow the approach carefully and lovingly laid out in the Apache POI documentation(誰想得到?!)。你會想要做的事,如:

import org.apache.poi.ss.usermodel.*; 

Workbook wb = WorkbookFactory.create(new File("input.xls")); 
Sheet sheet1 = wb.getSheetAt(0); 
for (Row row : sheet1) { 
    for (Cell cell : row) { 
     CellReference cellRef = new CellReference(row.getRowNum(), cell.getColumnIndex()); 
     System.out.print(cellRef.formatAsString()); 
     System.out.print(" - "); 

     switch (cell.getCellType()) { 
      case Cell.CELL_TYPE_STRING: 
       RichTextString contents = cell.getRichStringCellValue(); 
       // TODO Handle contents 
       break; 
      case Cell.CELL_TYPE_NUMERIC: 
       if (DateUtil.isCellDateFormatted(cell)) { 
        Date date = cell.getDateCellValue(); 
        // TODO Handle Date value 
       } else { 
        double number = cell.getNumericCellValue(); 
        // TODO Handle number 
       } 
       break; 
      case Cell.CELL_TYPE_BOOLEAN: 
       boolean value = cell.getBooleanCellValue(); 
       // TODO Handle 
       break; 
      case Cell.CELL_TYPE_FORMULA: 
       // Either get formula, or check last value, or evaluate 
       break; 
      default: 
       // Shouldn't happen 
     } 
    } 
} 

然後加上自己的邏輯,現在處理的內容,你已經賺得了他們

相關問題