Java JXL 数字格式和单元格类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6536405/
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
JXL Number Format and Cell Type
提问by Snowy Coder Girl
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".
我正在使用 JXL 编写 Excel 文件。客户希望某一列显示带一位小数的数字。他们还希望单元格类型为“数字”。当我使用以下(测试)代码时,数字显示正确,但单元格类型为“自定义”。
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.
如果我将数字格式更改为 NumberFormats 中的变量之一(例如 NumberFormats.INTEGER),则单元格类型是正确的。但是数字当然显示为整数。我在 NumberFormats 中找不到符合我需要的变量。
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.
任何人都知道如何同时正确显示数字和单元格类型?我需要用 1 个小数显示的所有数字(即使它们是整数)。而且我不能切换到任何其他技术,我必须使用 JXL。
回答by Nor Sakinah Abdullah
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)
如果这不是您想要的,我很抱歉。但是,我对您的需求的理解是让单元格类型=数字带有 1 个小数位。您可以定义自己的数字格式。您可以使用这种格式(“#.0”)来获得您想要的(例如:900.0,23.0,34324.0 和 .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);