[英]Apache POI: How do I set the dataFormat of a cell based on a Java DateTimeFormatter
我需要使用主機操作系統上指定的格式將日期和時間數據導出到 Excel。
我發現在 Java 中獲得這種格式的唯一方法是使用 DateTimeFormatter。 然后我需要使用 Apache POI 設置 Excel 單元格的數據格式。 我怎么做?
要為 CellStyle 中的 setDataFormat 方法獲取數據格式,我必須有一個格式字符串(或表示內置類型的 short),但我無法從 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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.