博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates
阅读量:7073 次
发布时间:2019-06-28

本文共 5193 字,大约阅读时间需要 17 分钟。

Setting Display Formats

Using Microsoft Excel:

  1. 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  :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

Java
//Instantiating a Workbook objectWorkbook workbook = new Workbook();//Accessing the added worksheet in the Excel fileint 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

Java
//Instantiating a Workbook objectWorkbook workbook = new Workbook();//Accessing the added worksheet in the Excel fileint 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

转载地址:http://ihzml.baihongyu.com/

你可能感兴趣的文章
LPC3250 Perpheral IO Mapping
查看>>
免费在线工具制作自己的卡通头像
查看>>
state-game.cs
查看>>
(iPhone/iPad开发)在UIWebView中自定义菜单栏
查看>>
Android 双卡双待识别
查看>>
该不该用inline-block取代float? inline和float的区别?
查看>>
WEB和APP谁是互联网未来
查看>>
Java中的内部接口
查看>>
IPv4 地址分类
查看>>
如何查看表和索引的统计信息
查看>>
使用 Eclipse 调试 Java 程序的技巧
查看>>
能源项目xml文件 -- app-datasource.xml
查看>>
使用Nginx负载均衡搭建高性能.NETweb应用程序(转)
查看>>
Bootstrap框架下实现图片切换
查看>>
poj1860--Currency Exchange
查看>>
浅谈js中的继承
查看>>
移动端web app开发备忘
查看>>
Lintcode---线段树查询(区间最大值)
查看>>
C++加载位图跟SOCKET通信的编写
查看>>
ZOJ 1649 Rescue(有敌人迷宫BFS)
查看>>