how to read exact cell content of excel file in apache POI

前端 未结 2 1897
鱼传尺愫
鱼传尺愫 2020-11-30 15:48

when I read a content of cell for e.g. if it is in date format it casts into some another value like 12/31/2099 -> 46052 and $50.00 -> 50 and 50.00% -> 0.5.

But what

2条回答
  •  眼角桃花
    2020-11-30 16:21

    I would not try and alter the data type while I am reading.

    Instead of that, I would try to

    • get the cell type first of all
    • then use the appropiate getter method for each data type

    For example:

    // Let's say you have prepared a DecimalFormat yourNumberFormatter and a
    // DateFormat yourDateFormatter with the format that is appropiate to your
    // presentation 
    Cell cell = ...;
    String value = null;
    switch (cell.getCellType()) {
       case Cell.TYPE_NUMERIC:
         // Date format
         if ( DateUtil.isCellDateFormatted(cell) ) {
            value = yourDateFormatter.format(cell.getDateCellValue());
         }
         else {
            value = yourNumberFormatter.format(cell.getNumericCellValue());
         }
         break;
       case Cell.TYPE_STRING:
         value = cell.getStringCellValue();
         break;
      // Etc.
    }
    

提交回复
热议问题