JXL 数字格式和单元格类型

发布于 2024-11-18 01:11:16 字数 817 浏览 9 评论 0原文

我正在使用 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();

如果我将数字格式更改为 NumberFormats 中的变量之一(例如 NumberFormats.INTEGER),则单元格类型是正确的。但数字当然显示为整数。我在 NumberFormats 中找不到符合我需求的变量。

有人知道如何正确显示数字和单元格类型吗?我需要所有以 1 位小数显示的数字(即使它们是整数)。而且我无法切换到任何其他技术,我必须使用 JXL。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-11-25 01:11:16

如果这不是您想要的,我很抱歉。但是,我对您的需求的理解是单元格类型=数字,小数点后一位。您可以定义自己的数字格式。您可以使用此格式(“#.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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文