【问题标题】:org.apache.poi create excel workbook with date cellsorg.apache.poi 创建带有日期单元格的 Excel 工作簿
【发布时间】:2014-07-26 07:27:06
【问题描述】:

我在 java 中有这种格式的日期:“2014.01.31 14:24:28”。我想使用具有单元格格式类型的 apache poi 库将其放入 excel:“日期”。 我怎样才能做到这一点?

【问题讨论】:

    标签: java excel apache date apache-poi


    【解决方案1】:

    试试这个

    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd"); HSSFCell myCell; myCell.setCellValue(dateFormat.format("2014.01.31 14:24:28"));

    【讨论】:

      【解决方案2】:

      检查here:将日期更改为您想要的格式并使用它。

      Workbook wb = new HSSFWorkbook();
      //Workbook wb = new XSSFWorkbook();
      CreationHelper createHelper = wb.getCreationHelper();
      Sheet sheet = wb.createSheet("new sheet");
      
      // Create a row and put some cells in it. Rows are 0 based.
      Row row = sheet.createRow(0);
      
      // Create a cell and put a date value in it.  The first cell is not styled
      // as a date.
      Cell cell = row.createCell(0);
      cell.setCellValue(new Date());
      
      // we style the second cell as a date (and time).  It is important to
      // create a new cell style from the workbook otherwise you can end up
      // modifying the built in style and effecting not only this cell but other cells.
      CellStyle cellStyle = wb.createCellStyle();
      cellStyle.setDataFormat(
          createHelper.createDataFormat().getFormat("yyyy/mm/dd hh:mm:ss"));
      cell = row.createCell(1);
      cell.setCellValue(new Date());
      cell.setCellStyle(cellStyle);
      
      //you can also set date as java.util.Calendar
      cell = row.createCell(2);
      cell.setCellValue(Calendar.getInstance());
      cell.setCellStyle(cellStyle);
      
      // Write the output to a file
      FileOutputStream fileOut = new FileOutputStream("workbook.xls");
      wb.write(fileOut);
      fileOut.close();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-26
        • 2011-07-09
        相关资源
        最近更新 更多