4
C# Excel Number Style In Excel, right-click a cell or group of cells and choosing Format Cell, a window with number formatting options will pop up for you to choose. Number formatting is the ability to control how a number is displayed by taking advantage of the culture- specific thousand and decimal separators, the amount of decimals as well as a currency symbol. Number format can also be used to format dates, percentages, and even custom formats. Through C#, programmers also can set number styles in Excel, and even without Microsoft Excel installed on system. How to Use C# Set Excel Number Style Spire.XLS for .NET is .NET Excel component which enables your .NET applications fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. With Spire.XLS, programmers can use C# set Excel number style easily. Download Spire.XLS for .NET (or Spire.Office ) with .NET Framework 2.0 (or above) together and use the code below to set Excel number Style: C# Excel Number Style: 01 private void ExcelDocViewer( string fileName ) 02 { 03 try 04 { 05 System.Diagnostics.Process.Start(fileName); 06 } 07 catch{} 08 } 09 10 private void btnRun_Click(object sender, System.EventArgs e) 11 { 12 Workbook workbook = new Workbook(); 13 Worksheet sheet = workbook.Worksheets[0]; 14 15 sheet.Range["B1"].Text = "NUMBER FORMATTING"; 16 sheet.Range["B1"].Style.Font.IsBold = true; 17 18 sheet.Range["B3"].Text = "0"; 19 sheet.Range["C3"].NumberValue = 1234.5678;

C# excel set excel number style

Embed Size (px)

DESCRIPTION

Use C# set Excel number Style

Citation preview

Page 1: C# excel   set excel number style

C# Excel Number Style

In Excel, right-click a cell or group of cells and choosing Format Cell, a window with number

formatting options will pop up for you to choose. Number formatting is the ability to control how

a number is displayed by taking advantage of the culture-specific thousand and decimal

separators, the amount of decimals as well as a currency symbol. Number format can also be used

to format dates, percentages, and even custom formats. Through C#, programmers also can set

number styles in Excel, and even without Microsoft Excel installed on system.

How to Use C# Set Excel Number Style

Spire.XLS for .NET is .NET Excel component which enables your .NET applications fast

generate, read, write and modify Excel document without Microsoft Office Excel Automation.

With Spire.XLS, programmers can use C# set Excel number style easily.

Download Spire.XLS for .NET (or Spire.Office) with .NET Framework 2.0 (or above) together

and use the code below to set Excel number Style:

C# Excel Number Style:

01 private void ExcelDocViewer( string fileName )

02 {

03 try

04 {

05 System.Diagnostics.Process.Start(fileName);

06 }

07 catch{}

08 }

09

10 private void btnRun_Click(object sender, System.EventArgs e)

11 {

12 Workbook workbook = new Workbook();

13 Worksheet sheet = workbook.Worksheets[0];

14

15 sheet.Range["B1"].Text = "NUMBER FORMATTING";

16 sheet.Range["B1"].Style.Font.IsBold = true;

17

18 sheet.Range["B3"].Text = "0";

19 sheet.Range["C3"].NumberValue = 1234.5678;

20 sheet.Range["C3"].NumberFormat = "0";

21

22 sheet.Range["B4"].Text = "0.00";

23 sheet.Range["C4"].NumberValue = 1234.5678;

24 sheet.Range["C4"].NumberFormat = "0.00";

Page 2: C# excel   set excel number style

25

26 sheet.Range["B5"].Text = "#,##0.00";

27 sheet.Range["C5"].NumberValue = 1234.5678;

28 sheet.Range["C5"].NumberFormat = "#,##0.00";

29

30 sheet.Range["B6"].Text = "$#,##0.00";

31 sheet.Range["C6"].NumberValue = 1234.5678;

32 sheet.Range["C6"].NumberFormat = "$#,##0.00";

33

34 sheet.Range["B7"].Text = "0;[Red]-0";

35 sheet.Range["C7"].NumberValue = -1234.5678;

36 sheet.Range["C7"].NumberFormat = "0;[Red]-0";

37

38 sheet.Range["B8"].Text = "0.00;[Red]-0.00";

39 sheet.Range["C8"].NumberValue = -1234.5678;

40 sheet.Range["C8"].NumberFormat = "0.00;[Red]-0.00";

41

42 sheet.Range["B9"].Text = "#,##0;[Red]-#,##0";

43 sheet.Range["C9"].NumberValue = -1234.5678;

44 sheet.Range["C9"].NumberFormat = "#,##0;[Red]-#,##0";

45

46 sheet.Range["B10"].Text = "#,##0.00;[Red]-#,##0.000";

47 sheet.Range["C10"].NumberValue = -1234.5678;

48 sheet.Range["C10"].NumberFormat = "#,##0.00;[Red]-#,##0.00";

49

50 sheet.Range["B11"].Text = "0.00E+00";

51 sheet.Range["C11"].NumberValue = 1234.5678;

52 sheet.Range["C11"].NumberFormat = "0.00E+00";

53

54 sheet.Range["B12"].Text = "0.00%";

55 sheet.Range["C12"].NumberValue = 1234.5678;

56 sheet.Range["C12"].NumberFormat = "0.00%";

57

58 sheet.Range["B3:B12"].Style.KnownColor = ExcelColors.Gray25Percent;

59

60

61 sheet.AutoFitColumn(2);

62 sheet.AutoFitColumn(3);

63

64

65 workbook.SaveToFile("Sample.xls");

66 ExcelDocViewer(workbook.FileName);

67 }

Page 3: C# excel   set excel number style

After running the code above in your application, you will get different number style as the image

below:

More about Spire.XSL

Download Spire.XSL

Purchase Spire.XLS

A short listing of the great formatting capabilities is provided below, but let's not regard it as a

replacement for the online reference Excel help on that matter.

To escape text, enclose it in quotes, like in $0.00" Surplus";$-0.00" Shortage"

To add a space character, include an _ (underscore).

Use # for insignificant digits. Using ####.#, 1234.59 displays as 1234.6

Use 0 for significant digits (or for padding). Using #.0#, 12 displays as 12.0

Use ? for alignment Use , as thousand separator

Use . as decimal separator

Use ; to separate the formatting when the number is positive, and the formatting when the

number is not

Use [Red] to add color to a cell, as in [Red]###,###.00. Among known colors are [Red],

[Black], [Cyan], [Green], [Magenta], [Blue], [White] and [Yellow], as well as colors from the

56 predefined palette of the form [Color___] where ___ is a number between 1 and 56. Use closed brackets to enclose conditions, or use the conditional formatting object. Example

using closed brackets : [Red][<=100];[Blue][>100]

Use % to display a number as a percentage. Please note that Excel will automatically perform

a 100 time multiplication. Use the typical date symbols to format dates. Symbols can be combined as below :

Page 4: C# excel   set excel number style