(转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates
Setting Display Formats
Using Microsoft Excel:
- Right-click on any desired cell and select Format Cells option.
A dialog appears that allows you to set the display formats of any kind of value.Formatting Cells using Microsoft Excel

In the left side of the above figure, you can see that there are many categories of the values like General, Number, Currency, Accounting, Date, Time, Percentage etc. Aspose.Cells supports all of these categories to set the display format of numbers or dates.
Aspose.Cells provides a class, Workbook that represents an Excel file. Workbook class contains a WorksheetCollection that allows to access each worksheet in the Excel file. A worksheet is represented by the Worksheet class. Worksheet class provides a Cells collection. Each item in the Cells collection represents an object of Cell class.
Aspose.Cells provides the setStyle method in the Cell class that is used to set the formatting style of a cell. Also, Style object of Style class can be used that further provides some useful properties to configure font settings.
Using Built-in Number Formats
Aspose.Cells offers some built-in number formats to configure the display formats of the numbers and dates. These built-in number formats can be applied by using the setNumber method of the Style object. All built-in number formats are given unique numeric values. Developers can provide any desired numeric value to the setNumber method of Style object and hence the display format is applied. This approach is more faster. The built-in number formats supported by Aspose.Cells are given below:
| 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 | @ |
The following output is the result of executing the code below.
Formatting data using built-in number formats
//Instantiating a Workbook object
Workbook workbook = new Workbook(); //Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells(); //Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
cell.setValue(Calendar.getInstance()); //Setting the display format of the date to number 15 to show date as "d-mmm-yy"
Style style = cell.getStyle();
style.setNumber(15);
cell.setStyle(style); //Adding a numeric value to "A2" cell
cell = cells.get("A2");
cell.setValue(20); //Setting the display format of the value to number 9 to show value as percentage
style = cell.getStyle();
style.setNumber(9);
cell.setStyle(style); //Adding a numeric value to "A3" cell
cell = cells.get("A3");
cell.setValue(1546); //Setting the display format of the value to number 6 to show value as currency
style = cell.getStyle();
style.setNumber(6);
cell.setStyle(style); //Saving the modified Excel file in default format
workbook.save("C:\\output.xls");
Using Custom Number Formats
To define your own customized format string to set data display format, use the setCustom method of the Style object. This approach is not as much faster as the first approach discussed above but it is more flexible.
The following output is the result of executing the code below.
Formatting data using custom number format string
//Instantiating a Workbook object
Workbook workbook = new Workbook(); //Accessing the added worksheet in the Excel file
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
Cells cells = worksheet.getCells(); //Adding the current system date to "A1" cell
Cell cell = cells.get("A1");
cell.setValue(Calendar.getInstance()); //Setting the display format of the date to number 15 to show date as "d-mmm-yy"
Style style = cell.getStyle();
style.setCustom("d-mmm-yy");
cell.setStyle(style); //Adding a numeric value to "A2" cell
cell = cells.get("A2");
cell.setValue(20); //Setting the display format of the value to number 9 to show value as percentage
style = cell.getStyle();
style.setCustom("0.0%");
cell.setStyle(style); //Adding a numeric value to "A3" cell
cell = cells.get("A3");
cell.setValue(1546); //Setting the display format of the value to number 6 to show value as currency
style = cell.getStyle();
style.setCustom("$#,##0;[Red]$-#,##0");
cell.setStyle(style); //Saving the modified Excel file in default format
workbook.save("C:\\output.xls"); from: http://www.aspose.com/docs/display/cellsjava/Setting+Display+Formats+of+Numbers+and+Dates
(转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates的更多相关文章
- UITableView设置cell为不可选?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术.本文将为读者讲解UITableView如何设置单 ...
- POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】
实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读 ...
- 网易彩票-我的彩票-设置-cell跳转界面
1. 点击“cell”推出对应的界面 1.1 新建group,名为:Setting 路径:MYLottery(我的彩票)->Controller 1.2 新建Cocoa Touch Class, ...
- IOS中设置cell的背景view和选中时的背景view 、设置cell最右边的指示器(比如箭头\文本标签)
一.Cell的设置 1.设置cell的背景view和选中时的背景view UIImageView *bg = [[UIImageView alloc] init]; bg.image = [UIIma ...
- 设置 cell点击 背景色
//设置 cell点击 背景色 cell.selectionStyle = UITableViewCellSelectionStyleDefault; cell.selectedBackgroundV ...
- collectionView代理方法快速设置cell大小上下左右间隔
#define JianGe 25 #define GeShu 4 #define ScreenWidth ([UIScreen mainScreen].bounds.size.width) #def ...
- 设置cell高度的两种方法(label高度的可变引起cell高度可变的情况)
第一种:(iOS8以后可用) 在Xib或stroyboard中(代码也可以) 利用AutoLayout设置好label的约束(比如可以设置四个边都距离屏幕50等方式,必须四个边都要固定好). 在代码部 ...
- ios 设置cell的间距
1.设置假的间距,我们在tableviewcell的contentView上添加一个view,比如让其距离上下左右的距离都是10:这个方法是最容易想到的: 2.用UIContentView来代替tab ...
- 设置cell背景色和选中色
// 设置cell的背景色 UIView *bg = [[[UIView alloc] init] autorelease]; bg.backgroundColor = [UIColor colorW ...
随机推荐
- BLE-NRF51822教程16-BLE地址
本教程基于 sdk9+sd8.0 51822的 BLE的设备地址 可以通过如下函数函数来获得 地址的设置可以调用如下函数设置. 官方的demo工程中,都是没有主动调用过 sd_ble_gap_addr ...
- Architecture of a Highly Scalable NIO-Based Server
一. thread-per-connection The thread-per-connection approach uses an exclusive worker thread for each ...
- Linux 下动态库 / 静态库(依赖)
一. 依赖动态库的动态库 libfun.so依赖动态库libtest.so(libfun.so动态库里的函数intnothing()调用了libtest.so里的intmytest()函数),而mai ...
- CentOS 6.3下PostgreSQL 的安装与配置
一.简介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库都不具备.这个起源于伯克 ...
- Java学习-020-Properties 判断是否存在对应的 key 项
在日常的脚本编写过程中,通常会判断配置文件中是否存在对应的配置项,以判断是否执行相应的业务逻辑. 小二上码...若有不足之处,敬请大神指正,不胜感激! 判断是否存在 key 项(配置项)的方法源码如下 ...
- C#中jQuery Ajax实例(一)
目标:在aspx页面输入两参数,传到后台.cs代码,在无刷新显示到前台 下面是我的Ajax异步传值的第一个实例 1.前台html代码: <html xmlns="http://www. ...
- ASP.NET中application对象的用法
一.Application对象的理解 Application对象在实际网络开发中的用途就是记录整个网络的信息,如上线人数.在线名单.意见调查和网上选举等.在给定的应用程序的多有用户之间共享信息,并在服 ...
- Interview Return Products of All Other Elements in Array
这是一道面试亚马逊时的题目,要求Time O(n). 我刚开始想的是算出所有的数的总product,再去除以对应位置的元素,但这种做法的问题是若该位置为0,就会报错. 到网上搜了下,才知道,原来有这种 ...
- saltstack之(九)配置管理源码部署Nginx
场景:rpm包安装的nginx服务,无法满足定制模块的需求,故线上环境使用nginx源码进行安装.本片文章详细介绍如何使用saltstack的配置管理功能实现nginx软件的源码安装. 下载源码:pc ...
- JBOSS /invoker/JMXInvokerServlet 利用工具 .
链接: http://pan.baidu.com/s/1F8bMI 密码: 1h2r 工具使用说明 1. 查看系统名称 java -jar jboss_exploit_fat.jar -i http: ...