【问题标题】:Get String value, when I read from excel with Date type (apache poi)获取字符串值,当我从 Excel 中读取日期类型(apache poi)时
【发布时间】:2013-10-22 18:00:03
【问题描述】:

我正在阅读一个 xlsx 文件 - Apache POI 库。

例如, 在某些行中,我有这样的单元格值:

2013 年 9 月 1 日 | 15136.00|马特|......


我的目标是: 将行中的所有单元格读取为字符串值。 但正如我所见,我无法将 01-Sep-13 读取为字符串, 它只用 cell.getDateCellValue() 表示像 Date 类型 ();


1) 我可以以某种方式将 01-Sep-13 读取为字符串:“01-Sep-13”; 2) 如果我有 不同的日期模式 (ddMMyyyy) 和 (dd-MMM-yy),我能否将日期值转换为字符串;

代码:

当我遍历行和单元格时,Apache POI 会分析每个单元格类型:

String normalizeCellType(Cell cell) {

    switch (cell.getCellType()) {
        case Cell.CELL_TYPE_STRING:
           return cell.getRichStringCellValue().getString());            
        case Cell.CELL_TYPE_NUMERIC:
            if (DateUtil.isCellDateFormatted(cell)) {

                Date date = cell.getDateCellValue(); ????????????
                System.out.println(date);

            } else {

                ....
            }
            break;
        case Cell.CELL_TYPE_BOOLEAN:
           return ....
        case Cell.CELL_TYPE_FORMULA:
           return ....
        default:
            System.out.println();
    }
}

【问题讨论】:

    标签: java apache-poi simpledateformat xssf


    【解决方案1】:

    您可以获取 cell#getDateCellValue() 的返回值并使用 SimpleDateFormat 将其转换为 String 实例

     // Create an instance of SimpleDateFormat used for formatting 
        // the string representation of date (month/day/year)
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
    
        // Get the date today using cell.getDateCellValue() 
        Date today = cell.getDateCellValue()       
        // Using DateFormat format method we can create a string 
        // representation of a date with the defined format.
        String reportDate = df.format(today);
    

    您可以获取字符串格式的日期值 - 31/10/2013。希望这会有所帮助

    【讨论】:

      【解决方案2】:

      你有两个选择:

      • 使用 Java 的 SimpleDateFormat class 将返回的 Date 格式化为您选择的格式的 String。您可以为所需的每种不同格式创建一个 SimpleDateFormat 实例。
      • 使用 Apache POI 的 DataFormatter class 直接格式化 Cell

      【讨论】:

      • 非常感谢方法。在类中 DateFormatter + .formatCellValue(cell) - 返回单元格的字符串表示形式
      【解决方案3】:

      以下是您问题的答案:

      1) 我可以以某种方式将 01-Sep-13 读取为字符串:“01-Sep-13”;

      是的,你可以。您需要在阅读之前将 Cell 类型设置为 String。像这样的:

      .
      .
      .
      int fcell = row.getFirstCellNum();// first cell number of excel
      int lcell = row.getLastCellNum(); //last cell number of excel
      while (rows.hasNext()) {
      row = (XSSFRow) rows.next();//increment the row iterator
      for (int i = fcell; i < lcell; i++) {
          row.getCell(i).setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
          .
          .
          ..//processing
          .
          .
          }
      }
      

      2.)如果我有不同的日期模式 (ddMMyyyy) 和 (dd-MMM-yy),我能否将日期值转换为字符串;

      因为第一个答案已经为您提供了 String 值。 仍然要将字符串值转换为日期,您可以使用 @rgettman 和 @Keerthi 建议的 SimpleDateFormat

      希望这会有所帮助...:)

      【讨论】:

        【解决方案4】:
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        java.util.Date d =  row1.getCell(i).getDateCellValue();
        String buy_date = df.format(d);
        System.out.println("date is :- "+ buy_date);
        

        输出将是

        date is :- 2014/08/01
        

        现在它将以String 格式提供日期,您可以将其存储在数据库中。

        【讨论】:

          【解决方案5】:

          Apache POI 具有为您执行此操作的功能,您只需调用它!您需要使用的类是DataFormatter。您将此单元格传递给它,它会尽力返回一个字符串,其中包含 Excel 会为您显示该单元格的内容。如果你给它传递一个字符串单元格,你会得到这个字符串。如果您传递一个应用了格式规则的数字单元格,它将根据它们格式化数字并返回字符串。如果您将“日期”单元格(具有日期格式规则的数字单元格)传递给它,它会将其格式化为日期并返回字符串

          您可以通过这段往返代码看到它的实际效果:

           DataFormat df = workBook.createDataFormat();
           CellStyle dateStyle = wb.createCellStye();
           setDataFormat.setDataFormat(df.getFormat("dd-mmm-yy"));
          
           Cell cell = row.createCell(0);
           cell.setCellStyle(dateStyle);
          
           // Set it to be a date
           Calendar c = Calendar.getInstance();
           c.set(2013,9-1,1); // Don't forget months are 0 based on Calendar
           cell.setCellValue( c.getTime() );
          
           // Get it formatted as a string
           DataFormatter formatter = new DataFormatter();
           String cellAsStr = formatter.formatCellValue(cell);
          
           // cellAsStr will now be "01-Sep-13"
           // based on the format rules applied to the cell
          

          【讨论】:

            【解决方案6】:

            我的目标是:将行中的所有单元格读取为字符串值。但正如我所见,我无法将 01-Sep-13 读取为字符串,它仅表示日期类型 () 与 cell.getDateCellValue();

            您可以使用 XSSFExcelExtractor 或 ExcelExtractor 来获取格式正确的单元格值。

            注意:- 1) XSSFExcelExtractor 用于 XLSX 文件 2) ExcelExtractor 用于 XLS 文件

            【讨论】:

              【解决方案7】:

              使用java读取excel表格中的字符串值非常容易。这是我的方式..

              Workbook wb=Workbook.getWorkbook(file);
                      Sheet s= wb.getSheet(1);
                      int row=s.getRows();
                      int col=s.getColumns();
                      for(int i=1;i<row;i++){
                          for(int j=0;j<col;j++){
                              Cell c=s.getCell(j,i);
                              String MyString=c.getContents().toString();
                              System.out.println(MyString);
                             }
                        }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2016-06-09
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-05-23
                • 2021-09-26
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多