Lines 42-119
Link Here
|
42 |
private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { |
42 |
private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception { |
43 |
|
43 |
|
44 |
Workbook workbook = new HSSFWorkbook(); |
44 |
Workbook workbook = new HSSFWorkbook(); |
45 |
String sheetName; |
45 |
try { |
46 |
if( dates ) { |
46 |
String sheetName; |
47 |
if( times ) { |
47 |
if( dates ) { |
48 |
sheetName = "DateTimes"; |
48 |
if( times ) { |
|
|
49 |
sheetName = "DateTimes"; |
50 |
} else { |
51 |
sheetName = "Dates"; |
52 |
} |
49 |
} else { |
53 |
} else { |
50 |
sheetName = "Dates"; |
54 |
sheetName = "Times"; |
51 |
} |
55 |
} |
52 |
} else { |
56 |
Sheet sheet = workbook.createSheet(sheetName); |
53 |
sheetName = "Times"; |
57 |
Row header = sheet.createRow(0); |
54 |
} |
58 |
header.createCell(0).setCellValue("locale"); |
55 |
Sheet sheet = workbook.createSheet(sheetName); |
59 |
header.createCell(1).setCellValue("DisplayName"); |
56 |
Row header = sheet.createRow(0); |
60 |
header.createCell(2).setCellValue("Excel " + styleName); |
57 |
header.createCell(0).setCellValue("locale"); |
61 |
header.createCell(3).setCellValue("java.text.DateFormat"); |
58 |
header.createCell(1).setCellValue("DisplayName"); |
62 |
header.createCell(4).setCellValue("Equals"); |
59 |
header.createCell(2).setCellValue("Excel " + styleName); |
63 |
header.createCell(5).setCellValue("Java pattern"); |
60 |
header.createCell(3).setCellValue("java.text.DateFormat"); |
64 |
header.createCell(6).setCellValue("Excel pattern"); |
61 |
header.createCell(4).setCellValue("Equals"); |
|
|
62 |
header.createCell(5).setCellValue("Java pattern"); |
63 |
header.createCell(6).setCellValue("Excel pattern"); |
64 |
|
65 |
int rowNum = 1; |
66 |
for( Locale locale : DateFormat.getAvailableLocales() ) { |
67 |
try { |
68 |
Row row = sheet.createRow(rowNum++); |
69 |
|
65 |
|
70 |
row.createCell(0).setCellValue(locale.toString()); |
66 |
int rowNum = 1; |
71 |
row.createCell(1).setCellValue(locale.getDisplayName()); |
67 |
for( Locale locale : DateFormat.getAvailableLocales() ) { |
72 |
|
68 |
try { |
73 |
DateFormat dateFormat; |
69 |
Row row = sheet.createRow(rowNum++); |
74 |
if( dates ) { |
70 |
|
75 |
if( times ) { |
71 |
row.createCell(0).setCellValue(locale.toString()); |
76 |
dateFormat = DateFormat.getDateTimeInstance(style, style, locale); |
72 |
row.createCell(1).setCellValue(locale.getDisplayName()); |
|
|
73 |
|
74 |
DateFormat dateFormat; |
75 |
if( dates ) { |
76 |
if( times ) { |
77 |
dateFormat = DateFormat.getDateTimeInstance(style, style, locale); |
78 |
} else { |
79 |
dateFormat = DateFormat.getDateInstance(style, locale); |
80 |
} |
77 |
} else { |
81 |
} else { |
78 |
dateFormat = DateFormat.getDateInstance(style, locale); |
82 |
dateFormat = DateFormat.getTimeInstance(style, locale); |
79 |
} |
83 |
} |
80 |
} else { |
84 |
|
81 |
dateFormat = DateFormat.getTimeInstance(style, locale); |
85 |
Cell cell = row.createCell(2); |
|
|
86 |
|
87 |
cell.setCellValue(date); |
88 |
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); |
89 |
|
90 |
String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); |
91 |
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); |
92 |
|
93 |
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); |
94 |
cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); |
95 |
row.createCell(3).setCellValue(dateFormat.format(date)); |
96 |
|
97 |
cell.setCellStyle(cellStyle); |
98 |
|
99 |
// the formula returns TRUE is the formatted date in column C equals to the string in column D |
100 |
row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); |
101 |
row.createCell(5).setCellValue(javaDateFormatPattern); |
102 |
row.createCell(6).setCellValue(excelFormatPattern); |
103 |
} catch (Exception e) { |
104 |
throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + |
105 |
Arrays.toString(DateFormat.getAvailableLocales()), e); |
82 |
} |
106 |
} |
|
|
107 |
} |
83 |
|
108 |
|
84 |
Cell cell = row.createCell(2); |
109 |
File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); |
85 |
|
110 |
FileOutputStream outputStream = new FileOutputStream(outputFile); |
86 |
cell.setCellValue(date); |
111 |
try { |
87 |
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle(); |
112 |
workbook.write(outputStream); |
88 |
|
113 |
} finally { |
89 |
String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern(); |
114 |
outputStream.close(); |
90 |
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern); |
|
|
91 |
|
92 |
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat(); |
93 |
cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern)); |
94 |
row.createCell(3).setCellValue(dateFormat.format(date)); |
95 |
|
96 |
cell.setCellStyle(cellStyle); |
97 |
|
98 |
// the formula returns TRUE is the formatted date in column C equals to the string in column D |
99 |
row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum); |
100 |
row.createCell(5).setCellValue(javaDateFormatPattern); |
101 |
row.createCell(6).setCellValue(excelFormatPattern); |
102 |
} catch (Exception e) { |
103 |
throw new RuntimeException("Failed for locale: " + locale + ", having locales: " + |
104 |
Arrays.toString(DateFormat.getAvailableLocales()), e); |
105 |
} |
115 |
} |
|
|
116 |
|
117 |
// FIXME: can testing the output file be automated by POI? |
118 |
// if not, should this message be sent to POILogger so as not to clutter ant test output? |
119 |
System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel"); |
106 |
} |
120 |
} |
107 |
|
121 |
finally { |
108 |
File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx"); |
122 |
workbook.close(); |
109 |
FileOutputStream outputStream = new FileOutputStream(outputFile); |
|
|
110 |
try { |
111 |
workbook.write(outputStream); |
112 |
} finally { |
113 |
outputStream.close(); |
114 |
} |
123 |
} |
115 |
|
|
|
116 |
System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel"); |
117 |
} |
124 |
} |
118 |
|
125 |
|
119 |
public void testJavaDateFormatsInExcel() throws Exception { |
126 |
public void testJavaDateFormatsInExcel() throws Exception { |