【问题标题】:Unable to format Date cell in exported excel using Apache POI无法使用 Apache POI 在导出的 Excel 中格式化日期单元格
【发布时间】:2020-06-15 15:51:32
【问题描述】:

我正在使用 Workbook wb = new XSSFWorkbook(); 导出一个 Excel 工作表,其中包含一个 DATE 列。即使将样式设置为 DATE,该列在 Excel 工作表中仍显示为 GENERAL

这是我创建单元格的一段代码

Workbook wb = new XSSFWorkbook(); 
int rowValue = 1; CellStyle cellStyleOfHeaderRow = wb.createCellStyle(); CellStyle style = wb.createCellStyle(); 
Font fontOfCell = initializeCellFont(wb, cellStyleOfHeaderRow); initializeCellBorders(cellStyleOfHeaderRow); initializeCellFillOptions(cellStyleOfHeaderRow); 
initializeCellBorders(style); CreationHelper createHelper = wb.getCreationHelper(); 
    short dateFormat = createHelper.createDataFormat().getFormat("yyyy-MM-dd-hh.mm.ss"); 
    style.setDataFormat(dateFormat);

打开导出的excel后,当我尝试将日期列的格式从GENERAL更改为DATE时,我无法这样做。

您能否建议一些代码或任何解决方案?

【问题讨论】:

  • 向我们展示您迄今为止尝试过的代码。
  • 你能先给我们看看你的代码吗?我的意思是应用单元格值和样式的那个...
  • 好吧,请edit 将代码添加到您的问题中,而不是将其作为评论发布。我在您的评论中看到的是一个非常奇怪的日期时间模式:yyyy-MM-dd-hh.mm.ss。小时的h 应该大写,我不会在白天和小时之间加上连字符。也许,您正在阻止 Excel 将其解释为有效的日期时间......它不仅仅是一个日期,因为它也包含有关一天中的时间的信息。
  • style 的代码看起来不错,能否请您说明如何将值和样式应用于日期单元格?

标签: java excel apache-poi


【解决方案1】:

我怀疑您的数据 2016-01-28 12:06:00.0, ... 是字符串而不是日期。如果您在Excel 单元格中设置字符串,则该单元格不能是日期单元格。单元格值必须是数值才能让单元格成为日期单元格。因此,在设置单元格值之前,您需要将 Strings 转换为 Dates。然后将 Date 设置为单元格值。

使用当前的apache poi 4.1.2 这可以使用java.time.format.DateTimeFormatterjava.time.LocalDateTime 来完成,因为现在有Cell.setCellValue(java.time.LocalDateTime value)

直到apache poi 3.17 可以使用java.text.SimpleDateFormatjava.util.Date 完成。 Cell.setCellValue(java.util.Date value) 是当时使用的 setCellValue 方法。

完整示例:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

class CreateExcelDateCells {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   String[][] data = new String[][] {
    new String[] {"Date"},
    new String[] {"2016-01-28 12:06:00.0"},
    new String[] {"2016-01-27 08:29:00.0"},
    new String[] {"2016-01-18 21:37:00.0"}
   };

   DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S", Locale.US);
   //SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.US); // up to apache poi 3.17

   CellStyle dateCellStyle = workbook.createCellStyle();
   dateCellStyle.setDataFormat(workbook.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));

   Sheet sheet = workbook.createSheet(); 

   for (int r = 0; r < data.length; r++) {
    Row row = sheet.createRow(r);
    Cell cell = row.createCell(0);
    if (r == 0) {
     cell.setCellValue(data[r][0]); // String cell value
    } else {
     cell.setCellValue(LocalDateTime.parse(data[r][0], dateTimeFormatter)); // Date cell value
     //cell.setCellValue(simpleDateFormatter.parse(data[r][0])); // Date cell value up to apache poi 3.17
     cell.setCellStyle(dateCellStyle);
    }
   }

   sheet.autoSizeColumn(0);

   workbook.write(fileout);
  }

 }
}

【讨论】:

  • SimpleDateFormat simpleDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S", Locale.US); 我尝试使用它并检查了我的 Time.equals(simpleDateFormatter),但它不起作用。
  • @champ.exe:我在回答中给出了一个完整的工作示例。这是经过测试和工作的。不清楚“我尝试使用它并检查了我的 Time.equals(simpleDateFormatter),但它不起作用。”方法。你到底试过什么?在您的情况下,Time 是什么?又该如何使用Time.equals(simpleDateFormatter)
  • 我有一个要添加的其他列的列表,所以我采用了一个包含日期、时间等的 Object[],所以我在这里尝试检查时间是否为指定的特定格式,我正在尝试将其转换为双精度,以便可以在 Excel 中进行修改。如果不是dateTimeFormat,例如Name,可以添加为String
  • @champ.exe:“我正在尝试检查时间是否为指定的特定格式”:Time 没有任何格式,除非有人对其进行格式化。所以总是不清楚你的问题在哪里。但这不是 cmets 可以在这里讨论的。
  • 时间与图片中提到的格式相同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
  • 2013-05-12
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多