Wednesday, July 08, 2009

Exporting GridView or ListView to Excel

A handy and simple way of exporting the content of your GridView og ListView to Excel.

protected void ExportExcelButton_OnClick(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=Contacts.xls");
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";
    EnableViewState = false;
    var stringWriter = new StringWriter();
    var htmlTextWriter = new HtmlTextWriter(stringWriter);
    userListView.RenderControl(htmlTextWriter);
    Response.Write(stringWriter.ToString());
    Response.End();
}

3 comments:

  1. and if i wish to use one row for each field of listview?

    ReplyDelete
  2. Then you have to make one row for each field in listview, then export. Not sure if I understood your question?

    ReplyDelete
  3. A gambler could participate within the game itself while betting on its consequence , or he may be be} prevented from any energetic participation in an occasion in which he has a stake . Some games are uninteresting or practically meaningless with out the accompanying betting exercise and are rarely performed until wagering occurs . In different games betting 1xbet just isn't intrinsically part of of} the game, and the association is merely standard and not essential to the efficiency of the game itself . Some activities of very giant scale usually require business and professional organizations to present and keep them effectively. Gov. Doug Burgum rejected a plea Wednesday by North Dakota’s American Indian tribes to provide them unique rights to host internet playing and sports activities betting end result of|as a result of} it isn't allowed under state law. – Gov. Doug Burgum rejected a plea Wednesday by North Dakota’s American Indian tribes to provide them unique rights to host internet playing and sports activities betting end result of|as a result of} it isn’t allowed under state law.

    ReplyDelete