Aspose 数字和日期 设置

Microsoft Excel一个非常强大的功能就是使客户可以设置数字和日期的显示格式,众所周知数字可以显示为不同的值格式,包含:小数、货币、百分数、分数、账面价值等,同样地Aspose.Cells也为开发人员提供了这些功能,可以对数字和时间进行格式的设置。在Excel里客户可以右键单元格,选择单元格式化进行单元格的格式设置,而Aspose.Cells提供了GetStyle和SetStyle方法专门用于对单元格进行格式的设置。

Aspose.Cells还为开发人员提供了很多内嵌的数字和日期格式,开发人员可以通过Style对象的Number属性调用这些内嵌格式,下面是列举出的Aspose.Cells提供的内嵌显示格式:

Value  Type  Format String

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/yy

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  @

咱们可以通过下面的代码来实际看下怎么使用这些内嵌的单元格显示格式:

//Instantiating a Workbook object

Workbook workbook = new Workbook();

//Adding a new worksheet to the Workbook object

int i = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = workbook.Worksheets[i];

//Adding the current system date to "A1" cell

worksheet.Cells["A1"].PutValue(DateTime.Now);

//Getting the Style of the A1 Cell

Style style = worksheet.Cells["A1"].GetStyle();

//Setting the display format to number 15 to show date as "d-mmm-yy"

style.Number = 15;

//Applying the style to the A1 cell

worksheet.Cells["A1"].SetStyle(style);

//Adding a numeric value to "A2" cell

worksheet.Cells["A2"].PutValue(20);

//Getting the Style of the A2 Cell

style = worksheet.Cells["A2"].GetStyle();

//Setting the display format to number 9 to show value as percentage

style.Number = 9;

//Applying the style to the A2 cell

worksheet.Cells["A2"].SetStyle(style);

//Adding a numeric value to "A3" cell

worksheet.Cells["A3"].PutValue(2546);

//Getting the Style of the A3 Cell

style = worksheet.Cells["A3"].GetStyle();

//Setting the display format to number 6 to show value as currency

style.Number = 6;

//Applying the style to the A3 cell

worksheet.Cells["A3"].SetStyle(style);

//Saving the Excel file

workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);

当然开发人员还可以为单元格设置自定义显示样式,下面的代码就怎么设置单元格自定义显示样式做举例:

//Instantiating a Workbook object

Workbook workbook = new Workbook();

//Adding a new worksheet to the Excel object

int i = workbook.Worksheets.Add();

//Obtaining the reference of the newly added worksheet by passing its sheet index

Worksheet worksheet = workbook.Worksheets[i];

//Adding the current system date to "A1" cell

worksheet.Cells["A1"].PutValue(DateTime.Now);

//Getting the style of A1 cell

Style style = worksheet.Cells["A1"].GetStyle();

//Setting the custom display format to show date as "d-mmm-yy"

style.Custom = "d-mmm-yy";

//Applying the style to A1 cell

worksheet.Cells["A1"].SetStyle(style);

//Adding a numeric value to "A2" cell

worksheet.Cells["A2"].PutValue(20);

//Getting the style of A2 cell

style = worksheet.Cells["A2"].GetStyle();

//Setting the custom display format to show value as percentage

style.Custom = "0.0%";

//Applying the style to A2 cell

worksheet.Cells["A2"].SetStyle(style);

//Adding a numeric value to "A3" cell

worksheet.Cells["A3"].PutValue(2546);

//Getting the style of A3 cell

style = worksheet.Cells["A3"].GetStyle();

//Setting the custom display format to show value as currency

style.Custom = "£#,##0;[Red]$-#,##0";

//Applying the style to A3 cell

worksheet.Cells["A3"].SetStyle(style);

//Saving the Excel file

workbook.Save("C:\\book1.xls", SaveFormat.Excel97To2003);

时间: 2024-07-31 21:36:38

Aspose 数字和日期 设置的相关文章

JavaScript 中的数字和日期类型

本章节介绍如何掌握Javascript里的数字和日期类型 数字EDIT 在 JavaScript 里面,数字都是双精度浮点类型的 double-precision 64-bit binary format IEEE 754 (也就是说一个数字只能在 -(253 -1) 和 253 -1之间).没有特定的数据类型为整型.除了能够表示浮点数,号码类型有三个符号值: +Infinity.-Infinity和 NaN (not-a-number).参见Javascript指南中的 JavaScript

postgresql数据库中判断是否是数字和日期时间格式函数

/* 在编写GreenPlum函数的过程中,遇到要判断字符串是否是数字和日期格式的情况,基于GreenPlum和postgresql的亲缘关系,找到了下面两个函数. */ --1.判断字符串是否是数字 CREATE OR REPLACE FUNCTION isnumeric(txtStr VARCHAR) RETURNS BOOLEAN AS $$ BEGIN RETURN txtStr ~ '^([0-9]+[.]?[0-9]*|[.][0-9]+)$'; END; $$ LANGUAGE '

140821●字符串、数字、日期及应用举例

brerk   彻底终断循环,跳出for语句 continue  中断当前循环,进行下一循环 字符串 .Length 字符串长度 .TrimStart() 截去开头空格 .TrimEnd()   截去结尾空格 .Trim()   截去开头跟结尾的空格 .ToUpper()   全部大写 .ToLower()   全部小写 .Substring(m,n)   从左边第m(m从0开始)个开始截取字符串,截取n个 .Replace(“m”,”n”) 用n替换m .IndexOf() 指定的字符串第一次

DB2中字符、数字和日期类型之间的转换

DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别是如何实现的呢?在Oracle这几个类型之间的转换是十分方便的,通过 to_char|to_date|to_number函数即可完成类型转换.本小节主要介绍DB2中的一些知识,从Oracle转过来的DBA或开发人 员,可以对比着学习. 数字型到字符型的转换 DB2中的col_a字段 字段类型 到字符类型的转换

2014.8.20break,continue,字符串,数字和日期

(一)break与continue break——彻底终断循环 continue——中断本次循环,继续下次循环 break举例: 1 //求100以内所有质数 2 for (int i = 1; i <= 100; i++) 3 { 4 int n = 0;//n用来存放能被整除的数的个数 5 for (int j = 1; j <= i; j++) 6 { 7 if (i % j == 0) 8 { 9 n++; 10 } 11 if (n > 2) 12 { 13 break;//跳

4.06.数字或日期转换为字符串.TO_CHAR()

TO_CHAR(x[[,c2],C3]) [功能]将日期或数据转换为char数据类型 [参数] x是一个date或number数据类型. c2为格式参数 c3为NLS设置参数 如果x为日期nlsparm=NLS_DATE_LANGUAGE 控制返回的月份和日份所使用的语言. 如果x为数字nlsparm=NLS_NUMERIC_CHARACTERS 用来指定小数位和千分位的分隔符,以及货币符号. NLS_NUMERIC_CHARACTERS ="dg", NLS_CURRENCY=&qu

Java入门教程五(数字和日期处理)

Java 提供了处理相关问题的类,包括 Math 类.Random 类.BigInteger 类.Date 类等. Math类 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数.对数.平方根和三角函数等.Math 类位于 java.lang 包 静态常量 Math 类中包含 E 和 PI 两个静态常量,其中 E 用于记录 e 的常量,而 PI 用于记录圆周率的值. System.out.println(Math.E); //2.718281828459045 System.out.

【Python进阶】数字、日期和时间

一.对数值进行取整 将浮点数取整到固定的小数位:roung(value, ndigits),ndigits可以是负数,取整到十位.百位.千位等. >>> round(1.456, 2)     # 1.46 >>> round(16743, -3)      # 17000 >>> format(1.2345, '0.3f')  # 1.235 二.精确的小数计算 浮点数无法精确表达出所有的十进制小数位. 0.4的二进制表示是无限循环,所以如果要精确表

Oracle函数-单行函数-数字、日期、日期处理函数

函数的分类 单行函数:一个input对应一个output,input和output存在一一对应的关系 如lower 组函数:多个input,但是只对应一个output.如 sum() ============================================================== 单行函数 特点: 每行返回一个结果,输入输出存在一一对应的关系 能嵌套使用 ,一个函数的输出能做另外一个函数的输入 如:select lowner(upper('aa')) from dua