New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Export Grid to a Preformatted Excel File



Product IDProduct NameUnit PriceUnits In StockCategoryDiscontinued
Page size:
 62 items in 7 pages
15Genen Shouyu15.5039Condiments
17Alice Mutton39.000Meat/Poultry
18Carnarvon Tigers62.5042Seafood
19Teatime Chocolate Biscuits9.2025Confections
20Sir Rodney's Marmalade81.0040Confections
21Sir Rodney's Scones10.003Confections
22Gustaf's Knäckebröd21.00104Grains/Cereals
23Tunnbröd9.0061Grains/Cereals
24Guaraná Fantástica4.5020Beverages
25NuNuCa Nuß-Nougat-Creme14.0076Confections

Demo Buttons

  • Export To Preformatted Excel - Shows the new functionality in action, Grid exported to Workbook and merged with a Preformatted Template Excel file. To check the empty template file, use the 'Download Template' button
  • Export to Excel - Shows the default out-of-the-box export functionality of the Grid. The same as calling .ExportToExcel() or clicking the built-in 'Export To Excel' button
  • Download Template - Downloads the empty Excel file that has pre-defined formulas, sheets and styles. This is the empty template file where the Grid data will be populated when using 'Export To Preformatted Excel' button

Description

This is a practical demonstration of the improved GenerateXlsxWorkbook() method provided by the grid. It can be utilized further for different scenarios where you need the Excel XLSX output even without actually exporting anything.

The new overloads for the GenerateXlsxOutput<T>() method, introduced in R1 2021, allow the method to return a boxed object which you can cast to a Workbook, byte[] or a string depending on the generic T type you have passed.

C#
// alias for the using used for shorter definition of the Workbook type
using xlsx = Telerik.Windows.Documents.Spreadsheet.Model;

// various ways to use the GenerateXlsxOutput() method
byte[] outputAsByteArray =  RadGrid1.MasterTableView.GenerateXlsxOutput<byte[]>() as byte[];
xlsx.Workbook outputAsWorkbook = RadGrid1.MasterTableView.GenerateXlsxOutput<xlsx.Workbook>() as xlsx.Workbook;
string outputAsString = RadGrid1.MasterTableView.GenerateXlsxOutput<string>() as string;
string outputAsString2 = RadGrid1.MasterTableView.GenerateXlsxOutput();
  
VB
' alias for the Import used for shorter definition of the Workbook type
Imports xlsx = Telerik.Windows.Documents.Spreadsheet.Model

' various ways to use the GenerateXlsxOutput() method
Dim outputAsByteArray As Byte() = TryCast(RadGrid1.MasterTableView.GenerateXlsxOutput(Of Byte())(), Byte())
Dim outputAsWorkbook As xlsx.Workbook = TryCast(RadGrid1.MasterTableView.GenerateXlsxOutput(Of xlsx.Workbook)(), xlsx.Workbook)
Dim outputAsString As String = TryCast(RadGrid1.MasterTableView.GenerateXlsxOutput(Of String)(), String)
Dim outputAsString2 As String = RadGrid1.MasterTableView.GenerateXlsxOutput()

  • DefaultVB.aspx
  • DefaultVB.aspx.vb
<%@ Page Language="vb" AutoEventWireup="false" Inherits="Telerik.GridExamplesCSharp.Exporting.ExportToExcel.DefaultVB"CodeFile="DefaultVB.aspx.vb"  %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<%@ Register TagPrefix="qsf" Namespace="Telerik.QuickStart" %>
<!DOCTYPE html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <div class="demo-container no-bg">
        <style>
            .demo-container .info-button + .RadButton {
                margin-left: 40px
            }

            .demo-container .info-button {
                font-size: 1.4em;
                color: #0487c4;
                position: absolute;
                top: 20%
            }
        </style>

        <telerik:RadToolTipManager runat="server" OffsetX="-100" RelativeTo="Element" AutoTooltipify="true" Width="300px">
        </telerik:RadToolTipManager>
        <div style="position: relative; vertical-align: middle">
            <telerik:RadButton RenderMode="Lightweight" ID="RadButton1" runat="server" Text="Export To Preformatted Excel"
                OnClick="RadButton1_Click" Primary="true">
                <Icon PrimaryIconCssClass="p-i-file-xlsx"></Icon>
            </telerik:RadButton>
            <span class="p-icon p-i-help info-button" title="The new functionality in action, Grid exported to Workbook and merged with a Preformatted Template Excel file. To check the empty template file, use the 'Download Template' button"></span>

            <telerik:RadButton RenderMode="Lightweight" ID="RadButton2" runat="server" Text="Export to Excel"
                OnClick="RadButton2_Click">
                <Icon PrimaryIconCssClass="p-i-file-xlsx"></Icon>
            </telerik:RadButton>
            <span class="p-icon p-i-help info-button" title="The default out-of-the-box export functionality of the Grid. The same as calling .ExportToExcel() or clicking the built-in Export To Excel button"></span>

            <telerik:RadButton RenderMode="Lightweight" ID="RadButton3" runat="server" Text="Download Template"
                OnClick="RadButton3_Click">
                <Icon PrimaryIconCssClass="p-i-download"></Icon>
            </telerik:RadButton>
            <span class="p-icon p-i-help info-button" style="margin-left: 4px;" title="Downloads the empty Excel file that has pre-defined formulas, sheets and styles. This is the empty template file where the Grid data will be populated when using 'Export To Preformatted Excel' button"></span>
        </div>
        <br />
        <br />

        <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" CellSpacing="0"
            DataSourceID="SqlDataSource1" GridLines="None"
            OnColumnCreated="RadGrids_ColumnCreated">
            <ExportSettings IgnorePaging="true" ExportOnlyData="true">
                <Excel Format="Xlsx" />
            </ExportSettings>
            <MasterTableView DataSourceID="SqlDataSource1">
            </MasterTableView>
        </telerik:RadGrid>
    </div>

    <asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT p.[ProductID], p.[ProductName], p.[UnitPrice], p.[UnitsInStock], c.[CategoryName] as [Category], p.[Discontinued] FROM [Products] p
JOIN [Categories] c ON c.CategoryID = p.CategoryID"></asp:SqlDataSource>
    </form>
</body>
</html>

Support & Learning Resources

Find Assistance