none
How to set Border Style of Excel Work Sheet

    Question

  •  

    Hi all,

     

    I am new to Excel Programming.

     

    I have defined the worksheet as follow

     

    Excel._Worksheet ExcelWorkSheet;

     

    And I would like to define the border style for the range of A1 .. B10.

     

    May I know how to do that in C#?

     

    And does anyone know of any Excel Programming tutorial that will be useful for beginners?

     

    Thanks

    Tuesday, January 15, 2008 9:54 AM

Answers

  • Hi,

     

    We can get the range of "A1" to "B10" using ExcelWorkSheet.Application.get_Range("A1","B10"). With the got range, we set proper value to each item in Borders collection.

    Following is code:

    Code Block
                Excel.Range range = ExcelWorkSheet.Application.get_Range("A1","B10");
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom].Color = Color.Blue.ToArgb();
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft].Color = Color.Blue.ToArgb();
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight].Color = Color.Blue.ToArgb();
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop].Color = Color.Blue.ToArgb();
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideHorizontal].Color = Color.Blue.ToArgb();
                range.Borders[Microsoft.Office.Interop.Excel.XlBordersIndex.xlInsideVertical].Color = Color.Blue.ToArgb();

     

    There are six types border in Borders collection(Botton, Right, Top, Left, Horizontal, Vertical), we need to use Borders[index] to get each kind of border, and then set its Color or LineStyle property.

    To get more information about Excel programming, you can refer the following MSDN link which contains Reference, How Tos, Tech Articles:

    http://msdn2.microsoft.com/en-us/library/bb979621.aspx

     

     

    Thanks

    Ji

     

     

    Thursday, January 17, 2008 2:32 AM
    Moderator