gpt4 book ai didi

java - Apache POI : How do I set the dataFormat of a cell based on a Java DateTimeFormatter

转载 作者:行者123 更新时间:2023-11-30 05:30:01 25 4
gpt4 key购买 nike

我需要使用主机操作系统上指定的格式将日期和时间数据导出到 Excel。

我发现在 Java 中获取此格式的唯一方法是使用 DateTimeFormatter。然后,我需要使用 Apache POI 设置 Excel 单元格的数据格式。我该怎么做?

要获取 CellStyle 中 setDataFormat 方法的数据格式,我必须有一个格式字符串(或表示内置类型的短格式),但我无法从 DateTimeFormatter 中获取字符串模式。有什么办法可以实现这种转换吗?

final SXSSFWorkbook workbook;
final CellStyle style;
final DataFormat formatFactory;

style = workbook.createCellStyle();
DateTimeFormatter format = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT); //Format depends on local settings on client machine

style.setDataFormat(formatFactory.getFormat(format)); //Doesn't work

最佳答案

可以使用 java.text.DateFormat 来实现此要求,如下所示:

DateFormat format = DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT,
Locale.getDefault());
String pattern = ((SimpleDateFormat)format).toPattern();
System.out.println(pattern);

或者像这样使用java.time.format.DateTimeFormatterBuilder:

String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT, FormatStyle.SHORT,
Chronology.ofLocale(Locale.getDefault()),
Locale.getDefault());
System.out.println(pattern);

在这两种情况下,都需要使用 DateFormatConverter 转换模式以便在 Excel 中使用。像这样:

pattern = DateFormatConverter.convert(Locale.getDefault(), pattern);
System.out.println(pattern);

完整示例:

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

import java.util.Date;
import java.util.Locale;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.FormatStyle;
import java.time.chrono.Chronology;

class CreateExcelCellDateFormat {

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

//Locale.setDefault(new Locale("en", "US"));

System.out.println(Locale.getDefault());

String pattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT, FormatStyle.SHORT,
Chronology.ofLocale(Locale.getDefault()), Locale.getDefault());
System.out.println(pattern);

/*
DateFormat format = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
String pattern = ((SimpleDateFormat)format).toPattern();
System.out.println(pattern);
*/

pattern = DateFormatConverter.convert(Locale.getDefault(), pattern);
System.out.println(pattern);

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

CellStyle style = workbook.createCellStyle();
style.setDataFormat(workbook.createDataFormat().getFormat(pattern));

Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(style);
cell.setCellValue(new Date());

sheet.setColumnWidth(0, 25 * 256);

workbook.write(fileout);
}

}
}
<小时/>

但是,如果真正的要求是创建一个 Excel 工作簿,根据 Excel 应用程序 运行所在的用户区域设置来显示日期时间值,那么这一切都不是必要的。然后使用 BuiltinFormats 0xe,“m/d/yy”表示短日期,或 0x16,“m/d/yy h:mm”表示短日期时间。

示例:

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

class CreateExcelCellDateFormatUserLocale {

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

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

CellStyle style = workbook.createCellStyle();
//style.setDataFormat((short)14); //0xe, "m/d/yy"
style.setDataFormat((short)22); //0x16, "m/d/yy h:mm"

Sheet sheet = workbook.createSheet();
Cell cell = sheet.createRow(0).createCell(0);
cell.setCellStyle(style);
cell.setCellValue(new java.util.Date());

sheet.setColumnWidth(0, 25 * 256);

workbook.write(fileout);
}

}
}

该代码生成一个 Excel 文件,该文件显示的日期时间与 Excel 完全一样,具体取决于 Excel 应用程序的区域设置 运行。对于德国,它显示 dd.MM.yyyy hh:mm。对于美国,它显示 MM/dd/yy h:m AM/PM。对于英国,它显示 dd/MM/yyyy hh:mm。

关于java - Apache POI : How do I set the dataFormat of a cell based on a Java DateTimeFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57775978/

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