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. Camera 幻灯片播放

    Camera 幻灯片播放与轮播有些类似,不同的是在camera 幻灯片播放时有很多很炫很酷的播放效果 : 简单的介绍Camera 插件的使用方式: 1.引入js: <script src=&qu ...

  2. ASP.NET 状态的传递和保存

    1,HTTP协议是无状态的.服务器不会记住上次给浏览器的处理结果,如果需要上次处理结果(上次状态)就需要浏览器把处理结果值(上次状态)再次给服务器. 2,URL传值:通过URL参数或者通过Form表单 ...

  3. Linux下设置定期执行脚本

    下面针对的是非ubuntu环境,会在文章末尾介绍ubuntu的一些区别. 在Linux下,经常需要定期的执行一些脚本从而来实现一些功能. 在Linux下我们用crontab来实现定期的执行脚本这个功能 ...

  4. jqure获取单选按钮的值(比如性别)

    使用jquery获取radio的值,最重要的是掌握jquery选择器的使用,在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项: 1. ...

  5. 理解C#系列 / 核心C# / 常量

    常量 常量? 我对常量的理解就是在初始化完成后再也不变的“全局变量”. 定义常量 [const][空格][变量类型][空格][变量名称][=][值][:] const表示定义的是常量. 常量特点 常量 ...

  6. SSH连接 NAT型 VirtualBox + LINUX

    1.首先登录到虚拟机中的Linux系统,查看一下使用NAT网卡的IP地址. 2.关闭虚拟机. 3.依次点击 "设置 -> 网络 -> (网络地址转换)端口转发",其中需 ...

  7. debian终端菱形乱码修复

    最简安装debian的时候由于没有中文字库,若选择看中文环境会出现菱形乱码.先把zh.utf8换为us.utf8看着好顺眼些.按空格键取消已选的zh.utf8选项按空格键选择us.utf8选项ok

  8. .Net三维控件

    AnyCAD .Net三维建模和可视化控件为.Net 4.0开发者提供简单易用的三维建模.三维可视化和文件交换的API. 30天试用版下载: 1.  三维建模 三维建模有以下功能: 三维基本体,如点. ...

  9. Python的 is 运算符

    1. is运算符判断的是同一性而不是相等性. #x和y都绑定到同一个列表,而z被绑定在另外一个具有相同数值和顺序的列表上 x = y = [1, 2, 3] z = [1, 2, 3] x == y ...

  10. java.lang.ThreadLocal源码分析

    ThreadLocal类提供线程本地变量,为变量在每个线程创建一个副本,每个线程可以访问自己内部的副本变量. 比如,有这样一个需求,需要为每个线程创建一个独一无二的标识,这个标识在第一次调用Threa ...