I want to introduce about Asp.net C# DataExport.
I am very insterested in data exporting and I find that many people pay attention on how to export data from GridView to Excel. Besides, it is common to export data to Word and HTML as well. Therefore, I show my method about exporting data to Excel, Word and HTML with C#. The data information in my example is about employees in a company, including Name, Phone Number, Salary and Haie Date.
ExportToExcel :
Create Method ExportToExcel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public void ExportToExcel(DataTable dt) { if (dt.Rows.Count > 0) { string filename = "DownloadMobileNoExcel.xls"; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); DataGrid dgGrid = new DataGrid(); dgGrid.DataSource = dt; dgGrid.DataBind(); //Get the HTML for the control. dgGrid.RenderControl(hw); //Write the HTML back to the browser. //Response.ContentType = application/vnd.ms-excel; Response.ContentType = "application/vnd.ms-excel"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ""); this.EnableViewState = false; Response.Write(tw.ToString()); Response.End(); } } |
Then Call The method:
1 2 3 4 5 |
protected void btnExport_Click(Object sender, EventArgs e) { DataSet dsReportData = new DataSet(); ExportToExcel(dsReportData.Tables[1]); } |
Thank you for reading.
Howdy very cool website!! Man .. Excellent ..
Superb .. I’ll bookmark your website and take the feeds additionally?
I’m glad to find so many useful info right here in the submit,
we want develop more strategies on this regard,
thanks for sharing. . . . . .