数字设置
如何设置数字和日期的显示格式
微软Excel的一个非常强大的功能是允许用户设置数字值和日期的显示格式。我们知道数字数据可以代表不同的值,包括十进制、货币、百分比、小数或会计值等。这些数值的显示格式根据所表示的信息类型而不同。同样,日期或时间也可以以多种格式显示。
Aspose.Cells支持此功能,并允许开发人员为数字或日期设置任何显示格式。
如何在Microsoft Excel中设置显示格式
在Microsoft Excel中设置显示格式:
- 右键单击任何单元格。
- 选择单元格格式。将弹出一个对话框,用于设置任何值的显示格式。
对话框左侧有许多类别,如常规、数字、货币、会计、日期、时间、百分比等。Aspose.Cells支持所有这些显示格式。
Aspose.Cells提供了一个模块,Workbook,代表一个Excel文件。Workbook模块包含一个Worksheets集合,可以访问Excel文件中的每个工作表。工作表由Worksheet模块表示。Worksheet模块提供一个Cells集合,每个Cells集合中的项目代表Cell模块的对象。
Aspose.Cells为Cell模块提供getStyle()和setStyle(Style)方法,用于获取和设置单元格的格式。Style模块提供一些处理数字和日期显示格式的有用属性。
如何使用内置数字格式
Aspose.Cells提供一些内置的数字格式,用于配置数字和日期的显示格式。这些内置数字格式可以通过Style对象的setNumber(number)方法应用。所有内置数字格式都赋予了唯一的数字值。开发者可以将任意希望的数字值分配给Style对象的setNumber(number)方法,以应用显示格式。该方法速度快。支持的内置数字格式如下:
数值 | 类型 | 格式字符串 |
---|---|---|
0 | General | General |
1 | Decimal | 0 |
2 | Decimal | 0.00 |
3 | Decimal | #,##0 |
4 | Decimal | #,##0.00 |
5 | Currency | $#,##0;$-#,##0 |
6 | Currency | $#,##0;[Red]$-#,##0 |
7 | Currency | $#,##0.00;$-#,##0.00 |
8 | Currency | $#,##0.00;[Red]$-#,##0.00 |
9 | Percentage | 0% |
10 | Percentage | 0.00% |
11 | Scientific | 0.00E+00 |
12 | Fraction | # ?/? |
13 | Fraction | # / |
14 | Date | m/d/yyyy |
15 | Date | d-mmm-yy |
16 | Date | d-mmm |
17 | Date | mmm-yy |
18 | Time | h:mm AM/PM |
19 | Time | h:mm:ss AM/PM |
20 | Time | h:mm |
21 | Time | h:mm:ss |
22 | Time | m/d/yy h:mm |
37 | Currency | #,##0;-#,##0 |
38 | Currency | #,##0;[Red]-#,##0 |
39 | Currency | #,##0.00;-#,##0.00 |
40 | Currency | #,##0.00;[Red]-#,##0.00 |
41 | Accounting | _ * #,##0_ ;_ * “_ ;_ @_ |
42 | Accounting | _ $* #,##0_ ;_ $* “_ ;_ @_ |
43 | Accounting | _ * #,##0.00_ ;_ * “??_ ;_ @_ |
44 | Accounting | _ $* #,##0.00_ ;_ $* “??_ ;_ @_ |
45 | Time | mm:ss |
46 | Time | h:mm:ss |
47 | Time | mm:ss.0 |
48 | Scientific | ##0.0E+00 |
49 | Text | @ |
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Adding the current system date to "A1" cell | |
worksheet.getCells().get("A1").putValue(new Date()); | |
// Getting the Style of the A1 Cell | |
let style = worksheet.getCells().get("A1").getStyle(); | |
// Setting the display format to number 15 to show date as "d-mmm-yy" | |
style.setNumber(15); | |
// Applying the style to the A1 cell | |
worksheet.getCells().get("A1").setStyle(style); | |
// Adding a numeric value to "A2" cell | |
worksheet.getCells().get("A2").putValue(20); | |
// Getting the Style of the A2 Cell | |
style = worksheet.getCells().get("A2").getStyle(); | |
// Setting the display format to number 9 to show value as percentage | |
style.setNumber(9); | |
// Applying the style to the A2 cell | |
worksheet.getCells().get("A2").setStyle(style); | |
// Adding a numeric value to "A3" cell | |
worksheet.getCells().get("A3").putValue(2546); | |
// Getting the Style of the A3 Cell | |
style = worksheet.getCells().get("A3").getStyle(); | |
// Setting the display format to number 6 to show value as currency | |
style.setNumber(6); | |
// Applying the style to the A3 cell | |
worksheet.getCells().get("A3").setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |
如何使用自定义数字格式
为定义自定义的显示格式字符串,请使用Style对象的setCustom(string)方法。这种方法比预设格式更灵活,但速度较慢。
const path = require("path"); | |
const AsposeCells = require("aspose.cells.node"); | |
// The path to the documents directory. | |
const dataDir = path.join(__dirname, "data"); | |
// Create directory if it is not already present. | |
const fs = require("fs"); | |
if (!fs.existsSync(dataDir)) { | |
fs.mkdirSync(dataDir); | |
} | |
// Instantiating a Workbook object | |
const workbook = new AsposeCells.Workbook(); | |
// Obtaining the reference of the first worksheet | |
const worksheet = workbook.getWorksheets().get(0); | |
// Adding the current system date to "A1" cell | |
worksheet.getCells().get("A1").putValue(new Date()); | |
// Getting the Style of the A1 Cell | |
let style = worksheet.getCells().get("A1").getStyle(); | |
// Setting the display format to number 15 to show date as "d-mmm-yy" | |
style.setNumber(15); | |
// Applying the style to the A1 cell | |
worksheet.getCells().get("A1").setStyle(style); | |
// Adding a numeric value to "A2" cell | |
worksheet.getCells().get("A2").putValue(20); | |
// Getting the Style of the A2 Cell | |
style = worksheet.getCells().get("A2").getStyle(); | |
// Setting the display format to number 9 to show value as percentage | |
style.setNumber(9); | |
// Applying the style to the A2 cell | |
worksheet.getCells().get("A2").setStyle(style); | |
// Adding a numeric value to "A3" cell | |
worksheet.getCells().get("A3").putValue(2546); | |
// Getting the Style of the A3 Cell | |
style = worksheet.getCells().get("A3").getStyle(); | |
// Setting the display format to number 6 to show value as currency | |
style.setNumber(6); | |
// Applying the style to the A3 cell | |
worksheet.getCells().get("A3").setStyle(style); | |
// Saving the Excel file | |
workbook.save(path.join(dataDir, "book1.out.xls"), AsposeCells.SaveFormat.Excel97To2003); |