gpt4 book ai didi

java - 无法使用 Apache POI 格式化导出的 Excel 中的日期单元格

转载 作者:行者123 更新时间:2023-12-02 01:02:21 28 4
gpt4 key购买 nike

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

这是我的单元格创建代码

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 时,我无法执行此操作。

enter image description here

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

最佳答案

我怀疑您的数据2016-01-28 12:06:00.0, ...是字符串而不是日期。如果您在 Excel 单元格中设置字符串,则该单元格不能是日期单元格。单元格值必须是数值才能使单元格成为日期单元格。因此,在设置单元格值之前,您需要将 String 转换为 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);
}

}
}

关于java - 无法使用 Apache POI 格式化导出的 Excel 中的日期单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60504291/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com