This is a migrated thread and some comments may be shown as answers.

Export HTML Table Above Grid in Excel

2 Answers 264 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rob
Top achievements
Rank 1
Rob asked on 14 Dec 2011, 09:54 PM
I have a RadGrid that I want to export to Excel, but above the exported grid I would like to add an addition HTML table.  After adding the table, the formatting of the export is unaligned.  I'm guessing I don't have my HTML tags correct.  All I want to accomplish is adding an HTML table above the exported grid and the table & the grid should fill 100% of the width of the screen.  Please help.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadScriptManager ID="mgrJS" runat="server">
        </telerik:RadScriptManager>
        <telerik:radajaxmanager ID="mgrAjax" runat="server">
        </telerik:radajaxmanager>       
        <telerik:RadGrid runat="server" ID="dg" Skin="WebBlue" AllowSorting="true" ></telerik:RadGrid>
        <asp:Button ID="btn" runat="server" text="Export" />
    </div>
    </form>
</body>
</html>
Imports System.Data
  
Partial Class _Default
    Inherits System.Web.UI.Page
  
    Private Function CreateDataTable() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("123", GetType(String))
        dt.Columns.Add("456", GetType(String))
        dt.Columns.Add("789", GetType(String))
        Dim r As DataRow = dt.NewRow
        r(0) = "abc"
        r(1) = "def"
        r(2) = "ghi"
        dt.Rows.Add(r)
        Return dt
    End Function
    
    Protected Sub dg_NeedDataSource(ByVal source As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles dg.NeedDataSource
        dg.DataSource = CreateDataTable()
    End Sub
  
    Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Dim header As New StringBuilder("<table style='width:100%'><tr><td></td></tr><tr><td align='center'>The Title</td></tr><tr><td></td></tr><tr><td><table style='width:100%'>")
        dg.MasterTableView.Caption = header.ToString
        dg.ExportSettings.ExportOnlyData = True
        dg.ExportSettings.OpenInNewWindow = True
        dg.ExportSettings.IgnorePaging = True
        dg.MasterTableView.ExportToExcel()
        header.Append("</table></td></tr></table>")
    End Sub
End Class

2 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 16 Dec 2011, 06:19 PM
Dudeman:

The problem is that when you add HTML as part of the string that you pass to the "dg.MasterTableView.Caption" setting, you are corrupting the ordering of the HTML tags automatically produced by the export function. In order to have MSExcel interpret and correctly render the RadGrid's export, there cannot be any added HTML tags in the stream.

This works for me:
Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
 
    Dim header As New StringBuilder("The Title")
    dg.MasterTableView.Caption = header.ToString
    dg.ExportSettings.ExportOnlyData = True
    dg.ExportSettings.OpenInNewWindow = True
    dg.ExportSettings.IgnorePaging = True
    dg.MasterTableView.ExportToExcel()
 
End Sub

Hope this helps.
0
Mira
Telerik team
answered on 19 Dec 2011, 01:36 PM
Hello guys,

Jumpstart is indeed right.
You can have only inline elements - bold, italic, strong, etc. in the caption of the exported grid.

I hope this helps.

Greetings,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Rob
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Mira
Telerik team
Share this question
or