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);

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

  1. JavaScript 中的数字和日期类型

    本章节介绍如何掌握Javascript里的数字和日期类型 数字EDIT 在 JavaScript 里面,数字都是双精度浮点类型的 double-precision 64-bit binary form ...

  2. MySQL的数据类型:文本、数字、日期/时间

    在MySQL中,有三种主要的类型:文本.数字和日期/时间类型. 文本类型(text):数据类型                                 描述 CHAR(size) 保存固定长度 ...

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

    DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...

  4. sql server使用公用表表达式CTE通过递归方式编写通用函数自动生成连续数字和日期

    问题:在数据库脚本开发中,有时需要生成一堆连续数字或者日期,例如yearly report就需要连续数字做年份,例如daily report就需要生成一定时间范围内的每一天日期.而自带的系统表mast ...

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

    Java 提供了处理相关问题的类,包括 Math 类.Random 类.BigInteger 类.Date 类等. Math类 Math 类封装了常用的数学运算,提供了基本的数学操作,如指数.对数.平 ...

  6. Python基础4:数据类型:数字 字符串 日期

    [ Python 数据类型 ] 我们知道,几乎任何编程语言都具有数据类型:常见的数据类型有:字符串.整型.浮点型以及布尔类型等. Python也不例外,也有自己的数据类型,主要有以下几种: 1.数字: ...

  7. HTML5 input新增的几种类型(数字、日期、颜色选取、范围)

    你可能已经听说过,HTML5里引入了几种新的input类型.在HTML5之前,大家熟知的input类型包括:text(输入框),hidden(隐藏域),submit(提交按钮)等.而HTML5到来之后 ...

  8. C#验证类 可验证:邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP (转)

    namespace YongFa365.Validator { using System; using System.Text.RegularExpressions; /**//// <summ ...

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

    (一)break与continue break——彻底终断循环 continue——中断本次循环,继续下次循环 break举例: //求100以内所有质数 ; i <= ; i++) { ;// ...

随机推荐

  1. Ajax学习笔记(一)

    来源:http://www.imooc.com/learn/250 Ajax不是某种编程语言,而是一种在无须重新加载整个网页的情况下能够更新部分网页的技术. 一.概念介绍--异步 利用XMLHttpR ...

  2. Java中的堆和栈的区别

    当一个人开始学习Java或者其他编程语言的时候,会接触到堆和栈,由于一开始没有明确清晰的说明解释,很多人会产生很多疑问,什么是堆,什么是栈,堆和栈有什么区别?更糟糕的是,Java中存在栈这样一个后进先 ...

  3. PHP与javascript实现变量交互

    <?php /** * 本例是PHP和javascript交互的例子,php中的值赋给js变量中,前提是这个php变量必须有值才行,就算是假分支中. * 比如php中的$flags在本例中为tr ...

  4. [转] 利用任务计划重启sqlserver服务

    1.建立一个批处理文件restartsqlserver.bat     内容如下:     net   stop   mssqlserver   /y    net   start  mssqlser ...

  5. Sql server For XML Path 学习

    最近看到太多人问这种问题   自己也不太了解  就在网上学习学习 自己测试一番 CREATE TABLE test0621 (id INT,NAME NVARCHAR(max)) INSERT tes ...

  6. 如何安装 JAVA 7 (JDK 7u75) 在 CentOS/RHEL 7/6/5 Fedora

    先下载JDK For 64 Bit:- # cd /opt/ # wget --no-cookies --no-check-certificate --header "Cookie: gpw ...

  7. GIT Learning

    一.Why Git 1.1 Git是分布式的,本地的版本管理 chect out代码后会在自己的机器上克隆一个自己的版本库,即使你在没有网络的环境,你仍然能够提交文件,查看历史版本记录,创建项目分支, ...

  8. js密码的校验(判断字符类型、统计字符类型个数)

    /** *判断字符类型 */ function CharMode(iN) { if (iN >= 48 && iN <= 57) //数字 return 1; if (iN ...

  9. LLVM language 参考手册 翻译停止相关

    再翻译LLVM language 参考手册的时候,个人感觉很多东西都不是很懂,因此打算学习完编译原理后再去继续研究翻译,多有不便望见谅

  10. 利用Nutch和Tomcat构建搜索引擎

    利用Nutch和Tomcat构建搜索引擎 1.安装环境及软件版本介绍 本教程是在Linux Ubuntu 12.04 desktop i386操作系统上搭建,结合使用了Nutch-1.2和Apache ...