poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Laubach Shawn Contr OC-ALC/PSB <Shawn.Laub...@tinker.af.mil>
Subject RE: Formatting using HSSFDataFormat.
Date Fri, 12 Dec 2003 18:00:46 GMT
I fixed the two places where you did not follow the example and it worked
just fine.  You have to call getFormat on an actual instance of
HSSFDataFormat and not the static call.  Plus all this did not occur until
version about 1.8 or so.  If you are not at the current release, I'd upgrade
to 2.0RC1 as it fixes many bugs and has many more features.

----------------------------------------------------------------------------

import java.io.*;
import org.apache.poi.hssf.usermodel.*;

public class TestHSSF {
	public TestHSSF(){
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("format sheet");
		HSSFCellStyle style;
		HSSFDataFormat format = wb.createDataFormat();
		HSSFRow row;
		HSSFCell cell;
		short rowNum = 0;
		short colNum = 0;

		row = sheet.createRow(rowNum++);
		cell = row.createCell(colNum);
		cell.setCellType( HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(11111.25);
		style = wb.createCellStyle();
/* This one is existing HSSFDataFormat. The field in Excel is stored as
Number. 
 *  Excel->right click cell->Format Cells, property is "Number" under the
"Number" tab.
 */
		style.setDataFormat(format.getFormat("#,##0.00"));
		cell.setCellStyle(style);

		row = sheet.createRow(rowNum++);
		cell = row.createCell(colNum);
		cell.setCellType( HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(11111.255555);
		style = wb.createCellStyle();

/* This one is the format that I need. This field in Excel is stored as
General. 
 *  Excel->right click cell->Format Cells, property is "General" under the
"Number" tab.
 */		
            style.setDataFormat(format.getFormat("#,##0.000000"));
		
		cell.setCellStyle(style);

        try {
            FileOutputStream fileOut = new
FileOutputStream("c:/temp/workbook.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                    
	}
	public static void main(String args[]){
		TestHSSF t = new TestHSSF();
	}
}

-----Original Message-----
From: Joshi, Rajesh [mailto:RJoshi@westernasset.com] 
Sent: Friday, December 12, 2003 11:37 AM
To: 'POI Users List'
Subject: RE: Formatting using HSSFDataFormat.

Thank you for the response. I used the example from the website and ran it.
But, still does not work.
Any help is really appreciated. Thanks a lot.

Excerpt of the code. 

public class TestHSSF {
	public TestHSSF(){
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("format sheet");
		HSSFCellStyle style;

		HSSFRow row;
		HSSFCell cell;
		short rowNum = 0;
		short colNum = 0;

		row = sheet.createRow(rowNum++);
		cell = row.createCell(colNum);
		cell.setCellType( HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(11111.25);
		style = wb.createCellStyle();
/* This one is existing HSSFDataFormat. The field in Excel is stored as
Number. 
 *  Excel->right click cell->Format Cells, property is "Number" under the
"Number" tab.
 */
		style.setDataFormat(HSSFDataFormat.getFormat("#,##0.00"));
		cell.setCellStyle(style);

		row = sheet.createRow(rowNum++);
		cell = row.createCell(colNum);
		cell.setCellType( HSSFCell.CELL_TYPE_NUMERIC);
		cell.setCellValue(11111.255555);
		style = wb.createCellStyle();

/* This one is the format that I need. This field in Excel is stored as
General. 
 *  Excel->right click cell->Format Cells, property is "General" under the
"Number" tab.
 */		
            style.setDataFormat(HSSFDataFormat.getFormat("#,##0.000000"));
		
		cell.setCellStyle(style);

        try {
            FileOutputStream fileOut = new
FileOutputStream("c:/temp/workbook.xls");
            wb.write(fileOut);
            fileOut.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
                    
	}
	public static void main(String args[]){
		TestHSSF t = new TestHSSF();
	}
}



-----Original Message-----
From: Laubach Shawn Contr OC-ALC/PSB
[mailto:Shawn.Laubach@tinker.af.mil]
Sent: Friday, December 12, 2003 8:59 AM
To: POI Users List
Subject: RE: Formatting using HSSFDataFormat.


Check out http://jakarta.apache.org/poi/hssf/quick-guide.html#DataFormats

It has many great examples of code for features.

Shawn

-----Original Message-----
From: Joshi, Rajesh [mailto:RJoshi@westernasset.com] 
Sent: Friday, December 12, 2003 10:52 AM
To: 'poi-user@jakarta.apache.org'
Subject: Formatting using HSSFDataFormat.

I am new-bie using POI - HSSFDataFormat class.

HSSF supports standard formatting (like for "price") #,###.00. Using this
formatting you get a numeric field with the appropriate formatting. But when
I try to change the formatting to 
#,###.0000000 the extra fields in the fraction don't show up. The junit test
however asserts that the field is numeric.

How do you define a user defined formatting standard like #,###.0000000? Is
there anyway of specifying the property of the field to be retained as
numeric?

Rajesh Joshi,
* (626) 844-4816   
<mailto:rjoshi@westernasset.com>
Western Asset Management Company
117, East Colorado Blvd., Pasadena, CA 91105



**********************************************************************
E-mail sent through the Internet is not secure. Western Asset therefore
recommends that you do not send any confidential or sensitive information to
us via electronic mail, including social security numbers, account numbers,
or personal identification numbers. Delivery, and or timely delivery of
Internet mail is not guaranteed. Western Asset therefore recommends that you
do not send time sensitive or action-oriented message to us via electronic
mail. 
**********************************************************************


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org


**********************************************************************
E-mail sent through the Internet is not secure. Western Asset therefore
recommends that you do not send any confidential or sensitive information to
us via electronic mail, including social security numbers, account numbers,
or personal identification numbers. Delivery, and or timely delivery of
Internet mail is not guaranteed. Western Asset therefore recommends that you
do not send time sensitive or action-oriented message to us via electronic
mail. 
**********************************************************************


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org

---------------------------------------------------------------------
To unsubscribe, e-mail: poi-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-user-help@jakarta.apache.org


Mime
View raw message