aspose cells Setting Display Formats of Numbers and Dates

发布时间:2019-03-13 整理:脚本之家 作者:未知
脚本之家收集整理的这篇文章主要介绍了aspose cells Setting Display Formats of Numbers and Dates脚本之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随脚本之家小编过来看看吧!
http://docs-qa.aspose.com/display/cellsnet/Setting+Display+Formats+of+Numbers+and+Dates

https://docs.aspose.com/display/cellsnet/approaches+to+format+data+in+cells

Cannot set background color to cell ( cancelStyle.Pattern = BackgroundType.Solid;)

Setting Display Formats

To set display formats in Microsoft Excel:

  1. Right-click any cell.
  2. SelectFormat Cells.
    A dialog appears that is used to set the display formats of any kind of value.

    Formatting cells using Microsoft Excel

In the left side of the dialog,there are many categories of values likeGeneral,Number,Currency,Accounting,Date,Time,Percentageetc. Aspose.Cells supports all of these display formats.

Aspose.Cells provides a class,Workbookthat represents a Microsoft Excel file. TheWorkbookclass contains aWorksheetscollection that allows access to each worksheet in the Excel file. A worksheet is represented by theWorksheetclass. TheWorksheetclass provides aCellscollection. Each item in theCellscollection represents an object of theCellclass.

Aspose.Cells providesGetStyleandSetStylemethods for theCellclass. These methods are used to get and set a cell's formatting. TheStyleclass provides some useful properties for dealing with the display formats of numbers and dates.

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 theNumberproperty of theStyleobject. All built-in number formats are given unique numeric values. Developers can assign any desired numeric value to theNumberproperty of theStyleobject to apply the display format. This approach is fast. The built-in number formats supported by Aspose.Cells are listed below.

Value

Type

Format String

0

General

General

1

Decimal

0

2

0.00

3

#,##0

4

5

Currency

$#,##0;$-#,255)">

6

7

8

9

Percentage

0%

10

0.00%

11

Scientific

0.00E+00

12

Fraction

# ?/?

13

#/

14

Date

m/d/yy

15

d-mmm-yy

16

d-mmm

17

mmm-yy

18

Time

h:mm AM/PM

19

h:mm:ss AM/PM

20

h:mm

21

h:mm:ss

22

m/d/yy h:mm

37

38

39

40

41

Accounting

_ * #,##0_ ;_ * "_ ;_ @_

42

_ $* #,##0_ ;_ $* "_ ;_ @_

43

44

$* "??_ ;_ @_

45

mm:ss

46

h :mm:ss

47

mm:ss.0

48

##0.0E+00

49

Text

@

Code Examples

The following output is achieved by executing the example code below.

Formatting data using built-in number formats

C#
//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[ ].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
].SetStyle(style);
//Adding a numeric value to "A2" cell
"A2" ].PutValue(20);
//Getting the Style of the A2 Cell
style = worksheet.Cells[ ].GetStyle();
//Setting the display format to number 9 to show value as percentage
style.Number = 9;
//Applying the style to the A2 cell
].SetStyle(style);
//Adding a numeric value to "A3" cell
"A3" ].PutValue(2546);
//Getting the Style of the A3 Cell
].GetStyle();
//Setting the display format to number 6 to show value as currency
style.Number = 6;
//Applying the style to the A3 cell
].SetStyle(style);
//Saving the Excel file
workbook.Save( "C:\\book1.xls" ,SaveFormat.Excel97To2003);
VB.NET
'Instantiating a Workbook object
Dim workbook As New Workbook()
'Adding a worksheet to the Workbook object
Dim i As Integer = workbook.Worksheets.Add()
'Obtaining the reference of the newly added worksheet by passing its sheet index
Dim worksheet As Worksheet = workbook.Worksheets(i)
'Adding the current system date to cell
worksheet.Cells( ).PutValue(DateTime.Now)
'Getting the Style of the A1 Cell
Dim style As Style = worksheet.Cells( ).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
).SetStyle(style)
'Adding a numeric value to cell
).PutValue(20)
'Getting the Style of the A2 Cell
style = worksheet.Cells( ).GetStyle()
'Setting the display format to number 9 to show value as percentage
style.Number = 9
'Applying the style to the A2 cell
).SetStyle(style)
cell
).PutValue(2546)
'Getting the Style of the A3 Cell
).GetStyle()
'Setting the display format to number 6 to show value currency
style.Number = 6
'Applying the style to the A3 cell
).SetStyle(style)
'Saving the Excel file
"C:\book1.xls" Using Custom Number Formats

To define your own customized format string for setting the display format,use theStyleobject'sCustomproperty. This approach is not as fast asusing pre-set formatsbut it is more flexible.

If you use theCustomproperty to set the number format,any previous format set using theNumberproperty is overridden and vice versa.

Code Example

The following output is achieved by executing the code below.

Formatting data using custom number format string

C#
//Adding a new worksheet to the Excel object
//Getting the style of A1 cell
//Setting the custom display format to show date as "d-mmm-yy"
style.Custom = "d-mmm-yy" ;
//Applying the style to A1 cell
//Getting the style of A2 cell
//Setting the custom display format to show value as percentage
"0.0%" ;
//Applying the style to A2 cell
//Getting the style of A3 cell
//Setting the custom display format to show value as currency
"£#,##0" ;
//Applying the style to A3 cell
VB.NET
worksheet to the Excel 'Getting the style of A1 cell
'Setting the custom display format to show date "d-mmm-yy"
'Applying the style to A1 cell
'Getting the style of A2 cell
'Setting the custom display format to show value "0.0%"
'Applying the style to A2 cell
'Getting the style of A3 cell
'Applying the style to A3 cell
脚本之家为你收集整理的aspose cells Setting Display Formats of Numbers and Dates全部内容,希望文章能够帮你解决aspose cells Setting Display Formats of Numbers and Dates所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

标签: