2008-07-20 13:18:07 -04:00
|
|
|
/* ====================================================================
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
|
|
|
|
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
2010-04-20 08:57:27 -04:00
|
|
|
import java.math.BigDecimal;
|
2008-07-20 13:18:07 -04:00
|
|
|
import java.text.DecimalFormat;
|
2010-04-20 08:57:27 -04:00
|
|
|
import java.text.DecimalFormatSymbols;
|
2008-07-20 13:18:07 -04:00
|
|
|
import java.text.Format;
|
2010-04-20 08:57:27 -04:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.GregorianCalendar;
|
2008-07-20 13:18:07 -04:00
|
|
|
import java.util.Iterator;
|
2010-04-20 08:57:27 -04:00
|
|
|
import java.util.Locale;
|
2008-07-20 13:18:07 -04:00
|
|
|
|
2009-05-16 13:39:31 -04:00
|
|
|
import org.apache.poi.hssf.HSSFTestDataSamples;
|
2009-05-16 20:10:36 -04:00
|
|
|
import org.apache.poi.ss.usermodel.Cell;
|
2010-06-02 13:17:42 -04:00
|
|
|
import org.apache.poi.ss.usermodel.DateUtil;
|
2009-05-16 13:39:31 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
import junit.framework.TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unit tests for HSSFDataFormatter.java
|
2008-07-28 16:08:15 -04:00
|
|
|
*
|
2008-07-20 13:18:07 -04:00
|
|
|
* @author James May (james dot may at fmr dot com)
|
|
|
|
*
|
|
|
|
*/
|
2008-07-28 16:08:15 -04:00
|
|
|
public final class TestHSSFDataFormatter extends TestCase {
|
|
|
|
|
|
|
|
private final HSSFDataFormatter formatter;
|
|
|
|
private final HSSFWorkbook wb;
|
2008-07-20 13:18:07 -04:00
|
|
|
|
|
|
|
public TestHSSFDataFormatter() {
|
|
|
|
// create the formatter to test
|
|
|
|
formatter = new HSSFDataFormatter();
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// create a workbook to test with
|
2008-07-28 16:08:15 -04:00
|
|
|
wb = new HSSFWorkbook();
|
|
|
|
HSSFSheet sheet = wb.createSheet();
|
|
|
|
HSSFDataFormat format = wb.createDataFormat();
|
|
|
|
|
|
|
|
// create a row and put some cells in it
|
|
|
|
HSSFRow row = sheet.createRow(0);
|
|
|
|
|
|
|
|
// date value for July 8 1901 1:19 PM
|
|
|
|
double dateNum = 555.555;
|
2010-06-02 13:17:42 -04:00
|
|
|
// date value for July 8 1901 11:23 AM
|
|
|
|
double timeNum = 555.47431;
|
2008-07-28 16:08:15 -04:00
|
|
|
|
|
|
|
//valid date formats -- all should have "Jul" in output
|
2010-06-02 13:17:42 -04:00
|
|
|
String[] goodDatePatterns = {
|
2008-07-28 16:08:15 -04:00
|
|
|
"[$-F800]dddd\\,\\ mmmm\\ dd\\,\\ yyyy",
|
|
|
|
"mmm/d/yy\\ h:mm PM;@",
|
|
|
|
"mmmm/d/yy\\ h:mm;@",
|
|
|
|
"mmmm/d;@",
|
|
|
|
"mmmm/d/yy;@",
|
|
|
|
"mmm/dd/yy;@",
|
|
|
|
"[$-409]d\\-mmm;@",
|
|
|
|
"[$-409]d\\-mmm\\-yy;@",
|
|
|
|
"[$-409]dd\\-mmm\\-yy;@",
|
|
|
|
"[$-409]mmm\\-yy;@",
|
|
|
|
"[$-409]mmmm\\-yy;@",
|
|
|
|
"[$-409]mmmm\\ d\\,\\ yyyy;@",
|
|
|
|
"[$-409]mmm/d/yy\\ h:mm:ss;@",
|
|
|
|
"[$-409]mmmm/d/yy\\ h:mm:ss am;@",
|
|
|
|
"[$-409]mmmmm;@",
|
|
|
|
"[$-409]mmmmm\\-yy;@",
|
|
|
|
"mmmm/d/yyyy;@",
|
|
|
|
"[$-409]d\\-mmm\\-yyyy;@"
|
|
|
|
};
|
2010-06-02 13:17:42 -04:00
|
|
|
|
|
|
|
//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",
|
|
|
|
};
|
2008-07-28 16:08:15 -04:00
|
|
|
|
|
|
|
// valid number formats
|
|
|
|
String[] goodNumPatterns = {
|
|
|
|
"#,##0.0000",
|
|
|
|
"#,##0;[Red]#,##0",
|
|
|
|
"(#,##0.00_);(#,##0.00)",
|
|
|
|
"($#,##0.00_);[Red]($#,##0.00)",
|
|
|
|
"$#,##0.00",
|
2009-05-16 20:10:36 -04:00
|
|
|
"[$-809]#,##0.00", // international format
|
|
|
|
"[$-2]#,##0.00", // international format
|
2008-07-28 16:08:15 -04:00
|
|
|
"0000.00000%",
|
|
|
|
"0.000E+00",
|
|
|
|
"0.00E+00",
|
2010-06-01 08:12:22 -04:00
|
|
|
"[BLACK]0.00;[COLOR 5]##.##",
|
2008-07-28 16:08:15 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// invalid date formats -- will throw exception in DecimalFormat ctor
|
|
|
|
String[] badNumPatterns = {
|
|
|
|
"#,#$'#0.0000",
|
|
|
|
"'#','#ABC#0;##,##0",
|
|
|
|
"000 '123 4'5'6 000",
|
|
|
|
"#''0#0'1#10L16EE"
|
|
|
|
};
|
|
|
|
|
|
|
|
// create cells with good date patterns
|
|
|
|
for (int i = 0; i < goodDatePatterns.length; i++) {
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(i);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(dateNum);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat(goodDatePatterns[i]));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
row = sheet.createRow(1);
|
2010-06-02 13:17:42 -04:00
|
|
|
|
|
|
|
// 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);
|
2008-07-28 16:08:15 -04:00
|
|
|
|
|
|
|
// create cells with num patterns
|
|
|
|
for (int i = 0; i < goodNumPatterns.length; i++) {
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(i);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(-1234567890.12345);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat(goodNumPatterns[i]));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
2010-06-02 13:17:42 -04:00
|
|
|
row = sheet.createRow(3);
|
2008-07-28 16:08:15 -04:00
|
|
|
|
|
|
|
// create cells with bad num patterns
|
|
|
|
for (int i = 0; i < badNumPatterns.length; i++) {
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(i);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(1234567890.12345);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat(badNumPatterns[i]));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Built in formats
|
|
|
|
|
|
|
|
{ // Zip + 4 format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = sheet.createRow(4);
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(123456789);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat("00000-0000"));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Phone number format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = sheet.createRow(5);
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(5551234567D);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat("[<=9999999]###-####;(###) ###-####"));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // SSN format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = sheet.createRow(6);
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
cell.setCellValue(444551234);
|
|
|
|
HSSFCellStyle cellStyle = wb.createCellStyle();
|
|
|
|
cellStyle.setDataFormat(format.getFormat("000-00-0000"));
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // formula cell
|
2010-06-02 13:17:42 -04:00
|
|
|
row = sheet.createRow(7);
|
2008-08-09 15:47:39 -04:00
|
|
|
HSSFCell cell = row.createCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
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);
|
|
|
|
}
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting formatted values from numeric and date cells.
|
|
|
|
*/
|
|
|
|
public void testGetFormattedCellValueHSSFCell() {
|
|
|
|
// Valid date formats -- cell values should be date formatted & not "555.555"
|
|
|
|
HSSFRow row = wb.getSheetAt(0).getRow(0);
|
2009-05-16 20:10:36 -04:00
|
|
|
Iterator<Cell> it = row.cellIterator();
|
2008-07-28 16:08:15 -04:00
|
|
|
log("==== VALID DATE FORMATS ====");
|
2008-07-20 13:18:07 -04:00
|
|
|
while (it.hasNext()) {
|
2009-05-16 20:10:36 -04:00
|
|
|
Cell cell = it.next();
|
2010-06-01 11:20:57 -04:00
|
|
|
String fmtval = formatter.formatCellValue(cell);
|
|
|
|
log(fmtval);
|
2008-07-20 13:18:07 -04:00
|
|
|
|
2010-06-02 13:17:42 -04:00
|
|
|
// should not be equal to "555.555"
|
|
|
|
assertTrue( DateUtil.isCellDateFormatted(cell) );
|
|
|
|
assertTrue( ! "555.555".equals(fmtval));
|
2010-04-20 08:57:27 -04:00
|
|
|
|
2010-06-01 11:20:57 -04:00
|
|
|
String fmt = cell.getCellStyle().getDataFormatString();
|
|
|
|
|
|
|
|
//assert the correct month form, as in the original Excel format
|
|
|
|
String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM";
|
|
|
|
// this line is intended to compute how "July" would look like in the current locale
|
|
|
|
String jul = new SimpleDateFormat(monthPtrn).format(new GregorianCalendar(2010,6,15).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);
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
|
|
|
|
2010-06-02 13:17:42 -04:00
|
|
|
row = wb.getSheetAt(0).getRow(1);
|
|
|
|
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"
|
|
|
|
assertTrue( DateUtil.isCellDateFormatted(cell) );
|
|
|
|
assertTrue( ! "555.47431".equals(fmtval));
|
|
|
|
|
|
|
|
// check we found the time properly
|
|
|
|
assertTrue("Format came out incorrect - " + fmt, fmtval.indexOf("11:23") > -1);
|
|
|
|
}
|
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// test number formats
|
|
|
|
row = wb.getSheetAt(0).getRow(1);
|
|
|
|
it = row.cellIterator();
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== VALID NUMBER FORMATS ====");
|
2008-07-20 13:18:07 -04:00
|
|
|
while (it.hasNext()) {
|
|
|
|
HSSFCell cell = (HSSFCell) it.next();
|
2008-07-28 16:08:15 -04:00
|
|
|
log(formatter.formatCellValue(cell));
|
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// should not be equal to "1234567890.12345"
|
|
|
|
assertTrue( ! "1234567890.12345".equals(formatter.formatCellValue(cell)));
|
|
|
|
}
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// test bad number formats
|
2010-06-02 13:17:42 -04:00
|
|
|
row = wb.getSheetAt(0).getRow(3);
|
2008-07-20 13:18:07 -04:00
|
|
|
it = row.cellIterator();
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== INVALID NUMBER FORMATS ====");
|
2008-07-20 13:18:07 -04:00
|
|
|
while (it.hasNext()) {
|
|
|
|
HSSFCell cell = (HSSFCell) it.next();
|
2008-07-28 16:08:15 -04:00
|
|
|
log(formatter.formatCellValue(cell));
|
2010-04-20 08:57:27 -04:00
|
|
|
// should be equal to "1234567890.12345"
|
|
|
|
// in some locales the the decimal delimiter is a comma, not a dot
|
|
|
|
char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
|
|
|
|
assertEquals("1234567890" + decimalSeparator + "12345", formatter.formatCellValue(cell));
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// test Zip+4 format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = wb.getSheetAt(0).getRow(4);
|
2008-07-20 13:18:07 -04:00
|
|
|
HSSFCell cell = row.getCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== ZIP FORMAT ====");
|
|
|
|
log(formatter.formatCellValue(cell));
|
2008-07-20 13:18:07 -04:00
|
|
|
assertEquals("12345-6789", formatter.formatCellValue(cell));
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// test phone number format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = wb.getSheetAt(0).getRow(5);
|
2008-07-20 13:18:07 -04:00
|
|
|
cell = row.getCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== PHONE FORMAT ====");
|
|
|
|
log(formatter.formatCellValue(cell));
|
2008-07-20 13:18:07 -04:00
|
|
|
assertEquals("(555) 123-4567", formatter.formatCellValue(cell));
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// test SSN format
|
2010-06-02 13:17:42 -04:00
|
|
|
row = wb.getSheetAt(0).getRow(6);
|
2008-07-20 13:18:07 -04:00
|
|
|
cell = row.getCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== SSN FORMAT ====");
|
|
|
|
log(formatter.formatCellValue(cell));
|
|
|
|
assertEquals("444-55-1234", formatter.formatCellValue(cell));
|
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// null test-- null cell should result in empty String
|
|
|
|
assertEquals(formatter.formatCellValue(null), "");
|
2008-07-28 16:08:15 -04:00
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// null test-- null cell should result in empty String
|
2008-07-28 16:08:15 -04:00
|
|
|
assertEquals(formatter.formatCellValue(null), "");
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testGetFormattedCellValueHSSFCellHSSFFormulaEvaluator() {
|
|
|
|
// test formula format
|
2010-06-02 13:17:42 -04:00
|
|
|
HSSFRow row = wb.getSheetAt(0).getRow(7);
|
2008-07-20 13:18:07 -04:00
|
|
|
HSSFCell cell = row.getCell(0);
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== FORMULA CELL ====");
|
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
// first without a formula evaluator
|
2008-07-28 16:08:15 -04:00
|
|
|
log(formatter.formatCellValue(cell) + "\t (without evaluator)");
|
|
|
|
assertEquals("SUM(12.25,12.25)/100", formatter.formatCellValue(cell));
|
|
|
|
|
|
|
|
// now with a formula evaluator
|
2008-09-12 04:18:54 -04:00
|
|
|
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
|
2008-07-28 16:08:15 -04:00
|
|
|
log(formatter.formatCellValue(cell, evaluator) + "\t\t\t (with evaluator)");
|
2010-04-20 08:57:27 -04:00
|
|
|
char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
|
|
|
|
assertEquals("24" + decimalSeparator + "50%", formatter.formatCellValue(cell,evaluator));
|
|
|
|
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test using a default number format. The format should be used when a
|
|
|
|
* format pattern cannot be parsed by DecimalFormat.
|
|
|
|
*/
|
|
|
|
public void testSetDefaultNumberFormat() {
|
2010-06-02 13:17:42 -04:00
|
|
|
HSSFRow row = wb.getSheetAt(0).getRow(3);
|
2009-05-16 20:10:36 -04:00
|
|
|
Iterator<Cell> it = row.cellIterator();
|
2008-07-20 13:18:07 -04:00
|
|
|
Format defaultFormat = new DecimalFormat("Balance $#,#00.00 USD;Balance -$#,#00.00 USD");
|
|
|
|
formatter.setDefaultNumberFormat(defaultFormat);
|
2008-09-12 04:18:54 -04:00
|
|
|
|
2008-07-28 16:08:15 -04:00
|
|
|
log("\n==== DEFAULT NUMBER FORMAT ====");
|
2008-07-20 13:18:07 -04:00
|
|
|
while (it.hasNext()) {
|
2009-05-16 20:10:36 -04:00
|
|
|
Cell cell = it.next();
|
2008-07-20 13:18:07 -04:00
|
|
|
cell.setCellValue(cell.getNumericCellValue() * Math.random() / 1000000 - 1000);
|
2008-07-28 16:08:15 -04:00
|
|
|
log(formatter.formatCellValue(cell));
|
2008-07-20 13:18:07 -04:00
|
|
|
assertTrue(formatter.formatCellValue(cell).startsWith("Balance "));
|
|
|
|
assertTrue(formatter.formatCellValue(cell).endsWith(" USD"));
|
2008-07-28 16:08:15 -04:00
|
|
|
}
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|
2009-05-16 13:39:31 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A format of "@" means use the general format
|
|
|
|
*/
|
|
|
|
public void testGeneralAtFormat() {
|
2009-05-16 20:10:36 -04:00
|
|
|
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());
|
|
|
|
assertEquals(2345.0, cellA1.getNumericCellValue(), 0.0001);
|
|
|
|
assertEquals("@", cellA1.getCellStyle().getDataFormatString());
|
|
|
|
|
|
|
|
HSSFDataFormatter f = new HSSFDataFormatter();
|
|
|
|
|
|
|
|
assertEquals("2345", f.formatCellValue(cellA1));
|
2009-05-16 13:39:31 -04:00
|
|
|
}
|
2010-06-02 12:51:05 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests various formattings of dates and numbers
|
|
|
|
*/
|
|
|
|
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...
|
2010-06-13 02:26:14 -04:00
|
|
|
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)));
|
2010-06-02 12:51:05 -04:00
|
|
|
}
|
2008-07-20 13:18:07 -04:00
|
|
|
|
2008-07-28 16:08:15 -04:00
|
|
|
private static void log(String msg) {
|
|
|
|
if (false) { // successful tests should be silent
|
|
|
|
System.out.println(msg);
|
|
|
|
}
|
|
|
|
}
|
2008-07-20 13:18:07 -04:00
|
|
|
}
|