Powered by SpreadsheetGear
<%@ Page Language="VB" 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">
<title>Simple Excel Compatible Range Image using ASP.NET and VB.NET</title>
<meta content="Simple Excel Compatible Range Image with ASP.NET, Visual Basic .NET 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="VB" runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        ' Create a new workbook.
        Dim workbook As SpreadsheetGear.IWorkbook = SpreadsheetGear.Factory.GetWorkbook()
        Dim worksheet As SpreadsheetGear.IWorksheet = workbook.Worksheets("Sheet1")
        Dim windowInfo As SpreadsheetGear.IWorksheetWindowInfo = worksheet.WindowInfo
        Dim cells As SpreadsheetGear.IRange = worksheet.Cells

        ' Load some sample data.
        Dim dataCells As SpreadsheetGear.IRange = 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
        Dim quarterCells As SpreadsheetGear.IRange = cells("A2:A5")
        quarterCells.Value = New String(,) {{"Q1"}, {"Q2"}, {"Q3"}, {"Q4"}}

        ' Create column headings for four regions.
        Dim regionCells As SpreadsheetGear.IRange = cells("B1:E1")
        regionCells.Value = New String(,) {{"North", "South", "East", "West"}}

        ' Add formulas which use the SUM worksheet function to total sales by region.
        Dim totalCells As SpreadsheetGear.IRange = 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.
        Dim left As Double = 5
        Dim top As Double = windowInfo.RowToPoints(6.0) + 5
        Dim right As Double = windowInfo.ColumnToPoints(5.0) - 5
        Dim bottom As Double = windowInfo.RowToPoints(16.0) - 5
        Dim chart As SpreadsheetGear.Charts.IChart = _
            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.
        Dim series As SpreadsheetGear.Charts.ISeries = 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.
        Dim image As New SpreadsheetGear.Drawing.Image(cells("A1:E16"))

        ' Get a new bitmap image of the represented range.
        Dim bitmap As System.Drawing.Bitmap = image.GetBitmap()
            ' Stream the image to the client in PNG format.
            Response.ContentType = "image/png"
            Dim memoryStream As New System.IO.MemoryStream()
            bitmap.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png)
        End Try
    End Sub 'Page_Load