JXL Number Format and Cell Type
Asked Answered
M

1

9

I am using JXL to write an Excel file. The customer wants a certain column to display numbers with one decimal place. They also want the cell types to be "Number". When I use the following (test) code, the numbers are displayed correctly, but the cell type is "Custom".

File excelFile = new File("C:\\Users\\Rachel\\Desktop\\TestFile.xls");

WritableWorkbook excelBook = Workbook.createWorkbook(excelFile);
WritableSheet excelSheet = excelBook.createSheet("Test Sheet", 0);

WritableFont numberFont = new WritableFont(WritableFont.ARIAL);
WritableCellFormat numberFormat = new WritableCellFormat(numberFont, new NumberFormat("#0.0"));

Number numberCell = new Number(0, 0, 25, numberFormat);
excelSheet.addCell(numberCell);

excelBook.write();
excelBook.close();

If I change the number format to be one of the variables in NumberFormats (e.g. NumberFormats.INTEGER), the cell type is correct. But the numbers are displayed as integers of course. I cannot find a variable in NumberFormats that matches my needs.

Anyone know of a way to get both the display of the numbers and the cell types correct? I need all of the numbers displayed with 1 decimal (even if they are integers). And I cannot switch to any other technology, I must use JXL.

Mckamey answered 30/6, 2011 at 14:31 Comment(2)
I know the question was asked in 2011. Did you manage to figure out? I am having the same problem though I have other option which is to use Apache POI.Tousle
I forget what I ended up doing and am now with a different company. SorryMckamey
W
10

I'm sorry if this is not what you want. But, what I understand about your needs is to have cell type=number with 1 decimal place. It is possible for you to define your own number formats. You can use this format ("#.0") to get what you want (eg: 900.0,23.0,34324.0 and .etc)

NumberFormat decimalNo = new NumberFormat("#.0"); 
WritableCellFormat numberFormat = new WritableCellFormat(decimalNo);
//write to datasheet
Number numberCell = new Number(0, 0, 25, numberFormat); 
excelSheet.addCell(numberCell);
Wentzel answered 26/2, 2014 at 3:38 Comment(1)
I get an error: 'NumberFormat' is abstract. Cannot be instantiatedWhaleback

© 2022 - 2024 — McMap. All rights reserved.