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

Export to CSV does nothing?

7 Answers 315 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stefan
Top achievements
Rank 2
Stefan asked on 18 Sep 2013, 03:04 PM
I have a grid and a telerik button.  When I click the button I want a Save As prompt dialog to appear so the user can change the file name and save the file locally to whichever destination they choose.  But when I click on my button nothing happens?  I am testing this through localhost\ does this function not work unless its run from the actual server? Or should it behave the same even when running from localhost? Here is a copy of my grid.

                                                  <telerik:RadAjaxPanel ID="pnlUpload" runat="server" height="580px" width="980px" LoadingPanelID="lpnUpload">  

  <telerik:RadButton ID="cmdExportPayrolFile" runat="server" Enabled="False" Skin="Metro" Text="Export Payroll File">
                                                    </telerik:RadButton>

<telerik:RadGrid ID="dgExportReport" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Skin="Sitefinity" Width="100%">
                                            <ExportSettings>
                                                <Csv ColumnDelimiter="Comma"
                                                     RowDelimiter="NewLine"
                                                     EncloseDataWithQuotes="False"
                                                     FileExtension ="txt"
                                                 />
                                            </ExportSettings>
                                            <MasterTableView PageSize="15">
                                                <Columns>
                                                    <telerik:GridBoundColumn DataField="EmplID" FilterControlAltText="" HeaderText="Emp Id" UniqueName="dgEmplID">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="FullName" FilterControlAltText="" HeaderText="Employee" UniqueName="dgLastName">
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="CostCenterID" FilterControlAltText="" HeaderText="Cost Center" UniqueName="dgCostCenter">
                                                        <ItemStyle HorizontalAlign="Right" />
                                                    </telerik:GridBoundColumn>
                                                    <telerik:GridBoundColumn DataField="Total" DataFormatString="{0:$###,##0.00}" FilterControlAltText="" HeaderText="Total" UniqueName="dgTotal">
                                                        <ItemStyle HorizontalAlign="Right" />
                                                    </telerik:GridBoundColumn>
                                                </Columns>
                                                <PagerStyle PageSizeControlType="RadComboBox" />
                                            </MasterTableView>
                                            <PagerStyle PageSizeControlType="RadComboBox" />
                                            <FilterMenu EnableImageSprites="False">
                                            </FilterMenu>
                                        </telerik:RadGrid>
        </telerik:RadAjaxPanel>

========================
VB CODE BEHIND
=========================

    Protected Sub cmdExportPayrolFile_Click(sender As Object, e As EventArgs) Handles cmdExportPayrolFile.Click

        dgExportReport.MasterTableView.ExportToCSV()

    End Sub


Am I missing something in the grid properties? Does this feature only work if the page is run from the server and NOT localhost? I am populating the grid using advanced binding upon a combo box select index changed.

Thanks


7 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 19 Sep 2013, 03:21 AM
Hi Stefan,

Please add the below code snippet to your code to Export to CSV on button click.

ASPX:
<telerik:RadAjaxPanel ID="pnlUpload" runat="server" ClientEvents-OnRequestStart="onRequestStart">
    <telerik:RadButton ID="cmdExportPayrolFile" runat="server" Skin="Metro" Text="Export Payroll File"       OnClick="cmdExportPayrolFile_Click">
    </telerik:RadButton>
    <telerik:RadGrid ID="dgExportReport" runat="server" >
    . . . . .
    </telerik:RadGrid>
</telerik:RadAjaxPanel>

JS:
<script type="text/javascript">
    function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("cmdExportPayrolFile") >= 0)  // ID of Button to Set Ajax false on the Button Click
            args.set_enableAjax(false);
    }   
</script>

Thanks,
Princy
0
Stefan
Top achievements
Rank 2
answered on 20 Sep 2013, 05:09 PM
Hey Princy,

Thanks for this.  But I have paging enabled so I am only getting the first page of the grid.  I need all of the data.  Plus this just loads the text into a browser window.  I need a "Save As" box to appear so the client can save the file locally.  Also I would like to be able to turn certain fields off during the export as I only need some of the fields (EmplID, CostCenterID and Total)  not all of them.

<telerik:RadGrid ID="dgExportReport" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellSpacing="0" GridLines="None" Skin="Sitefinity" Width="100%">
    <ExportSettings
        FileName ="DoesThisWork.txt"
        Csv-ColumnDelimiter ="comma"
        Csv-RowDelimiter="NewLine"
        Csv-EncloseDataWithQuotes="False"
        Csv-FileExtension ="txt">
    </ExportSettings>
    <MasterTableView PageSize="15">
        <Columns>
            <telerik:GridBoundColumn DataField="EmplID" FilterControlAltText="" HeaderText="Emp Id" UniqueName="dgEmplID">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="FullName" FilterControlAltText="" HeaderText="Employee" UniqueName="dgLastName">
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="CostCenterID" FilterControlAltText="" HeaderText="Cost Center" UniqueName="dgCostCenter">
                <ItemStyle HorizontalAlign="Right" />
            </telerik:GridBoundColumn>
            <telerik:GridBoundColumn DataField="Total" DataFormatString="{0:$###,##0.00}" FilterControlAltText="" HeaderText="Total" UniqueName="dgTotal">
                <ItemStyle HorizontalAlign="Right" />
            </telerik:GridBoundColumn>
        </Columns>
        <PagerStyle PageSizeControlType="RadComboBox" />
    </MasterTableView>
    <PagerStyle PageSizeControlType="RadComboBox" />
    <FilterMenu EnableImageSprites="False">
    </FilterMenu>
</telerik:RadGrid>
0
Princy
Top achievements
Rank 2
answered on 21 Sep 2013, 04:20 AM
Hi Stefan,

In order to export the entire data,please set IgnorePaging property of the ExportSettings to true.To hide a column on export you can set it's visibility to false.Please try the following code snippet,which shows confirm box to export.

ASPX:
<telerik:RadWindowManager ID="RadWindowManager1" runat="server">
</telerik:RadWindowManager>
<telerik:RadAjaxPanel ID="pnlUpload" runat="server" ClientEvents-OnRequestStart="onRequestStart">
    <telerik:RadButton ID="cmdExportPayrolFile" runat="server" Skin="Metro" Text="Export Payroll File"
        OnClick="cmdExportPayrolFile_Click" OnClientClicking="OnClientClicking">
    </telerik:RadButton>
    <telerik:RadGrid ID="dgExportReport" runat="server" >
       . . . .
    </telerik:RadGrid>
</telerik:RadAjaxPanel>

C#:
protected void cmdExportPayrolFile_Click(object sender, EventArgs e)
   {   
       dgExportReport.MasterTableView.GetColumn("CoulmnUniqueName").Visible = false; // To hide a column on export
       dgExportReport.ExportSettings.IgnorePaging = true; //To export all the pages
       dgExportReport.MasterTableView.ExportToCSV();
   }

JS:
<script type="text/javascript">
function onRequestStart(sender, args) {
 if (args.get_eventTarget().indexOf("cmdExportPayrolFile") >= 0)  // ID of Button to Set Ajax false on the Button Click
            args.set_enableAjax(false);
}
function OnClientClicking(sender, args) {
    var callBackFunction = Function.createDelegate(sender, function (argument) {
        if (argument) {
            this.click();
        }
    });
    var text = "Are you sure you want to Export the Grid?";
    radconfirm(text, callBackFunction, 300, 100, null, "Title");
    args.set_cancel(true);
}   
</script>

Thanks,
Princy

0
Stefan
Top achievements
Rank 2
answered on 25 Sep 2013, 02:35 PM
Hey Princy,

Thanks again.  This helps.  Only issue I have now is I set Export Only Data = true but I am still getting the column headers on the first row?

<ExportSettings
    FileName ="yea_ok"
    IgnorePaging =" True"
    ExportOnlyData=" True"
    Csv-ColumnDelimiter ="comma"
    Csv-RowDelimiter="NewLine"
    Csv-EncloseDataWithQuotes="False"
    Csv-FileExtension ="txt"
    OpenInNewWindow=" True">
    <Csv EncloseDataWithQuotes="False" />
</ExportSettings>


With dgExportReport
    .ExportSettings.FileName = "ippPayRoll_" & cboReportPeriod.Text & "_" & cboReportYear.Text
    .ExportSettings.ExportOnlyData = True
    .MasterTableView.GetColumn("dgEmplID").Visible = True
    .MasterTableView.GetColumn("dgTotal").Visible = False
    .MasterTableView.ExportToCSV()
End With


0
Princy
Top achievements
Rank 2
answered on 26 Sep 2013, 04:19 AM
Hi Stefan,

I have tried your code and it works fine at my end.Please try, From Q2 2013 a new property named Exportable has been introduced. This property allows you to choose whether a certain column should be included in the exported file or not. By setting this property to false the related column will be excluded from the exported file. Its default value is true.

ASPX:
<telerik:GridBoundColumn Exportable="false" DataField="Name" HeaderText="Name" UniqueName="Name"  />

OR can you try setting the following code to hide a column.

VB:
_with1.MasterTableView.Columns.FindByUniqueName("ShipCity").Visible = False

Thanks,
Princy
0
Stefan
Top achievements
Rank 2
answered on 26 Sep 2013, 02:02 PM
Hey Princy, I have the right columns exporting. It is that the first row of my file has the name of the columns. That is what I don't want. Header 1, header 2, , , Data, data Data, data I don't need the headers to show in my file and I have set exportdataonly = true but the still export? Thanks
0
Princy
Top achievements
Rank 2
answered on 27 Sep 2013, 04:18 AM
Hi Stefan,

Please try the following code snippet to hide the headers when exporting.

VB:
Protected Sub cmdExportPayrolFile_Click(sender As Object, e As EventArgs)
    Dim _with1 = dgExportReport
    _with1.ShowHeader = False
    _with1.Rebind()
    _with1.MasterTableView.ExportToCSV()
End Sub

Thanks,
Princy
Tags
Grid
Asked by
Stefan
Top achievements
Rank 2
Answers by
Princy
Top achievements
Rank 2
Stefan
Top achievements
Rank 2
Share this question
or