How to write formatted numbers as numbers in JExcel (jxl)
Asked Answered
B

3

8

I am using Java Spring and jxl to create Excel workbook on server side. The data that needs to be shown in Excel consists of already formatted numbers. I am using

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 so Excel can't use formulas on them, it gives a warning as 'Number stored as Text' and I have to do 'Convert to Number'.

In jxl, can we pass strings and tell to interpret them as numbers? All the numbers I have are valid numbers formatted differently using $, %, thousands separators. I don't want to convert them to valid numbers and give them formatting again while exporting to excel.

Please help. Thank you.

Basketball answered 22/12, 2010 at 20:12 Comment(0)
C
5

I had a similar problem

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

int eday =  Integer.parseInt(Trainingresult.getString("Day"));
//Label Eday = new Label(1, i, eday);
firstsheet.addCell(new Number(1,i,eday));
Crissycrist answered 8/7, 2014 at 17:54 Comment(0)
T
0
/*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();
    }
Triadelphous answered 22/12, 2010 at 21:57 Comment(0)
C
0

first, add import

import jxl.write.Number;

because you could get other Number class and get error, after that do anything

double dval = 1;
Number number = new Number(k, n, dval);

So you could also useful this codes

        if(readCell.getType() == CellType.BOOLEAN) {
            boolean bv = false;
            if(readCell.getContents().equals("true")) {
                bv = true;
            }
            Boolean b = new Boolean(i,j,bv);
            writesheet.addCell(b);
        } else
        if(readCell.getType() == CellType.NUMBER) {
            //if (!readCell.getContents().equals("")) {
            double dval = 0;
            try {
                dval = Double.parseDouble(readCell.getContents());
            } catch (NumberFormatException ex) {
            }

            Number n = new Number(i, j, dval);
            writesheet.addCell(n);
Candlepin answered 22/12, 2021 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.