/// <summary> ///导出Excel /// </summary> protected void ImportExcel_Click(object sender, EventArgs e) { GridView tempGridView = new GridView(); tempGridView.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333"); tempGridView.PagerSettings.Mode = PagerButtons.Numeric; tempGridView.PagerSettings.Position = PagerPosition.Bottom; tempGridView.PagerSettings.PageButtonCount = 10; tempGridView.PagerStyle.BackColor = Color.LightBlue; tempGridView.AlternatingRowStyle.BackColor = Color.White; tempGridView.AlternatingRowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#284775"); tempGridView.EditRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#999999"); tempGridView.HeaderStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#5D7B9D"); tempGridView.HeaderStyle.Font.Bold = true; tempGridView.HeaderStyle.ForeColor = Color.White; tempGridView.PagerStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#284775"); tempGridView.PagerStyle.ForeColor = Color.White; tempGridView.RowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#F7F6F3"); tempGridView.RowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333"); tempGridView.SelectedRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#E2DED6"); tempGridView.SelectedRowStyle.Font.Bold = true; tempGridView.SelectedRowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333"); tempGridView.AutoGenerateColumns = false; Report.BindingData(tempGridView, user); GridViewExportUtil.Export("Coronary Heart Disease Platform", tempGridView); }
/// <summary> /// 导出Excel /// </summary> protected void ImportExcel_Click(object sender, EventArgs e) { GridView tempGridView = new GridView(); tempGridView.AutoGenerateColumns = false; Report.BindingData(tempGridView, user); GridViewExportUtil.Export("Coronary Heart Disease Platform", tempGridView); }
/// <summary> /// /// </summary> /// <param name="fileName"></param> /// <param name="gv"></param> public static void Export(string fileName, GridView gv) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = false; HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.AddHeader( "content-disposition", string.Format("attachment; filename={0}.xls", fileName)); HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); HttpContext.Current.Response.ContentType = "application/ms-excel"; HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=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 the header row to the table if (gv.HeaderRow != null) { GridViewExportUtil.PrepareControlForExport(gv.HeaderRow); table.Rows.Add(gv.HeaderRow); } // add each of the data rows to the table foreach (GridViewRow row in gv.Rows) { GridViewExportUtil.PrepareControlForExport(row); table.Rows.Add(row); } // add the footer row to the table if (gv.FooterRow != null) { GridViewExportUtil.PrepareControlForExport(gv.FooterRow); table.Rows.Add(gv.FooterRow); } // render the table into the htmlwriter table.RenderControl(htw); // render the htmlwriter into the response HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } }