GridViewToExcel
2008-11-18 20:31
525 查看
Code
private void GridViewToExcel()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=WebOrderExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 解决前导0丢失问题
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.Page.EnableViewState = false;
tw.WriteLine(strStyle);
GridView1.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// 解决 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewToExcel();
}
private void GridViewToExcel()
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=WebOrderExcelFile.xls");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/ms-excel";
// 解决前导0丢失问题
string strStyle = "<style>td{mso-number-format:\"\\@\";}</style>";
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
GridView1.Page.EnableViewState = false;
tw.WriteLine(strStyle);
GridView1.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// 解决 类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
protected void Button1_Click1(object sender, EventArgs e)
{
GridViewToExcel();
}
相关文章推荐
- GridView to Excel
- ASP.Net 2.0: Export GridView to Excel - Part II
- gridview to excel
- export GridView to excel
- Export large data from GridView to Excel file using C#
- print gridview to excel and print several tables to excel(one sheet)
- Some thoughts about exporting GridView to Excel
- Export GridView to Excel
- Multiple GridView that contain multiple header row export data to excel(csharp)
- Export GridView to Excel within an UpdatePanel
- gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)
- gridview内容To EXCEL的要点
- Export Data from GridView to Excel, Word, HTML with C#
- Export GridView with Images from database to Word Excel and PDF Formats
- [译]ASP.Net 2.0: Export GridView to Excel
- ASP.Net 2.0: Export GridView to Excel
- Export GridView To WordExcelPDFCSV in ASP.Net
- ASP.Net 2.0: Export GridView to Excel
- Export GridView to Excel
- gridview to excel ; excel to ds