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

Export to Excel from aspLinkButtons

3 Answers 20 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kurt Kluth
Top achievements
Rank 1
Kurt Kluth asked on 24 Jul 2014, 03:07 PM
What I would like to do is have two LinkButton's that will both download the RadGrid into Excel format.  Previously I did have the "ExportCSV" and "ExportToExcel" menu items turned on and the code worked just fine however I needed the names of the downloads to be something else.  One version will be formatted like the RadGrid results and the other will be formatted slightly different (see below).  I was hoping that with option 1 that I could just tie into the RadGrid's ability to export to .XLS but it appears as if I don't have something right.  Can you offer assistance? 

File option 2 I need the data formatted differently would I have to requery the database or can I just manipulate the RadGrid results?

<telerik:RadGrid ID="GridContacts" runat="server" AllowPaging="True"
    AutoGenerateColumns="False" CellSpacing="0" 
    GridLines="None" PageSize="20"
    OnNeedDataSource="GridContacts_NeedDataSource" Visible="true">
    <GroupingSettings CaseSensitive="False" ShowUnGroupButton="false" />
    <ExportSettings IgnorePaging="True" OpenInNewWindow="True"
        ExportOnlyData="True" FileName="MarketingEmailList">
        <Excel AutoFitImages="True" Format="ExcelML" />
        <Csv ColumnDelimiter="VerticalBar" EncloseDataWithQuotes="False" />
    </ExportSettings>
    <MasterTableView CommandItemDisplay="Top" 
        IsFilterItemExpanded="true">
        <CommandItemTemplate>
            <asp:LinkButton ID="btnExportPrn" runat="server" CommandName="ExportPrinter" Visible="true"><img style="border:0px;vertical-align:middle;" alt="" src="/Images/DownloadPrinter.png"/>Printer Download</asp:LinkButton>  
            <asp:LinkButton ID="btnExportMarket" runat="server" CommandName="ExportMarketing"><img style="border:0px;vertical-align:middle;" alt="" src="/Images/DownloadMarket.png"/>Marketing Download</asp:LinkButton>  
        </CommandItemTemplate>

Code behind and while running it I see the CommandName is either "ExportPrinter" or "ExportMarketing" and to see if I could get it to work I just invoked the calls to "ConfigureCSVExport" and "ConfigureExcelExport"
Protected Sub RadGrid1_ItemCommand(sender As Object, e As Telerik.Web.UI.GridCommandEventArgs) Handles GridContacts.ItemCommand
       If e.CommandName = "ExportPrinter" Then
           ConfigureCSVExport()
       ElseIf e.CommandName = "ExportMarketing" Then
           ConfigureExcelExport()
       End If
       'If e.CommandName = Telerik.Web.UI.RadGrid.ExportToCsvCommandName Then
 
 
       'ElseIf e.CommandName = Telerik.Web.UI.RadGrid.ExportToExcelCommandName Then
 
       'End If
   End Sub

Currently the RadGrid displays the data as such

Company Name | Address info | CEO | CFO | Marketing Mgr | Operations Mgr
ABC Company | Main St, my town USA | Bob Smith | Susie Jones | Mr. X

For the "ExportMarketing" this format would work just fine but for the "ExportPrinter" I need the data slightly changed and wondering the best way of going about this.  Need the data to appear like

Bob Smith | ABC Company | Main St, My town USA
Susie Jones | ABC Company | Main St, My town USA
Mr. X | ABC Company | Main St, My town USA























3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 25 Jul 2014, 05:00 AM
Hi Kurt Kluth,

I guess you want to hide the headers during Export for one of the LinkButton click. Please try the following code snippet:

VB:
Private isExportMarketing As Boolean = False
Private Sub RadGrid1_PreRender(sender As Object, e As EventArgs)
    If isExportMarketing Then
        For Each column As GridColumn In RadGrid1.Columns
            column.HeaderText = String.Empty
        Next
        RadGrid1.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.None
        RadGrid1.Rebind()
    End If
End Sub
Private Sub RadGrid1_ItemCommand(sender As Object, e As GridCommandEventArgs)
    If e.CommandName = "ExportPrinter" Then
        RadGrid1.ExportSettings.ExportOnlyData = True
        RadGrid1.ExportSettings.FileName = "ExportPrinter"
        RadGrid1.MasterTableView.ExportToExcel()
    ElseIf e.CommandName = "ExportMarketing" Then
        isExportMarketing = True
        RadGrid1.ExportSettings.ExportOnlyData = True
        RadGrid1.ExportSettings.FileName = "ExportMarketing"
        RadGrid1.MasterTableView.ExportToExcel()
    End If
End Sub

Thanks,
Princy
0
Kurt Kluth
Top achievements
Rank 1
answered on 01 Aug 2014, 04:22 PM

I was able to figure out my problem with exporting but how do I manipulate the data before exporting?  What I mean? Here is a list of my columns: CHARTER #, CU NAME, MAILING ADDRESS, CITY, STATE, ZIP CODE, CEO, CFO, OPERATING MGR, MARKETING MGR, TOTAL ASSETS

I would like to then take this data and loop through the radgrid results and put it in a different layout and rebind it to just the "Printer" export.
Below is an example of what I am thinking I need to do to accomplish this.  I was trying to avoid making an additional call to the database and writing a new stored procedure.  Is there anyway of putting the RadGrid into a DataTable and loop through that before export?

Loop Each line of RadGrid.Results
If CEO <> "" then
   writeline CEO, CUName, Mailing Address, City, State, Zip
end if
 
IF CFO <> "" then
   writeline CFO, CUName, Mailing Address, City, State, Zip
end if
End loop
etc...
0
Princy
Top achievements
Rank 1
answered on 04 Aug 2014, 05:00 AM
Hi Kurt Kluth,

You can add a DataTable with your desired result on the Export button click, and bind to grid on Export. This wont change the current grid structure. Check the below sample code, you can use your logic to bind the data to grid.

VB:
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    Dim dt As New DataTable()
    dt.Columns.Add("Name", GetType(String))
    dt.Columns.Add("Date", GetType(DateTime))
    dt.Rows.Add("Name1", DateTime.Now.AddDays(8))
    dt.Rows.Add("Name2", DateTime.Now.AddDays(-2))
    dt.Rows.Add("Name3", DateTime.Now.AddDays(3))
    dt.Rows.Add("Name1", DateTime.Now.AddDays(10))
    dt.Rows.Add("Name8", DateTime.Now.AddDays(2))
    dt.Rows.Add("Name3", DateTime.Today)
    RadGrid1.DataSource = dt
    RadGrid1.Rebind()
    RadGrid1.MasterTableView.ExportToExcel()
End Sub

Thanks,
Princy
Tags
Grid
Asked by
Kurt Kluth
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Kurt Kluth
Top achievements
Rank 1
Share this question
or