ExpertXls Excel Library ASP.NET Web Forms demo · C#

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.

C# code of this sample

ChartsDemo.aspx.cs
using System;
using System.Data;
using System.IO;
using System.Web.UI;

using ExpertXls.ExcelLib;
using ExcelLibraryDemos;

/// <summary>
/// Charts: the workbook code is in App_Code\ChartsDemo.cs.
/// </summary>
public partial class ChartsDemoPage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            foreach (ExcelChartType chartType in ChartsDemo.ChartTypes2D)
                listChartType.Items.Add(chartType.ToString());
            foreach (ExcelChartType chartType in ChartsDemo.ChartTypes3D)
                listChartType.Items.Add(chartType.ToString());
        }
    }

    protected void buttonCreate_Click(object sender, EventArgs e)
    {
        ExcelWorkbookFormat format = radioXlsx.Checked ? ExcelWorkbookFormat.Xlsx_2007 : ExcelWorkbookFormat.Xls_2003;
        byte[] workbookBytes;
        try
        {
            // create the workbook in memory; the input files are in the folders of the web site
            using (MemoryStream stream = new MemoryStream())
            {
                ChartsDemo.CreateWorkbook(format, Server.MapPath("~"), stream,
                    (ExcelChartType)Enum.Parse(typeof(ExcelChartType), listChartType.SelectedValue), radioSeriesByRows.Checked);
                workbookBytes = stream.ToArray();
            }
        }
        catch (Exception ex)
        {
            labelError.Text = "The workbook could not be created: " + Server.HtmlEncode(ex.Message);
            return;
        }
        WorkbookDownload.Send(Response, workbookBytes, ChartsDemo.OutputName + DemoLayout.GetExtension(format), format);
    }
}