Turns out it was the single inverted comma that you used to surround the kg
symbol with; it should have been speach marks or quotation marks. Try this;
public static final void kgFormat(String filename) {
File file = null;
FileOutputStream fos = null;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
HSSFRow row = null;
HSSFCell cell = null;
HSSFCellStyle numberStyle = null;
try {
file = new File(filename);
workbook = new HSSFWorkbook();
numberStyle = workbook.createCellStyle();
numberStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00\"
kg\""));
sheet = workbook.createSheet();
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(10.023);
cell.setCellStyle(numberStyle);
fos = new FileOutputStream(file);
workbook.write(fos);
}
catch(IOException ioEx) {
System.out.println("Caught an: " + ioEx.getClass().getName());
System.out.println("Message: " + ioEx.getMessage());
System.out.println("Stacktrace follows:............");
ioEx.printStackTrace(System.out);
}
finally {
try {
if(fos != null) {
fos.close();
}
}
catch(IOException ioEx) {
}
}
}
It works for me using POI version 3.6 and Office 2007. If you do not want
the space between the final digit and the kg symbol, simply change this
line;
numberStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00\"
kg\""));
to this;
numberStyle.setDataFormat(workbook.createDataFormat().getFormat("0.00\"kg\""));
Yours
Mark B
PS This goes without saying of course, I only made this into a static method
so that I could plug the test code into a junk class I use to prototype/test
this type of thing. You will want to do something different for your code I
am sure.
Princess-4 wrote:
>
> I have already tried all these combinations.I get error ehen i open the
> file
> Hepzibah
> **Hope can be ignited by a spark of encouragement *****
>
>
>
> On Fri, Jan 22, 2010 at 3:19 PM, MSB <[email protected]> wrote:
>
>>
>> Have you tried
>>
>> this.styleKg.setDataFormat(wb.createDataFormat().getFormat("#,##000
>> \'kg\'"));
>>
>> or
>>
>> this.styleKg.setDataFormat(wb.createDataFormat().getFormat("#,##000
>> kg"));
>>
>> or even
>>
>> this.styleKg.setDataFormat(wb.createDataFormat().getFormat("#,##000kg"));
>>
>> just to see if the problem has to do with the single inverted commas
>> surrounding the units symbol? Have no idea if this will help at all,
>> sorry.
>>
>> Yours
>>
>> Mark B
>>
>>
>> Princess-4 wrote:
>> >
>> > this.styleKg.setDataFormat(wb.createDataFormat().getFormat("#,##000
>> > 'kg'"));
>> >
>> >
>> > is not setting the format in the cell with POI 2.5 .basically i want
>> 1000
>> > Kg
>> > to be written in the cell
>> > along with the unit.
>> >
>> > The same user defined format works in Excel..
>> >
>> > But to display %
>> > this.stylePer.setDataFormat(wb.createDataFormat().getFormat("0.00%"));
>> is
>> > working
>> >
>> > thanks
>> >
>> >
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Units-to-be-set-through-getFormat%28%29-tp27271302p27273826.html
>> Sent from the POI - User mailing list archive at Nabble.com.
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>>
>
>
--
View this message in context:
http://old.nabble.com/Units-to-be-set-through-getFormat%28%29-tp27271302p27284589.html
Sent from the POI - User mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]