ExpertXls Excel Library ASP.NET Web Forms demo · VB.NET

Charts

Adds charts to a worksheet, with series generated from a data range or added one by one, and a chart worksheet. Select the chart type and how the series are read from the data.

Select the format and press the button: the workbook is created on the server and downloaded by the browser.

Workbook format   
Chart type Series from the data range   

Create, edit and read Excel workbooks in ASP.NET: the workbooks are created on the server and sent to the browser. The code of each workbook is in App_Code.

VB.NET code of this sample

ChartsDemo.aspx.vb
Imports System.Data
Imports System.IO
Imports System.Web.UI
Imports ExpertXls.ExcelLib

''' <summary>
''' Charts: the workbook code is in App_Code\ChartsDemo.vb.
''' </summary>
Partial Class ChartsDemoPage
    Inherits Page

    ' AutoEventWireup="true" in the page directive connects Page_Load to the Load event
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        If Not IsPostBack Then
            For Each chartType As ExcelChartType In ChartsDemo.ChartTypes2D
                listChartType.Items.Add(chartType.ToString())
            Next
            For Each chartType As ExcelChartType In ChartsDemo.ChartTypes3D
                listChartType.Items.Add(chartType.ToString())
            Next
        End If
    End Sub

    Protected Sub buttonCreate_Click(sender As Object, e As EventArgs)
        Dim format As ExcelWorkbookFormat = If(radioXlsx.Checked, ExcelWorkbookFormat.Xlsx_2007, ExcelWorkbookFormat.Xls_2003)
        Dim workbookBytes As Byte()
        Try
            ' create the workbook in memory; the input files are in the folders of the web site
            Using stream As New MemoryStream()
                ChartsDemo.CreateWorkbook(format, Server.MapPath("~"), stream,
                    CType([Enum].Parse(GetType(ExcelChartType), listChartType.SelectedValue), ExcelChartType), radioSeriesByRows.Checked)
                workbookBytes = stream.ToArray()
            End Using
        Catch ex As Exception
            labelError.Text = "The workbook could not be created: " & Server.HtmlEncode(ex.Message)
            Return
        End Try
        WorkbookDownload.Send(Response, workbookBytes, ChartsDemo.OutputName & DemoLayout.GetExtension(format), format)
    End Sub

End Class