Excel spreadsheet library for C#/VB.NET

ExpertXls Excel Spreadsheet Library for .NET

Create Excel Spreadsheets in ASP.NET / VB.NET

Setting Cell Styles with ExpertXls Excel Library for .NET

This sample shows how to set various styles for the worksheet cells. A cell style consists in fill settings like solid fill, pattern fill or gradient fill, alignment, number format or border lines style.
To create the Excel workbook first select the format of the generated workbook and press the Create Workbook button. The Excel workbook will be created on the server and sent as an attachment to the browser. You will be prompted to open the generated workbook in an external viewer. ExpertXls library can generate both XLS and XLSX files.
Workbook Format:
Create Workbook Button Create Excel Workbook
Source Code:

Imports ExpertXls.ExcelLib
Imports System.Drawing

Partial Class CellStyles
    Inherits System.Web.UI.Page

    Protected Sub lnkBtnCreateWorkbook_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkBtnCreateWorkbook.Click
        ' get the Excel workbook format
        Dim workbookFormat As ExcelWorkbookFormat = ExcelWorkbookFormat.Xls_2003
        If radioXlsxFormat.Checked Then
            workbookFormat = ExcelWorkbookFormat.Xlsx_2007
        End If

        ' create the workbook in the desired format with a single worksheet
        Dim workbook As ExcelWorkbook = New ExcelWorkbook(workbookFormat)

        ' set the license key before saving the workbook
        'workbook.LicenseKey = "your license key here";

        ' set workbook description properties
        workbook.DocumentProperties.Subject = "Cell style demo"
        workbook.DocumentProperties.Comments = "Add cell styles to an Excel worksheet using ExpertXls Excel library for .NET"

        ' CREATE CUSTOM WORKBOOK STYLES

        ' Add a style used for the cells in the worksheet title area

        Dim titleStyle As ExcelCellStyle = workbook.Styles.AddStyle("WorksheetTitleStyle")
        ' center the text in the title area
        titleStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        titleStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        ' set the title area borders
        titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Green
        titleStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
        titleStyle.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
        titleStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
        titleStyle.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Green
        titleStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
        titleStyle.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Green
        titleStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            ' set the solid fill for the title area range with a custom color
            titleStyle.Fill.FillType = ExcelCellFillType.SolidFill
            titleStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        Else
            ' set the gradient fill for the title area range with a custom color
            titleStyle.Fill.FillType = ExcelCellFillType.GradientFill
            titleStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
            titleStyle.Fill.GradientFillOptions.Color2 = Color.White
        End If

        ' set the title area font 
        titleStyle.Font.Size = 14
        titleStyle.Font.Bold = True
        titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single

        ' Add a style used for text messages

        Dim textMessageStyle As ExcelCellStyle = workbook.Styles.AddStyle("TextMessageStyle")
        textMessageStyle.Font.Size = 12
        textMessageStyle.Font.Bold = True
        textMessageStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        textMessageStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
        textMessageStyle.Fill.FillType = ExcelCellFillType.PatternFill
        textMessageStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent
        textMessageStyle.Fill.PatternFillOptions.BackColor = Color.White
        textMessageStyle.Fill.PatternFillOptions.PatternColor = Color.Green
        textMessageStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Thin
        textMessageStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Thin
        textMessageStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Thin
        textMessageStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Thin



        ' Add a custom cell style

        Dim customCellStyle As ExcelCellStyle = workbook.Styles.AddStyle("CustomCellStyle")
        ' center the text in the title area
        customCellStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        customCellStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        ' set the title area borders
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Blue
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Red
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Yellow
        customCellStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium
        customCellStyle.Borders.Item(ExcelCellBorderIndex.DiagonalDown).Color = Color.Orange
        customCellStyle.Borders.Item(ExcelCellBorderIndex.DiagonalDown).LineStyle = ExcelCellLineStyle.Medium
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then

            ' set the solid fill for the title area range with a custom color
            customCellStyle.Fill.FillType = ExcelCellFillType.SolidFill
            customCellStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)

        Else

            ' set the gradient fill for the title area range with a custom color
            customCellStyle.Fill.FillType = ExcelCellFillType.GradientFill
            customCellStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
            customCellStyle.Fill.GradientFillOptions.Color2 = Color.Orange
        End If

        ' set the title area font 
        customCellStyle.Font.Size = 14
        customCellStyle.Font.Bold = True
        customCellStyle.Font.UnderlineType = ExcelCellUnderlineType.None

        ' get the first worksheet in the workbook
        Dim worksheet As ExcelWorksheet = workbook.Worksheets.Item(0)

        ' set the default worksheet name
        worksheet.Name = "Cell Styles Demo"

        ' WORKSHEET PAGE SETUP

        ' set worksheet paper size and orientation, margins, header and footer
        worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
        worksheet.PageSetup.Orientation = ExcelPageOrientation.Landscape
        worksheet.PageSetup.LeftMargin = 1
        worksheet.PageSetup.RightMargin = 1
        worksheet.PageSetup.TopMargin = 1
        worksheet.PageSetup.BottomMargin = 1

        ' add header and footer

        ' display a logo image in the left part of the header
        Dim imagesPath As String = System.IO.Path.Combine(Server.MapPath("~"), "Images")
        Dim logoImg As System.Drawing.Image = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "logo.jpg"))
        worksheet.PageSetup.LeftHeaderFormat = "&G"
        worksheet.PageSetup.LeftHeaderPicture = logoImg
        ' display worksheet name in the right part of the header
        worksheet.PageSetup.RightHeaderFormat = "&A"

        ' add worksheet header and footer
        ' display the page number in the center part of the footer
        worksheet.PageSetup.CenterFooterFormat = "&P"
        ' display the workbook file name in the left part of the footer
        worksheet.PageSetup.LeftFooterFormat = "&F"
        ' display the current date in the right part of the footer
        worksheet.PageSetup.RightFooterFormat = "&D"

        ' WRITE THE WORKSHEET TOP TITLE

        ' merge the cells in the range to create the title area 
        worksheet.Item("A2:G3").Merge()
        ' gets the merged range containing the top left cell of the range
        Dim titleRange As ExcelRange = worksheet.Item("A2").MergeArea
        ' set the text of title area
        worksheet.Item("A2").Text = "Cell Styles Demo"

        ' set a row height of 18 points for each row in the range
        titleRange.RowHeightInPoints = 18
        ' set the worksheet top title style
        titleRange.Style = titleStyle

        ' SET CELLS WITH VARIOUS VALUES AND FORMATTING

        worksheet.Item("A5:C5").Merge()
        worksheet.Item("A5:C5").Style = textMessageStyle
        worksheet.Item("A5:C5").RowHeightInPoints = 25
        worksheet.Item("A5").Value = "Custom Global Style Demo"

        worksheet.Item("A7:G7").Style = customCellStyle

        worksheet.Item("A9:C9").Merge()
        worksheet.Item("A9:C9").Style = textMessageStyle
        worksheet.Item("A9:C9").RowHeightInPoints = 25
        worksheet.Item("A9").Value = "Solid Fill"

        worksheet.Item("A11:G11").Style.Fill.FillType = ExcelCellFillType.SolidFill
        worksheet.Item("A11:G11").Style.Fill.SolidFillOptions.BackColor = Color.Orange

        worksheet.Item("A13:C13").Merge()
        worksheet.Item("A13:C13").Style = textMessageStyle
        worksheet.Item("A13:C13").RowHeightInPoints = 25
        worksheet.Item("A13").Value = "Pattern Fill Demo"

        worksheet.Item("A15:G15").Style.Fill.FillType = ExcelCellFillType.PatternFill
        worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent
        worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        worksheet.Item("A15:G15").Style.Fill.PatternFillOptions.PatternColor = Color.Orange

        worksheet.Item("A16:G16").Style.Fill.FillType = ExcelCellFillType.PatternFill
        worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.DiagonalCrosshatch
        worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        worksheet.Item("A16:G16").Style.Fill.PatternFillOptions.PatternColor = Color.Orange

        worksheet.Item("A17:G17").Style.Fill.FillType = ExcelCellFillType.PatternFill
        worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.HorizontalStripe
        worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        worksheet.Item("A17:G17").Style.Fill.PatternFillOptions.PatternColor = Color.Orange

        worksheet.Item("A19:C19").Merge()
        worksheet.Item("A19:C19").Style = textMessageStyle
        worksheet.Item("A19:C19").RowHeightInPoints = 25
        worksheet.Item("A19").Value = "Text Alignment Demo"

        worksheet.Item("A21:G21").ColumnWidthInChars = 15

        ' set row height
        worksheet.Item("A21:G21").RowHeightInPoints = 75
        ' set row style
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            ' set the solid fill with a custom color
            worksheet.Item("A21:G21").Style.Fill.FillType = ExcelCellFillType.SolidFill
            worksheet.Item("A21:G21").Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        Else
            ' set the gradient fill with a custom color
            worksheet.Item("A21:G21").Style.Fill.FillType = ExcelCellFillType.GradientFill
            worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
            worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
            worksheet.Item("A21:G21").Style.Fill.GradientFillOptions.Color2 = Color.Orange
        End If

        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
        worksheet.Item("A21:G21").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium

        worksheet.Item("A21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
        worksheet.Item("A21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
        worksheet.Item("A21").Style.Font.Bold = True
        worksheet.Item("A21").Style.Font.Color = Color.Red
        worksheet.Item("A21").Value = "Top Left"

        worksheet.Item("B21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
        worksheet.Item("B21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom
        worksheet.Item("B21").Style.Font.Bold = True
        worksheet.Item("B21").Style.Font.Color = Color.Green
        worksheet.Item("B21").Value = "Bottom Right"

        worksheet.Item("C21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("C21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("C21").Style.Font.Bold = True
        worksheet.Item("C21").Style.Font.Color = Color.Blue
        worksheet.Item("C21").Value = "Center Center"

        worksheet.Item("D21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("D21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("D21").Style.Alignment.Orientation = 90
        worksheet.Item("D21").Style.Font.Bold = True
        worksheet.Item("D21").Style.Font.Color = Color.Orange
        worksheet.Item("D21").Value = "Center Vertical"

        worksheet.Item("E21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("E21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
        worksheet.Item("E21").Style.Alignment.Orientation = -90
        worksheet.Item("E21").Style.Font.Bold = True
        worksheet.Item("E21").Style.Font.Color = Color.Red
        worksheet.Item("E21").Value = "Top Vertical"

        worksheet.Item("F21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
        worksheet.Item("F21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top
        worksheet.Item("F21").Style.Font.Bold = True
        worksheet.Item("F21").Style.Font.Color = Color.Green
        worksheet.Item("F21").Value = "Right Top"

        worksheet.Item("G21").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
        worksheet.Item("G21").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom
        worksheet.Item("G21").Style.Font.Bold = True
        worksheet.Item("G21").Style.Font.Color = Color.Blue
        worksheet.Item("G21").Value = "Right Bottom"

        worksheet.Item("A23:C23").Merge()
        worksheet.Item("A23:C23").Style = textMessageStyle
        worksheet.Item("A23:C23").RowHeightInPoints = 25
        worksheet.Item("A23").Value = "Fonts Demo"

        ' set row style
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            ' set the solid fill with a custom color
            worksheet.Item("A25:G25").Style.Fill.FillType = ExcelCellFillType.SolidFill
            worksheet.Item("A25:G25").Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        Else
            ' set the gradient fill with a custom color
            worksheet.Item("A25:G25").Style.Fill.FillType = ExcelCellFillType.GradientFill
            worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
            worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204)
            worksheet.Item("A25:G25").Style.Fill.GradientFillOptions.Color2 = Color.Orange
        End If

        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
        worksheet.Item("A25:G25").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium

        worksheet.Item("A25").Style.Font.Color = Color.Green
        worksheet.Item("A25").Style.Font.Bold = True
        worksheet.Item("A25").Style.Font.Size = 14
        worksheet.Item("A25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("A25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("A25").Value = "Hello"

        worksheet.Item("B25").Style.Font.Color = Color.Blue
        worksheet.Item("B25").Style.Font.Name = "Verdana"
        worksheet.Item("B25").Style.Font.Bold = True
        worksheet.Item("B25").Style.Font.Size = 16
        worksheet.Item("B25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("B25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("B25").Value = "Hello"

        worksheet.Item("C25").Style.Font.Color = Color.Red
        worksheet.Item("C25").Style.Font.Name = "Times New Roman"
        worksheet.Item("C25").Style.Font.Bold = True
        worksheet.Item("C25").Style.Font.Italic = True
        worksheet.Item("C25").Style.Font.Size = 14
        worksheet.Item("C25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("C25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("C25").Value = "Hello"


        worksheet.Item("D25").Style.Font.Color = Color.Green
        worksheet.Item("D25").Style.Font.Bold = True
        worksheet.Item("D25").Style.Font.Italic = True
        worksheet.Item("D25").Style.Font.Size = 16
        worksheet.Item("D25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("D25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("D25").Style.Alignment.Orientation = 90
        worksheet.Item("D25").Value = "Hello"

        worksheet.Item("E25").Style.Font.Color = Color.Blue
        worksheet.Item("E25").Style.Font.Bold = True
        worksheet.Item("E25").Style.Font.Italic = True
        worksheet.Item("E25").Style.Font.Size = 16
        worksheet.Item("E25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("E25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("E25").Style.Alignment.Orientation = -90
        worksheet.Item("E25").Value = "Hello"

        worksheet.Item("F25").Style.Font.Color = Color.Red
        worksheet.Item("F25").Style.Font.Bold = True
        worksheet.Item("F25").Style.Font.Italic = True
        worksheet.Item("F25").Style.Font.UnderlineType = ExcelCellUnderlineType.DoubleAccounting
        worksheet.Item("F25").Style.Font.Size = 14
        worksheet.Item("F25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("F25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("F25").Value = "Hello"

        worksheet.Item("G25").Style.Font.Color = Color.Orange
        worksheet.Item("G25").Style.Font.Bold = True
        worksheet.Item("G25").Style.Font.IsStrikethrough = True
        worksheet.Item("G25").Style.Font.Size = 14
        worksheet.Item("G25").Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        worksheet.Item("G25").Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        worksheet.Item("G25").Value = "Hello"

        worksheet.Item("A27:C27").Merge()
        worksheet.Item("A27:C27").Style = textMessageStyle
        worksheet.Item("A27:C27").RowHeightInPoints = 25
        worksheet.Item("A27").Value = "Borders Demo"

        ' set row height
        worksheet.Item("A29:G29").RowHeightInPoints = 75

        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.DarkBlue
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.DarkBlue
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.DarkBlue
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Medium
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.DarkBlue
        worksheet.Item("A29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Medium

        ' all cell borders have the sae style
        worksheet.Item("B29").Style.Borders.Color = Color.Red
        worksheet.Item("B29").Style.Borders.LineStyle = ExcelCellLineStyle.DashDot

        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Green
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Double
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Green
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Double
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Green
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Double
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Green
        worksheet.Item("C29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Double

        worksheet.Item("D29").Style.Borders.Color = Color.Orange
        worksheet.Item("D29").Style.Borders.LineStyle = ExcelCellLineStyle.Thick

        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).Color = Color.Blue
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Dot
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Top).Color = Color.Blue
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Dot
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Left).Color = Color.Blue
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Dot
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Right).Color = Color.Blue
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Dot
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.DiagonalDown).Color = Color.Blue
        worksheet.Item("E29").Style.Borders.Item(ExcelCellBorderIndex.DiagonalDown).LineStyle = ExcelCellLineStyle.Dot

        worksheet.Item("F29").Style.Borders.Color = Color.Red
        worksheet.Item("F29").Style.Borders.LineStyle = ExcelCellLineStyle.MediumDashDot

        worksheet.Item("G29").Style.Borders.Color = Color.Green
        worksheet.Item("G29").Style.Borders.LineStyle = ExcelCellLineStyle.Thin

        ' SAVE THE WORKBOOK

        ' Save the Excel document in the current HTTP response stream

        Dim outFileName As String
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            outFileName = "CellStyles.xls"
        Else
            outFileName = "CellStyles.xlsx"
        End If

        Dim httpResponse As System.Web.HttpResponse = System.Web.HttpContext.Current.Response

        ' Prepare the HTTP response stream for saving the Excel document

        ' Clear any data that might have been previously buffered in the output stream
        httpResponse.Clear()

        ' Set output stream content type for Excel 97-2003 (.xls) or Excel 2007-2013 (.xlsx)
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            httpResponse.ContentType = "Application/x-msexcel"
        Else
            httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        End If

        ' Add the HTTP header to announce the Excel document either as an attachment or inline
        httpResponse.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", outFileName))

        ' Save the workbook to the current HTTP response output stream
        ' and close the workbook after save to release all the allocated resources
        Try
            workbook.Save(httpResponse.OutputStream)
        Catch ex As Exception
            ' report any error that might occur during save
            Session.Item("ErrorMessage") = ex.Message
            Response.Redirect("ErrorPage.aspx")
        Finally
            ' close the workbook and release the allocated resources
            workbook.Close()

            ' Dispose the Image object

            If Not logoImg Is Nothing Then
                logoImg.Dispose()
            End If

        End Try

        ' End the response and finish the execution of this page
        httpResponse.End()
    End Sub
End Class

ExpertXls Excel Spreadsheet Library for .NET