简体   繁体   中英

Export All Rows of Paged ListView to Excel

In ASP.NET 4.0 webforms, I'm trying to export a paged ListView control to an Excel file by un-paging the ListView's (trucks) datasource:

dsTrucks.EnablePaging = false;

For a non-paged ListView control, I can get it to work.

Here's the attempt to "un-page" and then export the ListView control:

    // Nuke the current page.
    Response.Clear();

    // Setup the response header.
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=Trucks.xls");
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    // Turn off view state.
    this.EnableViewState = false;

    // Create a string writer.
    var stringWriter = new StringWriter();

    // Create an HTML text writer and give it a string writer to use.
    var htmlTextWriter = new HtmlTextWriter(stringWriter);

    // Disable paging so we get all rows.
    dsTrucks.EnablePaging = false;

    // Render the list view control into the HTML text writer.
    listViewTrucks.DataBind();
    listViewTrucks.RenderControl(htmlTextWriter);

    // Grab the final HTML out of the string writer.
    string output = stringWriter.ToString();

    // Write the HTML output to the response, which in this case, is an Excel file.
    Response.Write(output);
    Response.End();

There's no error but the output in the Excel file is still just one page of the ListView control instead of all rows.

Any ideas on where to start to get this to work?

Thanks, Adam

Just a guess, but it might be that EnablePaging works only on the control's OnInit() and it is too late by the time you call it from your code.

Perhaps you could you set the PageSize some MAXINT value and force all results into one single page?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM