您的位置:首页 > 编程语言 > ASP

(转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates

2015-08-20 11:27 751 查看

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:

ValueTypeFormat String
0GeneralGeneral
1Decimal0
2Decimal0.00
3Decimal#,##0
4Decimal#,##0.00
5Currency$#,##0;$-#,##0
6Currency$#,##0;[Red]$-#,##0
7Currency$#,##0.00;$-#,##0.00
8Currency$#,##0.00;[Red]$-#,##0.00
9Percentage0%
10Percentage0.00%
11Scientific0.00E+00
12Fraction# ?/?
13Fraction# /
14Datem/d/yy
15Dated-mmm-yy
16Dated-mmm
17Datemmm-yy
18Timeh:mm AM/PM
19Timeh:mm:ss AM/PM
20Timeh:mm
21Timeh:mm:ss
22Timem/d/yy h:mm
37Currency#,##0;-#,##0
38Currency#,##0;[Red]-#,##0
39Currency#,##0.00;-#,##0.00
40Currency#,##0.00;[Red]-#,##0.00
41Accounting_ * #,##0_ ;_ * "_ ;_ @_
42Accounting_ $* #,##0_ ;_ $* "_ ;_ @_
43Accounting_ * #,##0.00_ ;_ * "??_ ;_ @_
44Accounting_ $* #,##0.00_ ;_ $* "??_ ;_ @_
45Timemm:ss
46Timeh :mm:ss
47Timemm:ss.0
48Scientific##0.0E+00
49Text@
The following output is the result of executing the code below.

Formatting data using built-in number formats



Java

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



Java

//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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: