java 如何在 JExcel (jxl) 中将格式化的数字写为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4513297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 06:42:16  来源:igfitidea点击:

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

javaexcelspringjxljexcelapi

提问by engg

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

我正在使用 Java Spring 和 jxl 在服务器端创建 Excel 工作簿。需要在 Excel 中显示的数据由已格式化的数字组成。我在用

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'.

在下载的 excel 文件中,所有这些数字都存储为文本,因此 Excel 无法对它们使用公式,它会发出“数字存储为文本”的警告,我必须执行“转换为数字”。

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.

在 jxl 中,我们可以传递字符串并告诉将它们解释为数字吗?我拥有的所有数字都是使用 $、%、千位分隔符格式化的有效数字。我不想将它们转换为有效数字并在导出到 excel 时再次给它们格式化。

Please help. Thank you.

请帮忙。谢谢你。

回答by Krishnakanth Jc

I had a similar problem

我有一个类似的问题

I changed the variable to intand used new Numberfunction it helped.

我将变量更改为int并使用它帮助的新 Number函数。

int eday =  Integer.parseInt(Trainingresult.getString("Day"));
//Label Eday = new Label(1, i, eday);
firstsheet.addCell(new Number(1,i,eday));

回答by jrey

/*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();
    }