Current location: Hot Scripts Forums » Programming Languages » C/C++ » How to Use C# to Set Excel Font


How to Use C# to Set Excel Font

Reply
  #1 (permalink)  
Old 01-05-11, 04:22 AM
TracyLandy TracyLandy is offline
New Member
 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
How to Use C# to Set Excel Font

In order to show a good-looking Excel file or emphasize one part of Excel, different font styles for cells, which include characters, are needed. Generally speaking, the frequently used styles contain font type, size and color.
Also, developers need to set Excel Font Style in C# during the process to operate Excel, the Excel file is completely formatted after the manipulation is finished.
Set Excel Font via Spire.XLS
Actually, the action to set C# Excel Font style is a process to assign the Excel Font properties values, which may be used depending on different requirements. For example, we may assign a value for Font.Color property to get desired color of specified contents in the worksheet.
The following code shows some basic settings on Excel Font style:
cpp Code:
  1. using System.Drawing;
  2. using Spire.Xls;
  3.  
  4. namespace FontStyle
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             //Create a new workbook.
  11.             Workbook workbook = new Workbook();
  12.             Worksheet sheet = workbook.Worksheets[0];
  13.            
  14.             //Write text with the font of Arial.
  15.             sheet.Range["B1"].Text = "Arial";
  16.             sheet.Range["B1"].Style.Font.FontName = "Arial";
  17.            
  18.             //Write text with the font of Large size.
  19.             sheet.Range["B2"].Text = "Large size";
  20.             sheet.Range["B2"].Style.Font.Size = 20;
  21.            
  22.             //Write text with the font of Bold.
  23.             sheet.Range["B3"].Text = "Bold";
  24.             sheet.Range["B3"].Style.Font.IsBold = true;
  25.            
  26.             //Write text with the font of Italic.
  27.             sheet.Range["B4"].Text = "Italic";
  28.             sheet.Range["B4"].Style.Font.IsItalic = true;
  29.            
  30.             //Write text with the font of Superscript.
  31.             sheet.Range["B5"].Text = "Superscript";
  32.             sheet.Range["B5"].Style.Font.IsSuperscript = true;
  33.            
  34.             //Write text with the font of Colored.
  35.             sheet.Range["B6"].Text = "Colored";
  36.             sheet.Range["B6"].Style.Font.Color = Color.FromArgb(255, 125, 125);
  37.            
  38.             //Write text with the font of Underline.
  39.             sheet.Range["B7"].Text = "Underline";
  40.             sheet.Range["B7"].Style.Font.Underline = FontUnderlineType.Single;
  41.            
  42.             //Save the file.
  43.             workbook.SaveToFile("Sample.xls");
  44.            
  45.             //Launch the file.
  46.             System.Diagnostics.Process.Start("Sample.xls");
  47.         }
  48.     }
  49. }
  50.           The following code shows some basic settings on VB.NET Excel Font style:
  51. [Visual Basic]
  52. Imports Microsoft.VisualBasic
  53. Imports System.Drawing
  54. Imports Spire.Xls
  55.  
  56. Module Module1
  57.  
  58.     Sub Main()
  59.         'Create a new workbook.
  60.         Dim workbook As Workbook = New Workbook()
  61.         Dim sheet As Worksheet = workbook.Worksheets(0)
  62.        
  63.         'Write text with the font of Arial.
  64.         sheet.Range("B1").Text = "Arial"
  65.         sheet.Range("B1").Style.Font.FontName = "Arial"
  66.        
  67.         'Write text with the font of Large size.
  68.         sheet.Range("B2").Text = "Large size"
  69.         sheet.Range("B2").Style.Font.Size = 20
  70.        
  71.         'Write text with the font of Bold.
  72.         sheet.Range("B3").Text = "Bold"
  73.         sheet.Range("B3").Style.Font.IsBold = True
  74.        
  75.         'Write text with the font of Italic.
  76.         sheet.Range("B4").Text = "Italic"
  77.         sheet.Range("B4").Style.Font.IsItalic = True
  78.        
  79.         'Write text with the font of Superscript.
  80.         sheet.Range("B5").Text = "Superscript"
  81.         sheet.Range("B5").Style.Font.IsSuperscript = True
  82.        
  83.         'Write text with the font of Colored.
  84.         sheet.Range("B6").Text = "Colored"
  85.         sheet.Range("B6").Style.Font.Color = Color.FromArgb(255, 125, 125)
  86.        
  87.         'Write text with the font of Underline.
  88.         sheet.Range("B7").Text = "Underline"
  89.         sheet.Range("B7").Style.Font.Underline = FontUnderlineType.Single
  90.        
  91.         'Save the file.
  92.         workbook.SaveToFile("Sample.xls")
  93.        
  94.         'Launch the file.
  95.         System.Diagnostics.Process.Start("Sample.xls")
  96.     End Sub
  97. End Module

Last edited by Nico; 01-05-11 at 04:36 AM.
Reply With Quote
  #2 (permalink)  
Old 01-10-11, 02:03 AM
AuroraHall AuroraHall is offline
New Member
 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
A useful component
Reply With Quote
The Following User Says Thank You to AuroraHall For This Useful Post:
TracyLandy (03-07-11)
  #3 (permalink)  
Old 03-07-11, 07:43 PM
TracyLandy TracyLandy is offline
New Member
 
Join Date: Jan 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by AuroraHall View Post
A useful component
I am sorry for that I see your message right now.
And I want to recommand my friend's blog for you.
janewdaisy
She wrote an article about export data to Excel. I think it is very useful.
Reply With Quote
Reply

Bookmarks

Tags
excel font


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed to Send Online Form Results to Excel File tbucki1 HTML/XHTML/XML 3 01-19-10 01:10 AM
VBScript: CSV to Excel, List seperator? odi Visual Basic 0 12-13-06 05:35 AM
ASP upload prob minority ASP 1 06-27-05 08:35 AM


All times are GMT -5. The time now is 01:20 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.