*/
public class NumberComparingSpreadsheetGenerator {
- private static final class SheetWriter {
-
- private final HSSFSheet _sheet;
- private int _rowIndex;
-
- public SheetWriter(HSSFWorkbook wb) {
- HSSFSheet sheet = wb.createSheet("Sheet1");
-
- writeHeaderRow(wb, sheet);
- _sheet = sheet;
- _rowIndex = 1;
- }
-
- public void addTestRow(double a, double b, int expResult) {
- writeDataRow(_sheet, _rowIndex++, a, b, expResult);
- }
- }
-
- private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
- HSSFCell cell = row.createCell(i);
- cell.setCellValue(new HSSFRichTextString(text));
- cell.setCellStyle(style);
- }
- static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) {
- sheet.setColumnWidth(0, 6000);
- sheet.setColumnWidth(1, 6000);
- sheet.setColumnWidth(2, 3600);
- sheet.setColumnWidth(3, 3600);
- sheet.setColumnWidth(4, 2400);
- sheet.setColumnWidth(5, 2400);
- sheet.setColumnWidth(6, 2400);
- sheet.setColumnWidth(7, 2400);
- sheet.setColumnWidth(8, 2400);
- HSSFRow row = sheet.createRow(0);
- HSSFCellStyle style = wb.createCellStyle();
- HSSFFont font = wb.createFont();
- font.setBold(true);
- style.setFont(font);
- writeHeaderCell(row, 0, "Raw Long Bits A", style);
- writeHeaderCell(row, 1, "Raw Long Bits B", style);
- writeHeaderCell(row, 2, "Value A", style);
- writeHeaderCell(row, 3, "Value B", style);
- writeHeaderCell(row, 4, "Exp Cmp", style);
- writeHeaderCell(row, 5, "LT", style);
- writeHeaderCell(row, 6, "EQ", style);
- writeHeaderCell(row, 7, "GT", style);
- writeHeaderCell(row, 8, "Check", style);
- }
- /**
- * Fills a spreadsheet row with one comparison example. The two numeric values are written to
- * columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"),
- * which will be evaluated by Excel. Column D gets the expected comparison result. Column I
- * gets a formula to check that Excel's comparison results match that predicted in column D.
- *
- * @param v0 the first value to be compared
- * @param v1 the second value to be compared
- * @param expRes expected comparison result (-1, 0, or +1)
- */
- static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) {
- HSSFRow row = sheet.createRow(rowIx);
-
- int rowNum = rowIx + 1;
-
-
- row.createCell(0).setCellValue(formatDoubleAsHex(v0));
- row.createCell(1).setCellValue(formatDoubleAsHex(v1));
- row.createCell(2).setCellValue(v0);
- row.createCell(3).setCellValue(v1);
- row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ");
- row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum);
- row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum);
- row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum);
- // TODO - bug elsewhere in POI - something wrong with encoding of NOT() function
- String frm = "if(or(" +
- "and(E#='LT', F# , G#=FALSE, H#=FALSE)," +
- "and(E#='EQ', F#=FALSE, G# , H#=FALSE)," +
- "and(E#='GT', F#=FALSE, G#=FALSE, H# )" +
- "), 'OK', 'error')" ;
- row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"'));
- }
-
- private static String formatDoubleAsHex(double d) {
- long l = Double.doubleToLongBits(d);
- return HexDump.longToHex(l)+'L';
- }
-
- public static void main(String[] args) {
-
- HSSFWorkbook wb = new HSSFWorkbook();
- SheetWriter sw = new SheetWriter(wb);
- ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
- for (ComparisonExample ce : ces) {
- sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
- }
-
-
- File outputFile = new File("ExcelNumberCompare.xls");
-
- try {
- FileOutputStream os = new FileOutputStream(outputFile);
- wb.write(os);
- os.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
- }
+ private static final class SheetWriter {
+
+ private final HSSFSheet _sheet;
+ private int _rowIndex;
+
+ public SheetWriter(HSSFWorkbook wb) {
+ HSSFSheet sheet = wb.createSheet("Sheet1");
+
+ writeHeaderRow(wb, sheet);
+ _sheet = sheet;
+ _rowIndex = 1;
+ }
+
+ public void addTestRow(double a, double b, int expResult) {
+ writeDataRow(_sheet, _rowIndex++, a, b, expResult);
+ }
+ }
+
+ private static void writeHeaderCell(HSSFRow row, int i, String text, HSSFCellStyle style) {
+ HSSFCell cell = row.createCell(i);
+ cell.setCellValue(new HSSFRichTextString(text));
+ cell.setCellStyle(style);
+ }
+ static void writeHeaderRow(HSSFWorkbook wb, HSSFSheet sheet) {
+ sheet.setColumnWidth(0, 6000);
+ sheet.setColumnWidth(1, 6000);
+ sheet.setColumnWidth(2, 3600);
+ sheet.setColumnWidth(3, 3600);
+ sheet.setColumnWidth(4, 2400);
+ sheet.setColumnWidth(5, 2400);
+ sheet.setColumnWidth(6, 2400);
+ sheet.setColumnWidth(7, 2400);
+ sheet.setColumnWidth(8, 2400);
+ HSSFRow row = sheet.createRow(0);
+ HSSFCellStyle style = wb.createCellStyle();
+ HSSFFont font = wb.createFont();
+ font.setBold(true);
+ style.setFont(font);
+ writeHeaderCell(row, 0, "Raw Long Bits A", style);
+ writeHeaderCell(row, 1, "Raw Long Bits B", style);
+ writeHeaderCell(row, 2, "Value A", style);
+ writeHeaderCell(row, 3, "Value B", style);
+ writeHeaderCell(row, 4, "Exp Cmp", style);
+ writeHeaderCell(row, 5, "LT", style);
+ writeHeaderCell(row, 6, "EQ", style);
+ writeHeaderCell(row, 7, "GT", style);
+ writeHeaderCell(row, 8, "Check", style);
+ }
+ /**
+ * Fills a spreadsheet row with one comparison example. The two numeric values are written to
+ * columns C and D. Columns (F, G and H) respectively get formulas ("v0<v1", "v0=v1", "v0>v1"),
+ * which will be evaluated by Excel. Column D gets the expected comparison result. Column I
+ * gets a formula to check that Excel's comparison results match that predicted in column D.
+ *
+ * @param v0 the first value to be compared
+ * @param v1 the second value to be compared
+ * @param expRes expected comparison result (-1, 0, or +1)
+ */
+ static void writeDataRow(HSSFSheet sheet, int rowIx, double v0, double v1, int expRes) {
+ HSSFRow row = sheet.createRow(rowIx);
+
+ int rowNum = rowIx + 1;
+
+
+ row.createCell(0).setCellValue(formatDoubleAsHex(v0));
+ row.createCell(1).setCellValue(formatDoubleAsHex(v1));
+ row.createCell(2).setCellValue(v0);
+ row.createCell(3).setCellValue(v1);
+ row.createCell(4).setCellValue(expRes < 0 ? "LT" : expRes > 0 ? "GT" : "EQ");
+ row.createCell(5).setCellFormula("C" + rowNum + "<" + "D" + rowNum);
+ row.createCell(6).setCellFormula("C" + rowNum + "=" + "D" + rowNum);
+ row.createCell(7).setCellFormula("C" + rowNum + ">" + "D" + rowNum);
+ // TODO - bug elsewhere in POI - something wrong with encoding of NOT() function
+ String frm = "if(or(" +
+ "and(E#='LT', F# , G#=FALSE, H#=FALSE)," +
+ "and(E#='EQ', F#=FALSE, G# , H#=FALSE)," +
+ "and(E#='GT', F#=FALSE, G#=FALSE, H# )" +
+ "), 'OK', 'error')" ;
+ row.createCell(8).setCellFormula(frm.replaceAll("#", String.valueOf(rowNum)).replace('\'', '"'));
+ }
+
+ private static String formatDoubleAsHex(double d) {
+ long l = Double.doubleToLongBits(d);
+ return HexDump.longToHex(l)+'L';
+ }
+
+ public static void main(String[] args) throws IOException {
+ try (HSSFWorkbook wb = new HSSFWorkbook()) {
+ SheetWriter sw = new SheetWriter(wb);
+ ComparisonExample[] ces = NumberComparisonExamples.getComparisonExamples();
+ for (ComparisonExample ce : ces) {
+ sw.addTestRow(ce.getA(), ce.getB(), ce.getExpectedResult());
+ }
+
+ File outputFile = new File("ExcelNumberCompare.xls");
+ try (FileOutputStream os = new FileOutputStream(outputFile)) {
+ wb.write(os);
+ }
+ System.out.println("Finished writing '" + outputFile.getAbsolutePath() + "'");
+ }
+ }
}
public final class TestDateFormatConverter extends TestCase {
private void outputLocaleDataFormats( Date date, boolean dates, boolean times, int style, String styleName ) throws Exception {
- Workbook workbook = new HSSFWorkbook();
- try {
+ try (Workbook workbook = new HSSFWorkbook()) {
String sheetName;
- if( dates ) {
- if( times ) {
+ if (dates) {
+ if (times) {
sheetName = "DateTimes";
} else {
sheetName = "Dates";
header.createCell(4).setCellValue("Equals");
header.createCell(5).setCellValue("Java pattern");
header.createCell(6).setCellValue("Excel pattern");
-
+
int rowNum = 1;
- for( Locale locale : DateFormat.getAvailableLocales() ) {
+ for (Locale locale : DateFormat.getAvailableLocales()) {
try {
Row row = sheet.createRow(rowNum++);
-
+
row.createCell(0).setCellValue(locale.toString());
row.createCell(1).setCellValue(locale.getDisplayName(Locale.ROOT));
-
+
DateFormat dateFormat;
- if( dates ) {
- if( times ) {
+ if (dates) {
+ if (times) {
dateFormat = DateFormat.getDateTimeInstance(style, style, locale);
} else {
dateFormat = DateFormat.getDateInstance(style, locale);
} else {
dateFormat = DateFormat.getTimeInstance(style, locale);
}
-
+
Cell cell = row.createCell(2);
-
+
cell.setCellValue(date);
CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
-
- String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
+
+ String javaDateFormatPattern = ((SimpleDateFormat) dateFormat).toPattern();
String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
-
+
DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
row.createCell(3).setCellValue(dateFormat.format(date));
-
+
cell.setCellStyle(cellStyle);
-
+
// the formula returns TRUE is the formatted date in column C equals to the string in column D
- row.createCell(4).setCellFormula("TEXT(C"+rowNum+",G"+rowNum+")=D" + rowNum);
+ row.createCell(4).setCellFormula("TEXT(C" + rowNum + ",G" + rowNum + ")=D" + rowNum);
row.createCell(5).setCellValue(javaDateFormatPattern);
row.createCell(6).setCellValue(excelFormatPattern);
} catch (Exception e) {
- throw new RuntimeException("Failed for locale: " + locale + ", having locales: " +
+ throw new RuntimeException("Failed for locale: " + locale + ", having locales: " +
Arrays.toString(DateFormat.getAvailableLocales()), e);
}
}
-
+
File outputFile = TempFile.createTempFile("Locale" + sheetName + styleName, ".xlsx");
- FileOutputStream outputStream = new FileOutputStream(outputFile);
- try {
+ try (FileOutputStream outputStream = new FileOutputStream(outputFile)) {
workbook.write(outputStream);
- } finally {
- outputStream.close();
}
-
- System.out.println("Open " + outputFile.getAbsolutePath()+" in Excel");
- } finally {
- workbook.close();
+
+ System.out.println("Open " + outputFile.getAbsolutePath() + " in Excel");
}
}