Exporting Excel is often used in many projects. I introduced the C# implementation of GridView to export Excel instance code, and I also left myself a study note.
using ; using ; using ; using ; using ; using ; namespace { /// <summary> /// Summary description for GridViewExport /// </summary> public class GridViewExport { public GridViewExport() { // // TODO: Add constructor logic here // } public static void Export(string fileName, GridView gv) { (); ( "content-disposition", ("attachment; filename={0}", fileName)); = "application/ms-excel"; // = "utf-8"; using (StringWriter sw = new StringWriter()) { using (HtmlTextWriter htw = new HtmlTextWriter(sw)) { // Create a form to contain the grid Table table = new Table(); = ; //Add solid lines between cells // add the header row to the table if ( != null) { PrepareControlForExport(); (); } // add each of the data rows to the table foreach (GridViewRow row in ) { PrepareControlForExport(row); (row); } // add the footer row to the table if ( != null) { PrepareControlForExport(); (); } // render the table into the htmlwriter (htw); // render the htmlwriter into the response (()); (); } } } /// <summary> /// Replace any of the contained controls with literals /// </summary> /// <param name="control"></param> private static void PrepareControlForExport(Control control) { for (int i = 0; i < ; i++) { Control current = [i]; if (current is LinkButton) { (current); (i, new LiteralControl((current as LinkButton).Text)); } else if (current is ImageButton) { (current); (i, new LiteralControl((current as ImageButton).AlternateText)); } else if (current is HyperLink) { (current); (i, new LiteralControl((current as HyperLink).Text)); } else if (current is DropDownList) { (current); (i, new LiteralControl((current as DropDownList).)); } else if (current is CheckBox) { (current); (i, new LiteralControl((current as CheckBox).Checked ? "True" : "False")); } if (()) { PrepareControlForExport(current); } } } /// <summary> /// Export Grid data (all) to Excel ///Available when all fields are of BoundField type /// If the field is TemplateField template type, data cannot be retrieved /// </summary> /// <param name="grid">grid ID</param> /// <param name="dt">Data source</param> /// <param name="excelFileName">The file name to export Excel</param> public static void OutputExcel(GridView grid, DataTable dt, string excelFileName) { Page page = (Page); (); string fileName = (.(excelFileName)); ("Content-Disposition", "attachment:filename=" + fileName + ".xls"); = "application/-excel"; = "utf-8"; StringBuilder s = new StringBuilder(); ("<HTML><HEAD><TITLE>" + fileName + "</TITLE><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>"); int count = ; ("<table border=1>"); ("<tr>"); for (int i = 0; i < count; i++) { if ([i].GetType() == typeof(BoundField)) ("<td>" + [i].HeaderText + "</td>"); //("<td>" + [i].HeaderText + "</td>"); } ("</tr>"); foreach (DataRow dr in ) { ("<tr>"); for (int n = 0; n < count; n++) { if ([n].Visible && [n].GetType() == typeof(BoundField)) ("<td>" + dr[((BoundField)[n]).DataField].ToString() + "</td>"); } ("</tr>"); } ("</table>"); ("</body></html>"); (("utf-8").GetBytes(())); (); } } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.