Excel spreadsheet library for C#/VB.NET

ExpertXls Excel Spreadsheet Library for .NET

Create Excel Spreadsheets in ASP.NET / VB.NET

Getting Started with ExpertXls Excel Library for .NET

This sample shows how to create an Excel workbook, add worksheets to the workbook, set a value for a cell or a range of cells in the worksheet and apply basic formatting settings to the cells and ranges.
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 System.Drawing
Imports ExpertXls.ExcelLib


Partial Class _Default
    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 = "Getting started sample"
        workbook.DocumentProperties.Comments = "Getting started with ExpertXls Excel library for .NET"

        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

        

        Dim indexStyle As ExcelCellStyle = workbook.Styles.AddStyle("IndexColumnStyle")
        indexStyle.Font.Size = 12
        indexStyle.Font.Bold = False
        indexStyle.Fill.FillType = ExcelCellFillType.SolidFill
        indexStyle.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        indexStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Thin
        indexStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Thin
        indexStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Thin
        indexStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Thin

        

        Dim countryNameStyle As ExcelCellStyle = workbook.Styles.AddStyle("CountryNameStyle")
        countryNameStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
        countryNameStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        countryNameStyle.Font.Size = 12
        countryNameStyle.Font.Bold = True
        countryNameStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Dot
        countryNameStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Dot
        countryNameStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Dot
        countryNameStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Dot

        countryNameStyle.Fill.FillType = ExcelCellFillType.PatternFill
        countryNameStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_12_5_Percent
        countryNameStyle.Fill.PatternFillOptions.BackColor = Color.White
        countryNameStyle.Fill.PatternFillOptions.PatternColor = Color.Green

        ' Add a style used for all the cells containing the hello world text

        Dim helloWorldStyle As ExcelCellStyle = workbook.Styles.AddStyle("HelloWorldStyle")
        helloWorldStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
        helloWorldStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        helloWorldStyle.Font.Size = 14
        helloWorldStyle.Font.Bold = True

        helloWorldStyle.Borders.Item(ExcelCellBorderIndex.Bottom).LineStyle = ExcelCellLineStyle.Hair
        helloWorldStyle.Borders.Item(ExcelCellBorderIndex.Top).LineStyle = ExcelCellLineStyle.Hair
        helloWorldStyle.Borders.Item(ExcelCellBorderIndex.Left).LineStyle = ExcelCellLineStyle.Hair
        helloWorldStyle.Borders.Item(ExcelCellBorderIndex.Right).LineStyle = ExcelCellLineStyle.Hair

        If (workbook.Format = ExcelWorkbookFormat.Xls_2003) Then
            helloWorldStyle.Fill.FillType = ExcelCellFillType.PatternFill
            helloWorldStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_6_25_Percent
            helloWorldStyle.Fill.PatternFillOptions.BackColor = Color.White
            helloWorldStyle.Fill.PatternFillOptions.PatternColor = Color.Orange
        Else
            helloWorldStyle.Fill.FillType = ExcelCellFillType.GradientFill
            helloWorldStyle.Fill.GradientFillOptions.Direction = ExcelGradientDirection.Vertical
            helloWorldStyle.Fill.GradientFillOptions.Color1 = Color.FromArgb(255, 204, 153)
            helloWorldStyle.Fill.GradientFillOptions.Color2 = Color.White
        End If



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

        ' set the default worksheet name
        worksheet.Name = "Hello World"

        ' WORKSHEET PAGE SETUP

        ' set worksheet paper size and orientation, margins, header and footer
        worksheet.PageSetup.PaperSize = ExcelPagePaperSize.PaperA4
        worksheet.PageSetup.Orientation = ExcelPageOrientation.Portrait
        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 = "Say 'Hello World' in Different Languages"

        ' 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

        ' WRITE 'HELLO WORLD' IN DIFFERENT LANGUAGES

        Dim usFlagImg as System.Drawing.Image  = Nothing
        Dim frFlagImg As System.Drawing.Image = Nothing
        Dim deFlagImg As System.Drawing.Image = Nothing
        Dim esFlagImg As System.Drawing.Image = Nothing
        Dim ruFlagImg As System.Drawing.Image = Nothing
        Dim itFlagImg As System.Drawing.Image = Nothing
        Dim ptFlagImg As System.Drawing.Image = Nothing
        Dim nlFlagImg As System.Drawing.Image = Nothing

        ' set the separator between index column and text column
        worksheet.SetColumnWidthInChars(2, 3)
        ' set the country image column
        worksheet.SetColumnWidthInChars(6, 5)

        ' Say hello world in English

        worksheet.Item("A5").Style = indexStyle
        worksheet.Item("A5").Value = 1

        worksheet.Item("C5").Text = "Hello World !!!"
        worksheet.Item("C5").Style = helloWorldStyle
        worksheet.Item("C5").RowHeightInPoints = 19.5

        usFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "us.png"))
        worksheet.Pictures.AddPicture(6, 5, usFlagImg, 90, 90)
        worksheet.Item(5, 6, 5, 7).StyleName = "CountryNameStyle"
        worksheet.Item(5, 7).Text = "English"

        '  Say hello world in French

        worksheet.Item("A7").Style = indexStyle
        worksheet.Item("A7").Value = 2

        worksheet.Item("C7").Text = "Bonjour tout le monde !!!"
        worksheet.Item("C7").Style = helloWorldStyle
        worksheet.Item("C7").Style.Font.Color = Color.Blue
        worksheet.Item("C7").RowHeightInPoints = 19.5

        frFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "fr.png"))
        worksheet.Pictures.AddPicture(6, 7, frFlagImg, 90, 90)
        worksheet.Item(7, 6, 7, 7).StyleName = "CountryNameStyle"
        worksheet.Item(7, 7).Text = "French"

        ' Say hello world in German

        worksheet.Item("A9").Style = indexStyle
        worksheet.Item("A9").Value = 3

        worksheet.Item("C9").Text = "Hallo Welt"
        worksheet.Item("C9").Style = helloWorldStyle
        worksheet.Item("C9").Style.Font.Color = Color.Red
        worksheet.Item("C9").RowHeightInPoints = 19.5

        deFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "de.png"))
        worksheet.Pictures.AddPicture(6, 9, deFlagImg, 90, 90)
        worksheet.Item(9, 6, 9, 7).StyleName = "CountryNameStyle"
        worksheet.Item(9, 7).Text = "German"

        ' Say hello world in Spanish

        worksheet.Item("A11").Style = indexStyle
        worksheet.Item("A11").Value = 4

        worksheet.Item("C11").Text = "Hola Mundo"
        worksheet.Item("C11").Style = helloWorldStyle
        worksheet.Item("C11").Style.Font.Color = Color.Orange
        worksheet.Item("C11").RowHeightInPoints = 19.5

        esFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "es.png"))
        worksheet.Pictures.AddPicture(6, 11, esFlagImg, 90, 90)
        worksheet.Item(11, 6, 11, 7).StyleName = "CountryNameStyle"
        worksheet.Item(11, 7).Text = "Spanish"

        ' Say hello world in Russian

        worksheet.Item("A13").Style = indexStyle
        worksheet.Item("A13").Value = 5

        worksheet.Item("C13").Text = "Привет мир"
        worksheet.Item("C13").Style = helloWorldStyle
        worksheet.Item("C13").Style.Font.Color = Color.Indigo
        worksheet.Item("C13").RowHeightInPoints = 19.5

        ruFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "ru.png"))
        worksheet.Pictures.AddPicture(6, 13, ruFlagImg, 90, 90)
        worksheet.Item(13, 6, 13, 7).StyleName = "CountryNameStyle"
        worksheet.Item(13, 7).Text = "Russian"

        ' Say hello world in Italian

        worksheet.Item("A15").Style = indexStyle
        worksheet.Item("A15").Value = 6

        worksheet.Item("C15").Text = "Ciao a tutti"
        worksheet.Item("C15").Style = helloWorldStyle
        worksheet.Item("C15").Style.Font.Color = Color.Green
        worksheet.Item("C15").RowHeightInPoints = 19.5

        itFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "it.png"))
        worksheet.Pictures.AddPicture(6, 15, itFlagImg, 90, 90)
        worksheet.Item(15, 6, 15, 7).StyleName = "CountryNameStyle"
        worksheet.Item(15, 7).Text = "Italian"

        ' Say hello world in Dutch

        worksheet.Item("A17").Style = indexStyle
        worksheet.Item("A17").Value = 7


        worksheet.Item("C17").Text = "Hallo Wereld"
        worksheet.Item("C17").Style = helloWorldStyle
        worksheet.Item("C17").Style.Font.Color = Color.Blue
        worksheet.Item("C17").RowHeightInPoints = 19.5

        nlFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "nl.png"))
        worksheet.Pictures.AddPicture(6, 17, nlFlagImg, 90, 90)
        worksheet.Item(17, 6, 17, 7).StyleName = "CountryNameStyle"
        worksheet.Item(17, 7).Text = "Dutch"

        ' Say hello world in Portuguese

        worksheet.Item("A19").Style = indexStyle
        worksheet.Item("A19").Value = 8

        worksheet.Item("C19").Text = "Olá Mundo"
        worksheet.Item("C19").Style = helloWorldStyle
        worksheet.Item("C19").Style.Font.Color = Color.Red
        worksheet.Item("C19").RowHeightInPoints = 19.5

        ptFlagImg = System.Drawing.Image.FromFile(System.IO.Path.Combine(imagesPath, "pt.png"))
        worksheet.Pictures.AddPicture(6, 19, ptFlagImg, 90, 90)
        worksheet.Item(19, 6, 19, 7).StyleName = "CountryNameStyle"
        worksheet.Item(19, 7).Text = "Portuguese"

        ' autofit the index column width
        worksheet.AutofitColumn(1)
        ' autofit the Hello World text column
        worksheet.AutofitColumn(3)
        ' autofit the country name column
        worksheet.AutofitColumn(7)

        ' WRITE WORKSHEET CREATION TIMESTAMP

        worksheet.Item(28, 1, 28, 3).Merge()
        Dim timestampTextRange As ExcelRange = worksheet.Item(28, 1).MergeArea
        timestampTextRange.Style.Font.Bold = True
        worksheet.Item(28, 1).Text = "Workbook Creation Date & Time:"

        worksheet.Item(28, 4, 28, 7).Merge()
        Dim timestampDateRange As ExcelRange = worksheet.Item(28, 4).MergeArea
        timestampDateRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Left
        timestampDateRange.Style.Number.NumberFormatString = "mm/dd/yyyy hh:mm:ss"
        timestampDateRange.Style.Font.Color = Color.DarkBlue
        timestampDateRange.Style.Font.Bold = True
        worksheet.Item(28, 4).Value = DateTime.Now

        ' ADD A SECOND WORKSHEET TO THE WORKBOOK

        Dim secondWorksheet As ExcelWorksheet = workbook.Worksheets.AddWorksheet("Simple Chart")

        ' SECOND WORKSHEET PAGE SETUP

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

        ' add header and footer

        'display a logo image in the left part of the header
        secondWorksheet.PageSetup.LeftHeaderFormat = "&G"
        secondWorksheet.PageSetup.LeftHeaderPicture = logoImg
        ' display worksheet name in the right part of the header
        secondWorksheet.PageSetup.RightHeaderFormat = "&A"

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

        ' WRITE THE SECOND WORKSHEET TOP TITLE

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

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

        ' CREATE DATA TABLE FOR THE CHART IN SECOND WORKSHEET

        Dim chartValuesStyle As ExcelCellStyle = workbook.Styles.AddStyle("ChartValuesStyle")
        chartValuesStyle.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        chartValuesStyle.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        chartValuesStyle.Font.Color = Color.Black
        chartValuesStyle.Font.Bold = True
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            chartValuesStyle.Fill.FillType = ExcelCellFillType.PatternFill
            chartValuesStyle.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_6_25_Percent
            chartValuesStyle.Fill.PatternFillOptions.BackColor = Color.White
            chartValuesStyle.Fill.PatternFillOptions.PatternColor = Color.Green
        Else

            chartValuesStyle.Fill.FillType = ExcelCellFillType.GradientFill
            chartValuesStyle.Fill.GradientFillOptions.Color1 = Color.LightGreen
            chartValuesStyle.Fill.GradientFillOptions.Color2 = Color.White
            chartValuesStyle.Fill.GradientFillOptions.Direction = ExcelGradientDirection.DiagonlDown
        End If


        ' set the products tile row text and style 
        secondWorksheet.Item("C6:G6").Merge()
        secondWorksheet.Item("C6").Text = "Analyzed Products"
        Dim productsTitle As ExcelRange = secondWorksheet.Item("C6").MergeArea
        productsTitle.RowHeightInPoints = 21
        productsTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        productsTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        productsTitle.Style.Font.Size = 12
        productsTitle.Style.Font.Bold = True
        productsTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill
        productsTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent
        productsTitle.Style.Fill.PatternFillOptions.BackColor = Color.White
        productsTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Orange

        ' set the quarters title column text and style
        secondWorksheet.Item("A8:A11").Merge()
        secondWorksheet.Item("A8").Text = "Units Sold per Quarter"
        Dim quartersTitle As ExcelRange = secondWorksheet.Item("A8").MergeArea
        ' set vertical orientation for the text from bottom to top
        quartersTitle.Style.Alignment.Orientation = 90
        ' wrap text inside the merged range
        quartersTitle.Style.Alignment.WrapText = True
        quartersTitle.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        quartersTitle.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        quartersTitle.Style.Font.Size = 12
        quartersTitle.Style.Font.Bold = True
        quartersTitle.Style.Fill.FillType = ExcelCellFillType.PatternFill
        quartersTitle.Style.Fill.PatternFillOptions.PatternType = ExcelCellFillPatternType.Gray_25_Percent
        quartersTitle.Style.Fill.PatternFillOptions.BackColor = Color.White
        quartersTitle.Style.Fill.PatternFillOptions.PatternColor = Color.Green

        ' set the style for the product names row
        Dim productNamesRange As ExcelRange = secondWorksheet.Item("B7:G7")
        productNamesRange.RowHeightInPoints = 21
        productNamesRange.ColumnWidthInChars = 16
        productNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        productNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        productNamesRange.Style.Font.Bold = True
        productNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
        productNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)


        ' set the styles for the quarter names range
        Dim quarterNamesRange As ExcelRange = secondWorksheet.Item("B8:B11")
        quarterNamesRange.RowHeightInPoints = 16.5
        quarterNamesRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Right
        quarterNamesRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        quarterNamesRange.Style.Font.Bold = True
        quarterNamesRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
        quarterNamesRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)



        ' set the row height for the Yearly Total row
        Dim yearlyTotalRange As ExcelRange = secondWorksheet.Item("B12:G12")
        yearlyTotalRange.RowHeightInPoints = 21
        yearlyTotalRange.Style.Alignment.HorizontalAlignment = ExcelCellHorizontalAlignmentType.Center
        yearlyTotalRange.Style.Alignment.VerticalAlignment = ExcelCellVerticalAlignmentType.Center
        yearlyTotalRange.Style.Font.Color = Color.Blue
        yearlyTotalRange.Style.Font.Bold = True
        yearlyTotalRange.Style.Fill.FillType = ExcelCellFillType.SolidFill
        yearlyTotalRange.Style.Fill.SolidFillOptions.BackColor = Color.FromArgb(204, 255, 255)

        ' set the series name column
        secondWorksheet.Item("B8").Text = "First Quarter"
        secondWorksheet.Item("B9").Text = "Second Quarter"
        secondWorksheet.Item("B10").Text = "Third Quarter"
        secondWorksheet.Item("B11").Text = "Fourth Quarter"
        secondWorksheet.Item("B12").Text = "Yearly Total"

        ' set the categories name row
        secondWorksheet.Item("C7").Text = "HTML to PDF"
        secondWorksheet.Item("D7").Text = "PDF Merge"
        secondWorksheet.Item("E7").Text = "PDF Security"
        secondWorksheet.Item("F7").Text = "Web Chart"
        secondWorksheet.Item("G7").Text = "Excel Library"

        ' set the chart value style

        Dim chartValuesRange As ExcelRange = secondWorksheet.Item("C8:G11")
        chartValuesRange.Style = chartValuesStyle

        ' set the chart values

        secondWorksheet.Item("C8").Value = 1000
        secondWorksheet.Item("D8").Value = 500
        secondWorksheet.Item("E8").Value = 200
        secondWorksheet.Item("F8").Value = 400
        secondWorksheet.Item("G8").Value = 800

        secondWorksheet.Item("C9").Value = 850
        secondWorksheet.Item("D9").Value = 680
        secondWorksheet.Item("E9").Value = 350
        secondWorksheet.Item("F9").Value = 230
        secondWorksheet.Item("G9").Value = 640

        secondWorksheet.Item("C10").Value = 950
        secondWorksheet.Item("D10").Value = 450
        secondWorksheet.Item("E10").Value = 175
        secondWorksheet.Item("F10").Value = 350
        secondWorksheet.Item("G10").Value = 520

        secondWorksheet.Item("C11").Value = 500
        secondWorksheet.Item("D11").Value = 700
        secondWorksheet.Item("E11").Value = 250
        secondWorksheet.Item("F11").Value = 460
        secondWorksheet.Item("G11").Value = 320

        secondWorksheet.Item("C12").Formula = "=SUM(C8:C11)"
        secondWorksheet.Item("D12").Formula = "=SUM(D8:D11)"
        secondWorksheet.Item("E12").Formula = "=SUM(E8:E11)"
        secondWorksheet.Item("F12").Formula = "=SUM(F8:F11)"
        secondWorksheet.Item("G12").Formula = "=SUM(G8:G11)"

        ' auto fit the width of the quarter names column
        secondWorksheet.Item("B7").AutofitColumns()

        ' ADD A CHART TO THE SECOND WORKSHEET

        Dim dataSourceRange As ExcelRange = secondWorksheet.Item("B7:G12")
        Dim chart As ExcelChart = secondWorksheet.Charts.AddChart(ExcelChartType.ColumnClustered, dataSourceRange, True, 2, 15, 8, 32)
        chart.ShowDataTable = False

        ' set chart title
        chart.Title.Text = "Product Units Sold per Quarter"
        chart.Title.Interior.FillType = ExcelShapeFillType.NoFill
        chart.Title.Font.Size = 12
        chart.Title.Font.Color = Color.DarkBlue

        ' set chart area style
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then

            chart.ChartArea.Interior.FillType = ExcelShapeFillType.SolidFill
            chart.ChartArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 204, 153)

            chart.PlotArea.Interior.FillType = ExcelShapeFillType.SolidFill
            chart.PlotArea.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)
        Else
            chart.ChartArea.Interior.FillType = ExcelShapeFillType.GradientFill
            chart.ChartArea.Interior.GradientFillOptions.GradientColorsMode = ExcelGradientColors.Preset
            chart.ChartArea.Interior.GradientFillOptions.PresetGradientType = ExcelShapeFillPresetGradientType.Wheat

            chart.PlotArea.Interior.FillType = ExcelShapeFillType.TextureFill
            chart.PlotArea.Interior.TextureFillOptions.TextureType = ExcelShapeFillTextureType.Stationery
        End If

        ' set value axis title
        chart.ValueAxis.Title.Text = "Units sold"
        chart.ValueAxis.Title.Font.Size = 10
        chart.ValueAxis.Title.Font.Bold = True

        ' set value axis text style
        chart.ValueAxis.Font.Size = 8
        chart.ValueAxis.Font.Bold = False
        chart.ValueAxis.Font.Italic = True
        chart.ValueAxis.ShowVerticalTitleText()

        ' set category axis title
        chart.CategoryAxis.Title.Text = "Analyzed products"
        chart.CategoryAxis.Title.Font.Size = 10
        chart.CategoryAxis.Title.Font.Bold = True

        ' set category axis text style
        chart.CategoryAxis.Font.Size = 8
        chart.CategoryAxis.Font.Bold = False
        chart.CategoryAxis.Font.Italic = True

        ' set chart legend style
        chart.Legend.Interior.FillType = ExcelShapeFillType.SolidFill
        chart.Legend.Interior.SolidFillOptions.BackColor = Color.FromArgb(255, 255, 204)

        chart.Legend.Font.Size = 8
        chart.Legend.Font.Bold = True

        ' show a label with total number of units sold in a year
        'chart.Series.Item("Yearly Total").DataPoints.All.Label.ContainsValue = True
        'chart.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Size = 8
        'chart.Series.Item("Yearly Total").DataPoints.All.Label.LabelFormat.Font.Italic = True

        ' CREATE HYPERLINKS

        ' creat the merged range where to add the link to the second worksheet
        worksheet.Item(23, 1, 23, 3).Merge()
        ' create a named range used as target for the link to second worksheet
        Dim worksheetLinkTarget As ExcelNamedRange = workbook.NamedRanges.AddNamedRange(secondWorksheet.Item("A1"), "SecondWorksheet")
        Dim worksheetLinkSource As ExcelRange = worksheet.Item(23, 1)
        Dim secondWorksheetLink As ExcelHyperlink = worksheet.Hyperlinks.AddHyperlink(ExcelHyperlinkType.Range, worksheetLinkSource, "SecondWorksheet")
        secondWorksheetLink.Text = "Go To Next Worksheet"
        secondWorksheetLink.ToolTip = "Go To Next Worksheet"


        ' create the merged range where to add the link to the product website
        worksheet.Item(25, 1, 25, 3).Merge()
        ' create a hyperlink to the product website
        Dim websiteLinkSource As ExcelRange = worksheet.Item(25, 1)
        Dim websiteUrl As String = "http://www.html-to-pdf.net/excel-library.aspx"
        Dim websiteHyperlink As ExcelHyperlink = worksheet.Hyperlinks.AddHyperlink(ExcelHyperlinkType.Url, websiteLinkSource, websiteUrl)
        websiteHyperlink.Text = "Visit product website"
        websiteHyperlink.ToolTip = "Visit product website"

        ' evaluate workbook formulas
        workbook.UpdateFormulaResult()

        ' SAVE THE WORKBOOK

        ' Save the Excel document in the current HTTP response stream

        Dim outFileName As String = Nothing
        If workbookFormat = ExcelWorkbookFormat.Xls_2003 Then
            outFileName = "GettingStarted.xls"
        Else
            outFileName = "GettingStarted.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
            ' release the flag images
            If Not usFlagImg Is Nothing Then
                usFlagImg.Dispose()
            End If
            If Not frFlagImg Is Nothing Then
                frFlagImg.Dispose()
            End If
            If Not deFlagImg Is Nothing Then
                deFlagImg.Dispose()
            End If
            If Not esFlagImg Is Nothing Then
                esFlagImg.Dispose()
            End If
            If Not ruFlagImg Is Nothing Then
                ruFlagImg.Dispose()
            End If
            If Not itFlagImg Is Nothing Then
                itFlagImg.Dispose()
            End If
            If Not ptFlagImg Is Nothing Then
                ptFlagImg.Dispose()
            End If
            If Not nlFlagImg Is Nothing Then
                nlFlagImg.Dispose()
            End If
            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