Corrected bug in HSSFDataFormat and added alias for text format. Updated documentation dealing with data format.

PR:
Obtained from:
Submitted by:
Reviewed by:


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@352887 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shawn Laubach 2002-10-09 12:41:58 +00:00
parent dc9bbc45cb
commit 70eb3725d0
3 changed files with 46 additions and 30 deletions

View File

@ -134,18 +134,21 @@
</faq> </faq>
<faq> <faq>
<question> <question>
I want to set a cell format (Data format of a cell) of a excel sheet as###,###,###.#### or ###,###,###.0000. Is it possible using POI ? I want to set a cell format (Data format of a cell) of a excel sheet as ###,###,###.#### or ###,###,###.0000. Is it possible using POI ?
</question> </question>
<answer> <answer>
<p> <p>
HSSF does not yet support custom data formats, however, it should be a Yes. You first need to get a HSSFDataFormat object from the workbook and call getFormat with the desired format. Some examples are <link href="hssf/quick-guide.html#DataFormats">here</link>.
reasonably easy facillity to add and we'll gladly accept contributions </p>
in this area. </answer>
</faq>
These are the built in formats that it does support: <faq>
<question>
I want to set a cell format (Data format of a cell) of a excel sheet as text. Is it possible using POI ?
<link href="http://jakarta.apache.org/poi/javadocs/javasrc/org/apache/poi/hssf/usermodel/HSSFDataFormat_java.html#HSSFDataFormat">http://jakarta.apache.org/poi/javadocs/javasrc/org/apache/poi/hssf/usermodel/HSSFDataFormat_java.html#HSSFDataFormat</link> </question>
<answer>
<p>
Yes. This is a built-in format for excel that you can get from HSSFDataFormat object using the format string "@". Also, the string "text" will alias this format.
</p> </p>
</answer> </answer>
</faq> </faq>

View File

@ -21,7 +21,8 @@
This release allows numeric and string cell values to be written to This release allows numeric and string cell values to be written to
or read from an XLS file as well as reading and writing dates. Also or read from an XLS file as well as reading and writing dates. Also
in this release is row and column sizing, cell styling (bold, in this release is row and column sizing, cell styling (bold,
italics, borders,etc), and support for built-in data formats. New italics, borders,etc), and support for both built-in and user
defined data formats. New
to this release is an event-based API for reading XLS files. to this release is an event-based API for reading XLS files.
It differs greatly from the read/write API It differs greatly from the read/write API
and is intended for intermediate developers who need a smaller and is intended for intermediate developers who need a smaller
@ -96,6 +97,7 @@ HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle(); HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle(); HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle(); HSSFCellStyle cs3 = wb.createCellStyle();
HSSFDataFormat df = wb.createDataFormat();
// create 2 fonts objects // create 2 fonts objects
HSSFFont f = wb.createFont(); HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont(); HSSFFont f2 = wb.createFont();
@ -119,13 +121,15 @@ f2.setStrikeout( true );
//set cell stlye //set cell stlye
cs.setFont(f); cs.setFont(f);
//set the cell format see HSSFDataFromat for a full list //set the cell format
cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)")); cs.setDataFormat(df.getFormat("#,##0.0"));
//set a thin border //set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN); cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color //fill w fg fill color
cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND); cs2.setFillPattern((short) HSSFCellStyle.SOLID_FOREGROUND);
//set the cell format to text see HSSFDataFormat for a full list
cs2.setDataFormat(HSSFDataFormat.getBuiltinFormat("text"));
// set the font // set the font
cs2.setFont(f2); cs2.setFont(f2);

View File

@ -95,23 +95,24 @@ import java.util.Vector;
* 0x15, "h:mm:ss"<br> * 0x15, "h:mm:ss"<br>
* 0x16, "m/d/yy h:mm"<br> * 0x16, "m/d/yy h:mm"<br>
*<P> *<P>
* // 0x17 - 0x24 reserved for international and undocumented<br> * // 0x17 - 0x24 reserved for international and undocumented
* 0x25, "(#,##0_);(#,##0)"<br> * 0x25, "(#,##0_);(#,##0)"<P>
* 0x26, "(#,##0_);[Red](#,##0)"<br> * 0x26, "(#,##0_);[Red](#,##0)"<P>
* 0x27, "(#,##0.00_);(#,##0.00)"<br> * 0x27, "(#,##0.00_);(#,##0.00)"<P>
* 0x28, "(#,##0.00_);[Red](#,##0.00)"<br> * 0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
* 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<br> * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
* 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<br> * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
* 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<br> * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
* 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<br> * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
* 0x2d, "mm:ss"<br> * 0x2d, "mm:ss"<P>
* 0x2e, "[h]:mm:ss"<br> * 0x2e, "[h]:mm:ss"<P>
* 0x2f, "mm:ss.0"<br> * 0x2f, "mm:ss.0"<P>
* 0x30, "##0.0E+0"<br> * 0x30, "##0.0E+0"<P>
* 0x31, "@"<br> * 0x31, "@" - This is text format.<P>
* * 0x31 "text" - Alias for "@"<P>
* *
* @author Andrew C. Oliver (acoliver at apache dot org) * @author Andrew C. Oliver (acoliver at apache dot org)
* @author Shawn M. Laubach (slaubach at apache dot org)
*/ */
public class HSSFDataFormat public class HSSFDataFormat
@ -217,20 +218,24 @@ public class HSSFDataFormat
} }
/** /**
* get the format index that matches the given format string * get the format index that matches the given format string<p>
* Automatically converts "text" to excel's format string to represent text.
* @param format string matching a built in format * @param format string matching a built in format
* @return index of format or -1 if undefined. * @return index of format or -1 if undefined.
*/ */
public static short getBuiltinFormat( String format ) public static short getBuiltinFormat( String format )
{ {
if (format.toUpperCase().equals("TEXT"))
format = "@";
if ( builtinFormats == null ) if ( builtinFormats == null )
{ {
populateBuiltinFormats(); populateBuiltinFormats();
} }
short retval = -1; short retval = -1;
for ( short k = 0; k < 0x31; k++ ) for (short k = 0; k <= 0x31; k++)
{ {
String nformat = (String) builtinFormats.get( k ); String nformat = (String) builtinFormats.get( k );
@ -245,7 +250,7 @@ public class HSSFDataFormat
/** /**
* get the format index that matches the given format string. * get the format index that matches the given format string.
* Creates a new format if one is not found. * Creates a new format if one is not found. Aliases text to the proper format.
* @param format string matching a built in format * @param format string matching a built in format
* @return index of format. * @return index of format.
*/ */
@ -254,6 +259,10 @@ public class HSSFDataFormat
{ {
ListIterator i; ListIterator i;
int ind; int ind;
if (format.toUpperCase().equals("TEXT"))
format = "@";
if ( !movedBuiltins ) if ( !movedBuiltins )
{ {
i = builtinFormats.listIterator(); i = builtinFormats.listIterator();