</faq>
<faq>
<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>
<answer>
<p>
- HSSF does not yet support custom data formats, however, it should be a
- reasonably easy facillity to add and we'll gladly accept contributions
- in this area.
-
- These are the built in formats that it does support:
-
-
- <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>
+ 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>.
+ </p>
+ </answer>
+ </faq>
+ <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 ?
+ </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>
</answer>
</faq>
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
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.
It differs greatly from the read/write API
and is intended for intermediate developers who need a smaller
HSSFCellStyle cs = wb.createCellStyle();
HSSFCellStyle cs2 = wb.createCellStyle();
HSSFCellStyle cs3 = wb.createCellStyle();
+HSSFDataFormat df = wb.createDataFormat();
// create 2 fonts objects
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
//set cell stlye
cs.setFont(f);
-//set the cell format see HSSFDataFromat for a full list
-cs.setDataFormat(HSSFDataFormat.getFormat("($#,##0_);[Red]($#,##0)"));
+//set the cell format
+cs.setDataFormat(df.getFormat("#,##0.0"));
//set a thin border
cs2.setBorderBottom(cs2.BORDER_THIN);
//fill w fg fill color
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
cs2.setFont(f2);
* 0x15, "h:mm:ss"<br>
* 0x16, "m/d/yy h:mm"<br>
*<P>
- * // 0x17 - 0x24 reserved for international and undocumented<br>
- * 0x25, "(#,##0_);(#,##0)"<br>
- * 0x26, "(#,##0_);[Red](#,##0)"<br>
- * 0x27, "(#,##0.00_);(#,##0.00)"<br>
- * 0x28, "(#,##0.00_);[Red](#,##0.00)"<br>
- * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<br>
- * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<br>
- * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<br>
- * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<br>
- * 0x2d, "mm:ss"<br>
- * 0x2e, "[h]:mm:ss"<br>
- * 0x2f, "mm:ss.0"<br>
- * 0x30, "##0.0E+0"<br>
- * 0x31, "@"<br>
- *
+ * // 0x17 - 0x24 reserved for international and undocumented
+ * 0x25, "(#,##0_);(#,##0)"<P>
+ * 0x26, "(#,##0_);[Red](#,##0)"<P>
+ * 0x27, "(#,##0.00_);(#,##0.00)"<P>
+ * 0x28, "(#,##0.00_);[Red](#,##0.00)"<P>
+ * 0x29, "_(*#,##0_);_(*(#,##0);_(* \"-\"_);_(@_)"<P>
+ * 0x2a, "_($*#,##0_);_($*(#,##0);_($* \"-\"_);_(@_)"<P>
+ * 0x2b, "_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)"<P>
+ * 0x2c, "_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)"<P>
+ * 0x2d, "mm:ss"<P>
+ * 0x2e, "[h]:mm:ss"<P>
+ * 0x2f, "mm:ss.0"<P>
+ * 0x30, "##0.0E+0"<P>
+ * 0x31, "@" - This is text format.<P>
+ * 0x31 "text" - Alias for "@"<P>
*
* @author Andrew C. Oliver (acoliver at apache dot org)
+ * @author Shawn M. Laubach (slaubach at apache dot org)
*/
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
* @return index of format or -1 if undefined.
*/
public static short getBuiltinFormat( String format )
{
+ if (format.toUpperCase().equals("TEXT"))
+ format = "@";
+
if ( builtinFormats == null )
{
populateBuiltinFormats();
}
short retval = -1;
- for ( short k = 0; k < 0x31; k++ )
+ for (short k = 0; k <= 0x31; k++)
{
String nformat = (String) builtinFormats.get( k );
/**
* 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
* @return index of format.
*/
{
ListIterator i;
int ind;
+
+ if (format.toUpperCase().equals("TEXT"))
+ format = "@";
+
if ( !movedBuiltins )
{
i = builtinFormats.listIterator();