Use C# Add Excel Borders

Posted by Cc9530 under C# on 1/6/2011 | Points: 10 | Views : 20020 | Status : [Member] | Replies : 3
Here, we suppose you have written text in a cell of an Excel document. Now, you can add borders simply by setting the color and the line style with the sheet.Range["A1"].Borders.Color and sheet.Range["A1"].Borders.LineStyle methods. Something must be paid attention, sheet.Range["A1"].Borders method in Spire.XLS contains six borders:Four borders and two diagonals. You can delete the two diagonals by setting the sheet.Range["A1"].Borders[BordersLineType.DiagonalDown].LineStyle and sheet.Range["A1"].Borders[BordersLineType.DiagonalUp].LineStyle none.

Code of C# for adding excel borders :

01 using System.Drawing;

02 using Spire.Xls;

03

04 namespace ExtractText

05 {

06 class Program

07 {

08 static void Main(string[] args)

09 {

10 //Create a new workbook.

11 Workbook workbook = new Workbook();

12 Worksheet sheet = workbook.Worksheets[0];

13

14 //Write text in B2.

15 sheet.Range["B2"].Text = "Home page";

16

17 //Set the color of the six borders.

18 sheet.Range["B2"].Borders.Color = Color.Green;

19

20 //Set the line style of the borders.

21 sheet.Range["B2"].Borders.LineStyle = LineStyleType.Thick;

22

23 //Delete the diagonalDown border.

24 sheet.Range["B2"].Borders[BordersLineType.DiagonalDown].LineStyle = LineStyleType.None;

25

26 //Delete the DiagonalUp border.

27 sheet.Range["B2"].Borders[BordersLineType.DiagonalUp].LineStyle = LineStyleType.None;

28 sheet.AutoFitColumn(2);

29

30 //Save the file.

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

32

33 //Launch the file.

34 System.Diagnostics.Process.Start("Sample.xls");

35 }

36 }

37 }

use vb.net code here:
http://www.e-iceblue.com/Knowledgebase/Spire.XLS/Program-Guide/how-to-use-csharp-or-vbnet-to-add-excel-borders.html

i could be the one


Responses

Posted by: T.saravanan on: 1/6/2011 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi Cc9530,

Thanks for your sharing.

Thanks,
T.Saravanan

Cc9530, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikanbarasan on: 1/6/2011 [Member] [Moderator] [Microsoft_MVP] [MVP] Silver | Points: 25

Up
0
Down
Thanks

Nice code... i have a question here... when i export the datas from Grid view to excel, i can do the same formating stuffs andalso if want to take the same color of the grid view alternative rows wll that work here?

Thanks
Karthik
www.f5Debug.net

Cc9530, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Chvrsri on: 1/6/2011 [Member] [Moderator] [MVP] Silver | Points: 25

Up
0
Down
Hi Cc9530,

Your code might be very useful but , if u place it in Code Section it would be more better. Can u imagine booking a train ticket and going to a Bus Stand to board the train ? This is just like that.

Generally Users search codes at Codes Section . I feel your work shouldn't be wasted , so i kindly request you too move this post to codes Section . Then it will be used by many memebrs.

This is just my suggestion .

Hope you got my Point !!!


Thanks,
Radha Srikanth

Cc9530, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response