How to write formatted numbers as numbers in JExcel (jxl)

I am using Java Spring and jxl to create a server side Excel workbook. The data to be displayed in Excel consists of already formatted numbers. I use

WritableCellFormat wcf = new WritableCellFormat(); wcf.setAlignment(Alignment.RIGHT); .... .... sheet.addCell(new Label(j, i + 1, xxx, wcf)); //where xxx is a string which is a number already formatted 

In the downloaded excel file, all these numbers are stored as text and therefore Excel cannot use formulas on them, it gives a warning like "Number saved as text" and I need to do "Convert to number".

In jxl, can we pass strings and tell them to interpret them as numbers? All the numbers that I have are real numbers, formatted differently using $,%, thousand separators. I do not want to convert them to real numbers and format them again when exporting to excel.

Please, help. Thanks.

+6
java spring excel jxl jexcelapi
source share
2 answers

I had a similar problem

I changed the variable to int and used the new Number function, which it helped.

 int eday = Integer.parseInt(Trainingresult.getString("Day")); //Label Eday = new Label(1, i, eday); firstsheet.addCell(new Number(1,i,eday)); 
+2
source share
 /*use new Number to write number cell*/ WritableWorkbook w; try { w = Workbook.createWorkbook(new File("c:/test.xls")); WritableSheet s = w.createSheet("Demo Book", 0); WritableCellFormat wcf = new WritableCellFormat(); for (int i=0;i<10;i++){ int row = i+1; /*add cell number*/ s.addCell(new Number(1, i, (i*15))); /*add cell number*/ s.addCell(new Number(2, i, (i*3+Math.random()*10))); /*add formula*/ s.addCell(new Formula(3,i,"B"+row+" * C"+row)); } w.write(); w.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (RowsExceededException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (WriteException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
0
source share

All Articles