Fix inconsistent whitespace

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1710395 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-10-24 22:12:06 +00:00
parent e0addf54a5
commit cbde002fa1
1 changed files with 348 additions and 349 deletions

View File

@ -41,409 +41,408 @@ import org.junit.Test;
* Unit tests for HSSFDataFormatter.java * Unit tests for HSSFDataFormatter.java
*/ */
public final class TestHSSFDataFormatter { public final class TestHSSFDataFormatter {
private static TimeZone userTimeZone; private static TimeZone userTimeZone;
@BeforeClass @BeforeClass
public static void setTimeZone() { public static void setTimeZone() {
userTimeZone = LocaleUtil.getUserTimeZone(); userTimeZone = LocaleUtil.getUserTimeZone();
LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET")); LocaleUtil.setUserTimeZone(TimeZone.getTimeZone("CET"));
LocaleUtil.setUserLocale(Locale.US); LocaleUtil.setUserLocale(Locale.US);
} }
@AfterClass @AfterClass
public static void resetTimeZone() { public static void resetTimeZone() {
LocaleUtil.setUserTimeZone(userTimeZone); LocaleUtil.setUserTimeZone(userTimeZone);
LocaleUtil.setUserLocale(Locale.ROOT); LocaleUtil.setUserLocale(Locale.ROOT);
} }
private final HSSFDataFormatter formatter; private final HSSFDataFormatter formatter;
private final HSSFWorkbook wb; private final HSSFWorkbook wb;
public TestHSSFDataFormatter() { public TestHSSFDataFormatter() {
// create the formatter to test // create the formatter to test
formatter = new HSSFDataFormatter(); formatter = new HSSFDataFormatter();
// create a workbook to test with // create a workbook to test with
wb = new HSSFWorkbook(); wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet(); HSSFSheet sheet = wb.createSheet();
HSSFDataFormat format = wb.createDataFormat(); HSSFDataFormat format = wb.createDataFormat();
// create a row and put some cells in it // create a row and put some cells in it
HSSFRow row = sheet.createRow(0); HSSFRow row = sheet.createRow(0);
// date value for July 8 1901 1:19 PM // date value for July 8 1901 1:19 PM
double dateNum = 555.555; double dateNum = 555.555;
// date value for July 8 1901 11:23 AM // date value for July 8 1901 11:23 AM
double timeNum = 555.47431; double timeNum = 555.47431;
//valid date formats -- all should have "Jul" in output //valid date formats -- all should have "Jul" in output
String[] goodDatePatterns = { String[] goodDatePatterns = {
"[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy", "[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy",
"mmm/d/yy\\ h:mm PM;@", "mmm/d/yy\\ h:mm PM;@",
"mmmm/d/yy\\ h:mm;@", "mmmm/d/yy\\ h:mm;@",
"mmmm/d;@", "mmmm/d;@",
"mmmm/d/yy;@", "mmmm/d/yy;@",
"mmm/dd/yy;@", "mmm/dd/yy;@",
"[$-409]d\\-mmm;@", "[$-409]d\\-mmm;@",
"[$-409]d\\-mmm\\-yy;@", "[$-409]d\\-mmm\\-yy;@",
"[$-409]dd\\-mmm\\-yy;@", "[$-409]dd\\-mmm\\-yy;@",
"[$-409]mmm\\-yy;@", "[$-409]mmm\\-yy;@",
"[$-409]mmmm\\-yy;@", "[$-409]mmmm\\-yy;@",
"[$-409]mmmm\\ d\\,\\ yyyy;@", "[$-409]mmmm\\ d\\,\\ yyyy;@",
"[$-409]mmm/d/yy\\ h:mm:ss;@", "[$-409]mmm/d/yy\\ h:mm:ss;@",
"[$-409]mmmm/d/yy\\ h:mm:ss am;@", "[$-409]mmmm/d/yy\\ h:mm:ss am;@",
"[$-409]mmmmm;@", "[$-409]mmmmm;@",
"[$-409]mmmmm\\-yy;@", "[$-409]mmmmm\\-yy;@",
"mmmm/d/yyyy;@", "mmmm/d/yyyy;@",
"[$-409]d\\-mmm\\-yyyy;@" "[$-409]d\\-mmm\\-yyyy;@"
}; };
//valid time formats - all should have 11:23 in output
String[] goodTimePatterns = {
"HH:MM",
"HH:MM:SS",
"HH:MM;HH:MM;HH:MM",
// This is fun - blue if positive time,
// red if negative time or green for zero!
"[BLUE]HH:MM;[RED]HH:MM;[GREEN]HH:MM",
"yyyy-mm-dd hh:mm",
"yyyy-mm-dd hh:mm:ss",
};
// valid number formats //valid time formats - all should have 11:23 in output
String[] goodNumPatterns = { String[] goodTimePatterns = {
"#,##0.0000", "HH:MM",
"#,##0;[Red]#,##0", "HH:MM:SS",
"(#,##0.00_);(#,##0.00)", "HH:MM;HH:MM;HH:MM",
"($#,##0.00_);[Red]($#,##0.00)", // This is fun - blue if positive time,
"$#,##0.00", // red if negative time or green for zero!
"[$-809]#,##0.00", // international format "[BLUE]HH:MM;[RED]HH:MM;[GREEN]HH:MM",
"[$-2]#,##0.00", // international format "yyyy-mm-dd hh:mm",
"0000.00000%", "yyyy-mm-dd hh:mm:ss",
"0.000E+00", };
"0.00E+00",
"[BLACK]0.00;[COLOR 5]##.##",
"[>999999]#,,\"M\";[>999]#,\"K\";#", // num/K/M
"[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000", // with decimals
};
// invalid date formats -- will throw exception in DecimalFormat ctor // valid number formats
String[] badNumPatterns = { String[] goodNumPatterns = {
"#,#$'#0.0000", "#,##0.0000",
"'#','#ABC#0;##,##0", "#,##0;[Red]#,##0",
"000 '123 4'5'6 000", "(#,##0.00_);(#,##0.00)",
"#''0#0'1#10L16EE" "($#,##0.00_);[Red]($#,##0.00)",
}; "$#,##0.00",
"[$-809]#,##0.00", // international format
"[$-2]#,##0.00", // international format
"0000.00000%",
"0.000E+00",
"0.00E+00",
"[BLACK]0.00;[COLOR 5]##.##",
"[>999999]#,,\"M\";[>999]#,\"K\";#", // num/K/M
"[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000", // with decimals
};
// create cells with good date patterns // invalid date formats -- will throw exception in DecimalFormat ctor
for (int i = 0; i < goodDatePatterns.length; i++) { String[] badNumPatterns = {
HSSFCell cell = row.createCell(i); "#,#$'#0.0000",
cell.setCellValue(dateNum); "'#','#ABC#0;##,##0",
HSSFCellStyle cellStyle = wb.createCellStyle(); "000 '123 4'5'6 000",
cellStyle.setDataFormat(format.getFormat(goodDatePatterns[i])); "#''0#0'1#10L16EE"
cell.setCellStyle(cellStyle); };
}
row = sheet.createRow(1);
// create cells with time patterns
for (int i = 0; i < goodTimePatterns.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(timeNum);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat(goodTimePatterns[i]));
cell.setCellStyle(cellStyle);
}
row = sheet.createRow(2);
// create cells with num patterns // create cells with good date patterns
for (int i = 0; i < goodNumPatterns.length; i++) { for (int i = 0; i < goodDatePatterns.length; i++) {
HSSFCell cell = row.createCell(i); HSSFCell cell = row.createCell(i);
cell.setCellValue(-1234567890.12345); cell.setCellValue(dateNum);
HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat(goodNumPatterns[i])); cellStyle.setDataFormat(format.getFormat(goodDatePatterns[i]));
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
} }
row = sheet.createRow(3); row = sheet.createRow(1);
// create cells with bad num patterns // create cells with time patterns
for (int i = 0; i < badNumPatterns.length; i++) { for (int i = 0; i < goodTimePatterns.length; i++) {
HSSFCell cell = row.createCell(i); HSSFCell cell = row.createCell(i);
// If the '.' is any later, ExcelGeneralNumberFormat will render an integer, as Excel does. cell.setCellValue(timeNum);
cell.setCellValue(12345678.9012345); HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(format.getFormat(goodTimePatterns[i]));
cellStyle.setDataFormat(format.getFormat(badNumPatterns[i])); cell.setCellStyle(cellStyle);
cell.setCellStyle(cellStyle); }
} row = sheet.createRow(2);
// Built in formats // create cells with num patterns
for (int i = 0; i < goodNumPatterns.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(-1234567890.12345);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat(goodNumPatterns[i]));
cell.setCellStyle(cellStyle);
}
row = sheet.createRow(3);
{ // Zip + 4 format // create cells with bad num patterns
row = sheet.createRow(4); for (int i = 0; i < badNumPatterns.length; i++) {
HSSFCell cell = row.createCell(0); HSSFCell cell = row.createCell(i);
cell.setCellValue(123456789); // If the '.' is any later, ExcelGeneralNumberFormat will render an integer, as Excel does.
HSSFCellStyle cellStyle = wb.createCellStyle(); cell.setCellValue(12345678.9012345);
cellStyle.setDataFormat(format.getFormat("00000-0000")); HSSFCellStyle cellStyle = wb.createCellStyle();
cell.setCellStyle(cellStyle); cellStyle.setDataFormat(format.getFormat(badNumPatterns[i]));
} cell.setCellStyle(cellStyle);
}
{ // Phone number format // Built in formats
row = sheet.createRow(5);
HSSFCell cell = row.createCell(0);
cell.setCellValue(5551234567D);
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat("[<=9999999]###-####;(###) ###-####"));
cell.setCellStyle(cellStyle);
}
{ // SSN format { // Zip + 4 format
row = sheet.createRow(6); row = sheet.createRow(4);
HSSFCell cell = row.createCell(0); HSSFCell cell = row.createCell(0);
cell.setCellValue(444551234); cell.setCellValue(123456789);
HSSFCellStyle cellStyle = wb.createCellStyle(); HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat("000-00-0000")); cellStyle.setDataFormat(format.getFormat("00000-0000"));
cell.setCellStyle(cellStyle); cell.setCellStyle(cellStyle);
} }
{ // formula cell { // Phone number format
row = sheet.createRow(7); row = sheet.createRow(5);
HSSFCell cell = row.createCell(0); HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA); cell.setCellValue(5551234567D);
cell.setCellFormula("SUM(12.25,12.25)/100"); HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFCellStyle cellStyle = wb.createCellStyle(); cellStyle.setDataFormat(format.getFormat("[<=9999999]###-####;(###) ###-####"));
cellStyle.setDataFormat(format.getFormat("##.00%;")); cell.setCellStyle(cellStyle);
cell.setCellStyle(cellStyle); }
}
}
/** { // SSN format
* Test getting formatted values from numeric and date cells. row = sheet.createRow(6);
*/ HSSFCell cell = row.createCell(0);
@Test cell.setCellValue(444551234);
public void testGetFormattedCellValueHSSFCell() { HSSFCellStyle cellStyle = wb.createCellStyle();
// Valid date formats -- cell values should be date formatted & not "555.555" cellStyle.setDataFormat(format.getFormat("000-00-0000"));
HSSFRow row = wb.getSheetAt(0).getRow(0); cell.setCellStyle(cellStyle);
Iterator<Cell> it = row.cellIterator(); }
log("==== VALID DATE FORMATS ====");
while (it.hasNext()) {
Cell cell = it.next();
String fmtval = formatter.formatCellValue(cell);
log(fmtval);
// should not be equal to "555.555" { // formula cell
assertTrue( DateUtil.isCellDateFormatted(cell) ); row = sheet.createRow(7);
assertTrue( ! "555.555".equals(fmtval)); HSSFCell cell = row.createCell(0);
cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
cell.setCellFormula("SUM(12.25,12.25)/100");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(format.getFormat("##.00%;"));
cell.setCellStyle(cellStyle);
}
}
String fmt = cell.getCellStyle().getDataFormatString(); /**
* Test getting formatted values from numeric and date cells.
*/
@Test
public void testGetFormattedCellValueHSSFCell() {
// Valid date formats -- cell values should be date formatted & not "555.555"
HSSFRow row = wb.getSheetAt(0).getRow(0);
Iterator<Cell> it = row.cellIterator();
log("==== VALID DATE FORMATS ====");
while (it.hasNext()) {
Cell cell = it.next();
String fmtval = formatter.formatCellValue(cell);
log(fmtval);
//assert the correct month form, as in the original Excel format // should not be equal to "555.555"
String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM"; assertTrue( DateUtil.isCellDateFormatted(cell) );
// this line is intended to compute how "July" would look like in the current locale assertTrue( ! "555.555".equals(fmtval));
SimpleDateFormat sdf = new SimpleDateFormat(monthPtrn, LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone());
Calendar calDef = LocaleUtil.getLocaleCalendar(2010, 6, 15, 0, 0, 0);
String jul = sdf.format(calDef.getTime());
// special case for MMMMM = 1st letter of month name
if(fmt.indexOf("mmmmm") > -1) {
jul = jul.substring(0,1);
}
// check we found july properly
assertTrue("Format came out incorrect - " + fmt, fmtval.indexOf(jul) > -1);
}
row = wb.getSheetAt(0).getRow(1); String fmt = cell.getCellStyle().getDataFormatString();
it = row.cellIterator();
log("==== VALID TIME FORMATS ====");
while (it.hasNext()) {
Cell cell = it.next();
String fmt = cell.getCellStyle().getDataFormatString();
String fmtval = formatter.formatCellValue(cell);
log(fmtval);
// should not be equal to "555.47431" //assert the correct month form, as in the original Excel format
assertTrue( DateUtil.isCellDateFormatted(cell) ); String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM";
assertTrue( ! "555.47431".equals(fmtval)); // this line is intended to compute how "July" would look like in the current locale
SimpleDateFormat sdf = new SimpleDateFormat(monthPtrn, LocaleUtil.getUserLocale());
sdf.setTimeZone(LocaleUtil.getUserTimeZone());
Calendar calDef = LocaleUtil.getLocaleCalendar(2010, 6, 15, 0, 0, 0);
String jul = sdf.format(calDef.getTime());
// special case for MMMMM = 1st letter of month name
if(fmt.indexOf("mmmmm") > -1) {
jul = jul.substring(0,1);
}
// check we found july properly
assertTrue("Format came out incorrect - " + fmt, fmtval.indexOf(jul) > -1);
}
// check we found the time properly row = wb.getSheetAt(0).getRow(1);
assertTrue("Format came out incorrect - " + fmt + ": " + fmtval + ", but expected to find '11:23'", it = row.cellIterator();
fmtval.indexOf("11:23") > -1); log("==== VALID TIME FORMATS ====");
} while (it.hasNext()) {
Cell cell = it.next();
// test number formats String fmt = cell.getCellStyle().getDataFormatString();
row = wb.getSheetAt(0).getRow(1); String fmtval = formatter.formatCellValue(cell);
it = row.cellIterator(); log(fmtval);
log("\n==== VALID NUMBER FORMATS ====");
while (it.hasNext()) {
HSSFCell cell = (HSSFCell) it.next();
final String formatted = formatter.formatCellValue(cell);
log(formatted);
// should not include "12345678" - note that the input value was negative // should not be equal to "555.47431"
assertTrue(formatted != null && ! formatted.contains("12345678")); assertTrue( DateUtil.isCellDateFormatted(cell) );
} assertTrue( ! "555.47431".equals(fmtval));
// test bad number formats // check we found the time properly
row = wb.getSheetAt(0).getRow(3); assertTrue("Format came out incorrect - " + fmt + ": " + fmtval + ", but expected to find '11:23'",
it = row.cellIterator(); fmtval.indexOf("11:23") > -1);
log("\n==== INVALID NUMBER FORMATS ===="); }
while (it.hasNext()) {
HSSFCell cell = (HSSFCell) it.next();
log(formatter.formatCellValue(cell));
// in some locales the the decimal delimiter is a comma, not a dot
char decimalSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getDecimalSeparator();
assertEquals("12345678" + decimalSeparator + "9", formatter.formatCellValue(cell));
}
// test Zip+4 format // test number formats
row = wb.getSheetAt(0).getRow(4); row = wb.getSheetAt(0).getRow(1);
HSSFCell cell = row.getCell(0); it = row.cellIterator();
log("\n==== ZIP FORMAT ===="); log("\n==== VALID NUMBER FORMATS ====");
log(formatter.formatCellValue(cell)); while (it.hasNext()) {
assertEquals("12345-6789", formatter.formatCellValue(cell)); HSSFCell cell = (HSSFCell) it.next();
final String formatted = formatter.formatCellValue(cell);
log(formatted);
// test phone number format // should not include "12345678" - note that the input value was negative
row = wb.getSheetAt(0).getRow(5); assertTrue(formatted != null && ! formatted.contains("12345678"));
cell = row.getCell(0); }
log("\n==== PHONE FORMAT ====");
log(formatter.formatCellValue(cell));
assertEquals("(555) 123-4567", formatter.formatCellValue(cell));
// test SSN format // test bad number formats
row = wb.getSheetAt(0).getRow(6); row = wb.getSheetAt(0).getRow(3);
cell = row.getCell(0); it = row.cellIterator();
log("\n==== SSN FORMAT ===="); log("\n==== INVALID NUMBER FORMATS ====");
log(formatter.formatCellValue(cell)); while (it.hasNext()) {
assertEquals("444-55-1234", formatter.formatCellValue(cell)); HSSFCell cell = (HSSFCell) it.next();
log(formatter.formatCellValue(cell));
// in some locales the the decimal delimiter is a comma, not a dot
char decimalSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getDecimalSeparator();
assertEquals("12345678" + decimalSeparator + "9", formatter.formatCellValue(cell));
}
// null test-- null cell should result in empty String // test Zip+4 format
assertEquals(formatter.formatCellValue(null), ""); row = wb.getSheetAt(0).getRow(4);
HSSFCell cell = row.getCell(0);
log("\n==== ZIP FORMAT ====");
log(formatter.formatCellValue(cell));
assertEquals("12345-6789", formatter.formatCellValue(cell));
// null test-- null cell should result in empty String // test phone number format
assertEquals(formatter.formatCellValue(null), ""); row = wb.getSheetAt(0).getRow(5);
} cell = row.getCell(0);
log("\n==== PHONE FORMAT ====");
log(formatter.formatCellValue(cell));
assertEquals("(555) 123-4567", formatter.formatCellValue(cell));
@Test // test SSN format
public void testGetFormattedCellValueHSSFCellHSSFFormulaEvaluator() { row = wb.getSheetAt(0).getRow(6);
// test formula format cell = row.getCell(0);
HSSFRow row = wb.getSheetAt(0).getRow(7); log("\n==== SSN FORMAT ====");
HSSFCell cell = row.getCell(0); log(formatter.formatCellValue(cell));
log("\n==== FORMULA CELL ===="); assertEquals("444-55-1234", formatter.formatCellValue(cell));
// first without a formula evaluator // null test-- null cell should result in empty String
log(formatter.formatCellValue(cell) + "\t (without evaluator)"); assertEquals(formatter.formatCellValue(null), "");
assertEquals("SUM(12.25,12.25)/100", formatter.formatCellValue(cell));
// now with a formula evaluator // null test-- null cell should result in empty String
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb); assertEquals(formatter.formatCellValue(null), "");
log(formatter.formatCellValue(cell, evaluator) + "\t\t\t (with evaluator)"); }
char decimalSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getDecimalSeparator();
assertEquals("24" + decimalSeparator + "50%", formatter.formatCellValue(cell,evaluator));
}
/** @Test
* Test using a default number format. The format should be used when a public void testGetFormattedCellValueHSSFCellHSSFFormulaEvaluator() {
* format pattern cannot be parsed by DecimalFormat. // test formula format
*/ HSSFRow row = wb.getSheetAt(0).getRow(7);
@Test HSSFCell cell = row.getCell(0);
public void testSetDefaultNumberFormat() { log("\n==== FORMULA CELL ====");
HSSFRow row = wb.getSheetAt(0).getRow(3);
Iterator<Cell> it = row.cellIterator();
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
Format defaultFormat = new DecimalFormat("Balance $#,#00.00 USD;Balance -$#,#00.00 USD", dfs);
formatter.setDefaultNumberFormat(defaultFormat);
log("\n==== DEFAULT NUMBER FORMAT ===="); // first without a formula evaluator
while (it.hasNext()) { log(formatter.formatCellValue(cell) + "\t (without evaluator)");
Cell cell = it.next(); assertEquals("SUM(12.25,12.25)/100", formatter.formatCellValue(cell));
cell.setCellValue(cell.getNumericCellValue() * Math.random() / 1000000 - 1000);
log(formatter.formatCellValue(cell));
assertTrue(formatter.formatCellValue(cell).startsWith("Balance "));
assertTrue(formatter.formatCellValue(cell).endsWith(" USD"));
}
}
/**
* A format of "@" means use the general format
*/
@Test
public void testGeneralAtFormat() {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("47154.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cellA1 = row.getCell(0);
assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cellA1.getCellType()); // now with a formula evaluator
assertEquals(2345.0, cellA1.getNumericCellValue(), 0.0001); HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
assertEquals("@", cellA1.getCellStyle().getDataFormatString()); log(formatter.formatCellValue(cell, evaluator) + "\t\t\t (with evaluator)");
char decimalSeparator = new DecimalFormatSymbols(LocaleUtil.getUserLocale()).getDecimalSeparator();
assertEquals("24" + decimalSeparator + "50%", formatter.formatCellValue(cell,evaluator));
HSSFDataFormatter f = new HSSFDataFormatter(); }
assertEquals("2345", f.formatCellValue(cellA1)); /**
} * Test using a default number format. The format should be used when a
* format pattern cannot be parsed by DecimalFormat.
/** */
* Tests various formattings of dates and numbers @Test
*/ public void testSetDefaultNumberFormat() {
@Test HSSFRow row = wb.getSheetAt(0).getRow(3);
public void testFromFile() { Iterator<Cell> it = row.cellIterator();
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("Formatting.xls"); DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
HSSFSheet sheet = workbook.getSheetAt(0); Format defaultFormat = new DecimalFormat("Balance $#,#00.00 USD;Balance -$#,#00.00 USD", dfs);
formatter.setDefaultNumberFormat(defaultFormat);
HSSFDataFormatter f = new HSSFDataFormatter();
// This one is one of the nasty auto-locale changing ones... log("\n==== DEFAULT NUMBER FORMAT ====");
assertEquals("dd/mm/yyyy", sheet.getRow(1).getCell(0).getStringCellValue()); while (it.hasNext()) {
assertEquals("m/d/yy", sheet.getRow(1).getCell(1).getCellStyle().getDataFormatString()); Cell cell = it.next();
assertEquals("11/24/06", f.formatCellValue(sheet.getRow(1).getCell(1))); cell.setCellValue(cell.getNumericCellValue() * Math.random() / 1000000 - 1000);
log(formatter.formatCellValue(cell));
assertEquals("yyyy/mm/dd", sheet.getRow(2).getCell(0).getStringCellValue()); assertTrue(formatter.formatCellValue(cell).startsWith("Balance "));
assertEquals("yyyy/mm/dd", sheet.getRow(2).getCell(1).getCellStyle().getDataFormatString()); assertTrue(formatter.formatCellValue(cell).endsWith(" USD"));
assertEquals("2006/11/24", f.formatCellValue(sheet.getRow(2).getCell(1))); }
}
assertEquals("yyyy-mm-dd", sheet.getRow(3).getCell(0).getStringCellValue());
assertEquals("yyyy\\-mm\\-dd", sheet.getRow(3).getCell(1).getCellStyle().getDataFormatString());
assertEquals("2006-11-24", f.formatCellValue(sheet.getRow(3).getCell(1)));
assertEquals("yy/mm/dd", sheet.getRow(4).getCell(0).getStringCellValue());
assertEquals("yy/mm/dd", sheet.getRow(4).getCell(1).getCellStyle().getDataFormatString());
assertEquals("06/11/24", f.formatCellValue(sheet.getRow(4).getCell(1)));
// Another builtin fun one
assertEquals("dd/mm/yy", sheet.getRow(5).getCell(0).getStringCellValue());
assertEquals("d/m/yy;@", sheet.getRow(5).getCell(1).getCellStyle().getDataFormatString());
assertEquals("24/11/06", f.formatCellValue(sheet.getRow(5).getCell(1)));
assertEquals("dd-mm-yy", sheet.getRow(6).getCell(0).getStringCellValue());
assertEquals("dd\\-mm\\-yy", sheet.getRow(6).getCell(1).getCellStyle().getDataFormatString());
assertEquals("24-11-06", f.formatCellValue(sheet.getRow(6).getCell(1)));
// Another builtin fun one
assertEquals("nn.nn", sheet.getRow(9).getCell(0).getStringCellValue());
assertEquals("General", sheet.getRow(9).getCell(1).getCellStyle().getDataFormatString());
assertEquals("10.52", f.formatCellValue(sheet.getRow(9).getCell(1)));
// text isn't quite the format rule... /**
assertEquals("nn.nnn", sheet.getRow(10).getCell(0).getStringCellValue()); * A format of "@" means use the general format
assertEquals("0.000", sheet.getRow(10).getCell(1).getCellStyle().getDataFormatString()); */
assertEquals("10.520", f.formatCellValue(sheet.getRow(10).getCell(1))); @Test
public void testGeneralAtFormat() {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("47154.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row = sheet.getRow(0);
HSSFCell cellA1 = row.getCell(0);
// text isn't quite the format rule... assertEquals(HSSFCell.CELL_TYPE_NUMERIC, cellA1.getCellType());
assertEquals("nn.n", sheet.getRow(11).getCell(0).getStringCellValue()); assertEquals(2345.0, cellA1.getNumericCellValue(), 0.0001);
assertEquals("0.0", sheet.getRow(11).getCell(1).getCellStyle().getDataFormatString()); assertEquals("@", cellA1.getCellStyle().getDataFormatString());
assertEquals("10.5", f.formatCellValue(sheet.getRow(11).getCell(1)));
// text isn't quite the format rule... HSSFDataFormatter f = new HSSFDataFormatter();
assertEquals("\u00a3nn.nn", sheet.getRow(12).getCell(0).getStringCellValue());
assertEquals("\"\u00a3\"#,##0.00", sheet.getRow(12).getCell(1).getCellStyle().getDataFormatString());
assertEquals("\u00a310.52", f.formatCellValue(sheet.getRow(12).getCell(1)));
}
private static void log(String msg) { assertEquals("2345", f.formatCellValue(cellA1));
// if (false) { // successful tests should be silent }
// System.out.println(msg);
// } /**
} * Tests various formattings of dates and numbers
*/
@Test
public void testFromFile() {
HSSFWorkbook workbook = HSSFTestDataSamples.openSampleWorkbook("Formatting.xls");
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFDataFormatter f = new HSSFDataFormatter();
// This one is one of the nasty auto-locale changing ones...
assertEquals("dd/mm/yyyy", sheet.getRow(1).getCell(0).getStringCellValue());
assertEquals("m/d/yy", sheet.getRow(1).getCell(1).getCellStyle().getDataFormatString());
assertEquals("11/24/06", f.formatCellValue(sheet.getRow(1).getCell(1)));
assertEquals("yyyy/mm/dd", sheet.getRow(2).getCell(0).getStringCellValue());
assertEquals("yyyy/mm/dd", sheet.getRow(2).getCell(1).getCellStyle().getDataFormatString());
assertEquals("2006/11/24", f.formatCellValue(sheet.getRow(2).getCell(1)));
assertEquals("yyyy-mm-dd", sheet.getRow(3).getCell(0).getStringCellValue());
assertEquals("yyyy\\-mm\\-dd", sheet.getRow(3).getCell(1).getCellStyle().getDataFormatString());
assertEquals("2006-11-24", f.formatCellValue(sheet.getRow(3).getCell(1)));
assertEquals("yy/mm/dd", sheet.getRow(4).getCell(0).getStringCellValue());
assertEquals("yy/mm/dd", sheet.getRow(4).getCell(1).getCellStyle().getDataFormatString());
assertEquals("06/11/24", f.formatCellValue(sheet.getRow(4).getCell(1)));
// Another builtin fun one
assertEquals("dd/mm/yy", sheet.getRow(5).getCell(0).getStringCellValue());
assertEquals("d/m/yy;@", sheet.getRow(5).getCell(1).getCellStyle().getDataFormatString());
assertEquals("24/11/06", f.formatCellValue(sheet.getRow(5).getCell(1)));
assertEquals("dd-mm-yy", sheet.getRow(6).getCell(0).getStringCellValue());
assertEquals("dd\\-mm\\-yy", sheet.getRow(6).getCell(1).getCellStyle().getDataFormatString());
assertEquals("24-11-06", f.formatCellValue(sheet.getRow(6).getCell(1)));
// Another builtin fun one
assertEquals("nn.nn", sheet.getRow(9).getCell(0).getStringCellValue());
assertEquals("General", sheet.getRow(9).getCell(1).getCellStyle().getDataFormatString());
assertEquals("10.52", f.formatCellValue(sheet.getRow(9).getCell(1)));
// text isn't quite the format rule...
assertEquals("nn.nnn", sheet.getRow(10).getCell(0).getStringCellValue());
assertEquals("0.000", sheet.getRow(10).getCell(1).getCellStyle().getDataFormatString());
assertEquals("10.520", f.formatCellValue(sheet.getRow(10).getCell(1)));
// text isn't quite the format rule...
assertEquals("nn.n", sheet.getRow(11).getCell(0).getStringCellValue());
assertEquals("0.0", sheet.getRow(11).getCell(1).getCellStyle().getDataFormatString());
assertEquals("10.5", f.formatCellValue(sheet.getRow(11).getCell(1)));
// text isn't quite the format rule...
assertEquals("\u00a3nn.nn", sheet.getRow(12).getCell(0).getStringCellValue());
assertEquals("\"\u00a3\"#,##0.00", sheet.getRow(12).getCell(1).getCellStyle().getDataFormatString());
assertEquals("\u00a310.52", f.formatCellValue(sheet.getRow(12).getCell(1)));
}
private static void log(String msg) {
// if (false) { // successful tests should be silent
// System.out.println(msg);
// }
}
} }