Powered by SpreadsheetGear
<%@ Page Language="C#" EnableViewState="false" %>
<%@OutputCache Duration="86400" VaryByParam="*" %> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Excel Compatible Range Image using ASP.NET and C#</title>
<meta content="Simple Excel Compatible Range Image with ASP.NET, C# and SpreadsheetGear, a royalty free Microsoft Excel compatible spreadsheet component for the Microsoft .NET Framework featuring the fastest and most complete calculation engine available. Create, read, modify, calculate and write Microsoft Excel workbooks from your Microsoft .NET, ASP.NET, C#, VB.NET and Microsoft Office solutions. Integrates with Microsoft Visual Studio .NET, including IntelliSense and Dynamic Help." name="description" />
<script language="C#" runat="server">
     
    void Page_Load(Object sender, EventArgs e)
    {
        // Create a new workbook.
        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook();
        SpreadsheetGear.IWorksheet worksheet = workbook.Worksheets["Sheet1"];
        SpreadsheetGear.IWorksheetWindowInfo windowInfo = worksheet.WindowInfo;
        SpreadsheetGear.IRange cells = worksheet.Cells;

        // Load some sample data.
        SpreadsheetGear.IRange dataCells = cells["B2:E5"];
        dataCells.Value = new string[,] {
            { "$3,723", "$5,954", "$3,522", "$7,701" }, 
            { "$2,681", "$8,665", "$5,836", "$7,851" }, 
            { "$3,739", "$2,107", "$3,298", "$8,711" }, 
            { "$1,459", "$7,385", "$2,125", "$9,070" }, };

        // Create row headings for four quarters
        SpreadsheetGear.IRange quarterCells = cells["A2:A5"];
        quarterCells.Value = new string[,] { { "Q1" }, { "Q2" }, { "Q3" }, { "Q4" } };

        // Create column headings for four regions.
        SpreadsheetGear.IRange regionCells = cells["B1:E1"];
        regionCells.Value = new string[,] { { "North", "South", "East", "West" } };
        
        // Add formulas which use the SUM worksheet function to total sales by region.
        SpreadsheetGear.IRange totalCells = cells["B6:E6"];
        totalCells.Formula = "=SUM(B2:B5)";
        totalCells.NumberFormat = "$#,##0_);($#,##0)";

        // Add a chart to the worksheet's shape collection.
        // NOTE: Calculate the coordinates of the chart by converting row and column
        //       coordinates to points.  Use fractional row and colum values to get 
        //       coordinates anywhere in between row and column boundaries.
        double left = 5;
        double top = windowInfo.RowToPoints(6.0) + 5;
        double right = windowInfo.ColumnToPoints(5.0) - 5;
        double bottom = windowInfo.RowToPoints(16.0) - 5;
        SpreadsheetGear.Charts.IChart chart =
            worksheet.Shapes.AddChart(left, top, right - left, bottom - top).Chart;

        // Set the chart's source data range, plotting series in rows.
        chart.SetSourceData(totalCells, SpreadsheetGear.Charts.RowCol.Rows);

        // Set the chart type to a pie chart.
        chart.ChartType = SpreadsheetGear.Charts.ChartType.Pie;

        // Increase the first pie slice angle.
        chart.ChartGroups[0].FirstSliceAngle = 30;

        // Get a reference to the first and only series.
        SpreadsheetGear.Charts.ISeries series = chart.SeriesCollection[0];

        // Link category labels to the region cells.
        series.XValues = regionCells;

        // Add series data labels and change to show percentage only.
        series.HasDataLabels = true;
        series.DataLabels.ShowPercentage = true;
        series.DataLabels.ShowValue = false;

        // Explode the first pie slice (North Region).
        series.Points[0].Explosion = 25;
        
        // Add a chart title and change the font size.
        chart.HasTitle = true;
        chart.ChartTitle.Text = "Percentage of Sales by Region";
        chart.ChartTitle.Font.Size = 12;

        // Create the image class from a specified range.
        SpreadsheetGear.Drawing.Image image = 
            new SpreadsheetGear.Drawing.Image(cells["A1:E16"]);
        
        // Get a new bitmap image of the represented range.
        using (System.Drawing.Bitmap bitmap = image.GetBitmap())
        {
            // Stream the image to the client in PNG format.
            Response.Clear();
            Response.ContentType = "image/png";
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
            memoryStream.WriteTo(Response.OutputStream);
        }
    }
    
</script>
</head>
<body>
</body>
</html>