Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
95
downloading excel file after export Data in it
posted

Hey guys,

i have a WebDataGrid and exported the Data displayed into an Excel file,

so how can i now offer this File on my Server for the User who exported this, to download it.

so what i mean is that after exporting the data, there should automatically start the download of this file.

my Export do i with following Code:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        WebDataGrid1.Behaviors.Paging.PageIndex = 0
        WebDataGrid1.Behaviors.Paging.Enabled = False

        Dim lWorkbook As New Workbook
        Dim lWorksheet = lWorkbook.Worksheets.Add("myTable")

        Dim iRow As Integer = 0
        Dim lCounter As Long = 0

        For Each GridRecord In WebDataGrid1.Rows
            For iRow = 0 To WebDataGrid1.Columns.Count - 1
                lWorksheet.Rows(lCounter).Cells(iRow).Value() = GridRecord.Items(iRow).Text
            Next
            lCounter = lCounter + 1
        Next

        MsgBox("Excel saved to C:\t.xls")
        lWorkbook.Save("C:\t.xls")

        WebDataGrid1.Behaviors.Paging.Enabled = True
        WebDataGrid1.Behaviors.Paging.PageIndex = 300

        Exit Sub

    End Sub

 

Hope somebody can help me.

  • 4960
    Suggested Answer
    posted

    When you call Save("C:\t.xls") instead of saving it to your C: drive root folder (not a good place), you could instead save it to a sub-folder of your Web site, for instance Save(Server.MapPath("~/excel_downloads/t.xls")) to save to C:\inetpub\wwwroot\my_app\excel_downloads\t.xls. 

    Next, do a Response.Redirect to "~/excel_downloads/t.xls" so that instead of running your page, ASP.NET will redirect the user's HTTP request to download ~/excel_downloads/t.xls.

    However, rather than saving it onto your Web server and redirecting the user's request to download the .XLS file that you have saved (and then having to deal with additional complexities like security and cleaning-up old files) you can use our WebExcelExporter control that's new in 2010 Volume 2,

    http://ko.infragistics.com/dotnet/netadvantage/aspnet/excel-exporter.aspx

    It will save you from all of this trouble.