Excel spreadsheet library for C#/VB.NET

ExpertXls Excel Spreadsheet Library for .NET

Create Excel Spreadsheets in ASP.NET / C#

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:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Drawing;
using ExpertXls.ExcelLib;

public partial class CellStyles : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void lnkBtnCreateWorkbook_Click(object sender, EventArgs e)
    {
        // get the Excel workbook format
        ExcelWorkbookFormat workbookFormat = radioXlsFormat.Checked ? ExcelWorkbookFormat.Xls_2003 : ExcelWorkbookFormat.Xlsx_2007;

        // create the workbook in the desired format with a single worksheet
        ExcelWorkbook workbook = 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";

        #region CREATE CUSTOM WORKBOOK STYLES

        #region Add a style used for the cells in the worksheet title area

        ExcelCellStyle titleStyle = 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[ExcelCellBorderIndex.Bottom].Color = Color.Green;
        titleStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
        titleStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
        titleStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
        titleStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
        titleStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
        titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
        titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
        titleStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
        titleStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
        if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        {
            // 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;
        }
        // set the title area font 
        titleStyle.Font.Size = 14;
        titleStyle.Font.Bold = true;
        titleStyle.Font.UnderlineType = ExcelCellUnderlineType.Single;



        #endregion

        #region Add a style used for text messages

        ExcelCellStyle textMessageStyle = 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[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Thin;
        textMessageStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Thin;
        textMessageStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Thin;
        textMessageStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Thin;

        #endregion

        #region Add a custom cell style

        ExcelCellStyle customCellStyle = 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[ExcelCellBorderIndex.Bottom].Color = Color.Blue;
        customCellStyle.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
        customCellStyle.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
        customCellStyle.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
        customCellStyle.Borders[ExcelCellBorderIndex.Left].Color = Color.Red;
        customCellStyle.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
        customCellStyle.Borders[ExcelCellBorderIndex.Right].Color = Color.Yellow;
        customCellStyle.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;
        customCellStyle.Borders[ExcelCellBorderIndex.DiagonalDown].Color = Color.Orange;
        customCellStyle.Borders[ExcelCellBorderIndex.DiagonalDown].LineStyle = ExcelCellLineStyle.Medium;
        if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        {
            // 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;
        }
        // set the title area font 
        customCellStyle.Font.Size = 14;
        customCellStyle.Font.Bold = true;
        customCellStyle.Font.UnderlineType = ExcelCellUnderlineType.None;

        #endregion

        #endregion

        // get the first worksheet in the workbook
        ExcelWorksheet worksheet = workbook.Worksheets[0];

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

        #region 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
        string imagesPath = System.IO.Path.Combine(Server.MapPath("~"), @"Images");
        System.Drawing.Image logoImg = 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";

        #endregion

        #region WRITE THE WORKSHEET TOP TITLE

        // merge the cells in the range to create the title area 
        worksheet["A2:G3"].Merge();
        // gets the merged range containing the top left cell of the range
        ExcelRange titleRange = worksheet["A2"].MergeArea;
        // set the text of title area
        worksheet["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;

        #endregion


        #region SET CELLS WITH VARIOUS VALUES AND FORMATTING

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

        worksheet["A7:G7"].Style = customCellStyle;

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

        worksheet["A11:G11"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
        worksheet["A11:G11"].Style.Fill.SolidFillOptions.BackColor = Color.Orange;

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

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

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

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

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

        worksheet["A21:G21"].ColumnWidthInChars = 15;

        // set row height
        worksheet["A21:G21"].RowHeightInPoints = 75;
        // set row style
        if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        {
            // set the solid fill with a custom color
            worksheet["A21:G21"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
            worksheet["A21:G21"].Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
        }
        else
        {
            // set the gradient fill with a custom color
            worksheet["A21:G21"].Style.Fill.FillType = ExcelCellFillType.GradientFill;
            worksheet["A21:G21"].Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown;
            worksheet["A21:G21"].Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
            worksheet["A21:G21"].Style.Fill.GradientFillOptions.Color2 = Color.Orange;
        }
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
        worksheet["A21:G21"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;

        worksheet["A21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left;
        worksheet["A21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
        worksheet["A21"].Style.Font.Bold = true;
        worksheet["A21"].Style.Font.Color = Color.Red;
        worksheet["A21"].Value = "Top Left";

        worksheet["B21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
        worksheet["B21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom;
        worksheet["B21"].Style.Font.Bold = true;
        worksheet["B21"].Style.Font.Color = Color.Green;
        worksheet["B21"].Value = "Bottom Right";

        worksheet["C21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["C21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["C21"].Style.Font.Bold = true;
        worksheet["C21"].Style.Font.Color = Color.Blue;
        worksheet["C21"].Value = "Center Center";

        worksheet["D21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["D21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["D21"].Style.Alignment.Orientation = 90;
        worksheet["D21"].Style.Font.Bold = true;
        worksheet["D21"].Style.Font.Color = Color.Orange;
        worksheet["D21"].Value = "Center Vertical";

        worksheet["E21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["E21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
        worksheet["E21"].Style.Alignment.Orientation = -90;
        worksheet["E21"].Style.Font.Bold = true;
        worksheet["E21"].Style.Font.Color = Color.Red;
        worksheet["E21"].Value = "Top Vertical";

        worksheet["F21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
        worksheet["F21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Top;
        worksheet["F21"].Style.Font.Bold = true;
        worksheet["F21"].Style.Font.Color = Color.Green;
        worksheet["F21"].Value = "Right Top";

        worksheet["G21"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right;
        worksheet["G21"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Bottom;
        worksheet["G21"].Style.Font.Bold = true;
        worksheet["G21"].Style.Font.Color = Color.Blue;
        worksheet["G21"].Value = "Right Bottom";

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

        // set row style
        if (workbookFormat == ExcelWorkbookFormat.Xls_2003)
        {
            // set the solid fill with a custom color
            worksheet["A25:G25"].Style.Fill.FillType = ExcelCellFillType.SolidFill;
            worksheet["A25:G25"].Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204);
        }
        else
        {
            // set the gradient fill with a custom color
            worksheet["A25:G25"].Style.Fill.FillType = ExcelCellFillType.GradientFill;
            worksheet["A25:G25"].Style.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown;
            worksheet["A25:G25"].Style.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 255, 204);
            worksheet["A25:G25"].Style.Fill.GradientFillOptions.Color2 = Color.Orange;
        }
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
        worksheet["A25:G25"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;

        worksheet["A25"].Style.Font.Color = Color.Green;
        worksheet["A25"].Style.Font.Bold = true;
        worksheet["A25"].Style.Font.Size = 14;
        worksheet["A25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["A25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["A25"].Value = "Hello";

        worksheet["B25"].Style.Font.Color = Color.Blue;
        worksheet["B25"].Style.Font.Name = "Verdana";
        worksheet["B25"].Style.Font.Bold = true;
        worksheet["B25"].Style.Font.Size = 16;
        worksheet["B25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["B25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["B25"].Value = "Hello";

        worksheet["C25"].Style.Font.Color = Color.Red;
        worksheet["C25"].Style.Font.Name = "Times New Roman";
        worksheet["C25"].Style.Font.Bold = true;
        worksheet["C25"].Style.Font.Italic = true;
        worksheet["C25"].Style.Font.Size = 14;
        worksheet["C25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["C25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["C25"].Value = "Hello";


        worksheet["D25"].Style.Font.Color = Color.Green;
        worksheet["D25"].Style.Font.Bold = true;
        worksheet["D25"].Style.Font.Italic = true;
        worksheet["D25"].Style.Font.Size = 16;
        worksheet["D25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["D25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["D25"].Style.Alignment.Orientation = 90;
        worksheet["D25"].Value = "Hello";

        worksheet["E25"].Style.Font.Color = Color.Blue;
        worksheet["E25"].Style.Font.Bold = true;
        worksheet["E25"].Style.Font.Italic = true;
        worksheet["E25"].Style.Font.Size = 16;
        worksheet["E25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["E25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["E25"].Style.Alignment.Orientation = -90;
        worksheet["E25"].Value = "Hello";

        worksheet["F25"].Style.Font.Color = Color.Red;
        worksheet["F25"].Style.Font.Bold = true;
        worksheet["F25"].Style.Font.Italic = true;
        worksheet["F25"].Style.Font.UnderlineType = ExcelCellUnderlineType.DoubleAccounting;
        worksheet["F25"].Style.Font.Size = 14;
        worksheet["F25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["F25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["F25"].Value = "Hello";

        worksheet["G25"].Style.Font.Color = Color.Orange;
        worksheet["G25"].Style.Font.Bold = true;
        worksheet["G25"].Style.Font.IsStrikethrough = true;
        worksheet["G25"].Style.Font.Size = 14;
        worksheet["G25"].Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center;
        worksheet["G25"].Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center;
        worksheet["G25"].Value = "Hello";

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

        // set row height
        worksheet["A29:G29"].RowHeightInPoints = 75;

        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.DarkBlue;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.DarkBlue;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.DarkBlue;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Medium;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.DarkBlue;
        worksheet["A29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Medium;

        // all cell borders have the sae style
        worksheet["B29"].Style.Borders.Color = Color.Red;
        worksheet["B29"].Style.Borders.LineStyle = ExcelCellLineStyle.DashDot;

        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Green;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Double;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.Green;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Double;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.Green;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Double;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.Green;
        worksheet["C29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Double;

        worksheet["D29"].Style.Borders.Color = Color.Orange;
        worksheet["D29"].Style.Borders.LineStyle = ExcelCellLineStyle.Thick;

        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Bottom].Color = Color.Blue;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Bottom].LineStyle = ExcelCellLineStyle.Dot;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Top].Color = Color.Blue;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Top].LineStyle = ExcelCellLineStyle.Dot;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Left].Color = Color.Blue;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Left].LineStyle = ExcelCellLineStyle.Dot;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Right].Color = Color.Blue;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.Right].LineStyle = ExcelCellLineStyle.Dot;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.DiagonalDown].Color = Color.Blue;
        worksheet["E29"].Style.Borders[ExcelCellBorderIndex.DiagonalDown].LineStyle = ExcelCellLineStyle.Dot;

        worksheet["F29"].Style.Borders.Color = Color.Red;
        worksheet["F29"].Style.Borders.LineStyle = ExcelCellLineStyle.MediumDashDot;

        worksheet["G29"].Style.Borders.Color = Color.Green;
        worksheet["G29"].Style.Borders.LineStyle = ExcelCellLineStyle.Thin;


        #endregion

        // SAVE THE WORKBOOK

        // Save the Excel document in the current HTTP response stream

        string outFileName = workbookFormat == ExcelWorkbookFormat.Xls_2003 ? "CellStyles.xls" : "CellStyles.xlsx";

        System.Web.HttpResponse 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)
            httpResponse.ContentType = "Application/x-msexcel";
        else
            httpResponse.ContentType = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

        // 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 (Exception ex)
        {
            // report any error that might occur during save
            Session["ErrorMessage"] = ex.Message;
            Response.Redirect("ErrorPage.aspx");
        }
        finally
        {
            // close the workbook and release the allocated resources
            workbook.Close();

            #region Dispose the Image object

            if (logoImg != null)
                logoImg.Dispose();

            #endregion
        }

        // End the response and finish the execution of this page
        httpResponse.End();

    }
}

ExpertXls Excel Spreadsheet Library for .NET