您的位置:首页 > 编程语言 > C#

C# 导出Excel代码

2009-02-28 11:07 225 查看
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Mobile;
using System.Web.UI.MobileControls;
using System.Xml;

/// <summary>
/// OutToExcel 导出Excel的通用方法
/// </summary>
public class OutToExcel : System.Web.UI.Page
{
public OutToExcel()
{
//
// OutToExcel 导出Excel的通用方法
//
}
public static void ToExcel(Control ctl, string FileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=/"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls/"");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
Response.Flush();
Page.FindControl(_PrintArea).RenderControl(hw);
HttpContext.Current.Response.End();
}

public static void ToExcelGb(Control ctl, string FileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=/"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls/"");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.End();
}
public static void StringToExcel(string str,string FileName)
{
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=/"" + System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls/"");

HttpContext.Current.Response.Write(str.ToString());
HttpContext.Current.Response.End();
}
}
*******************************************页面引用法***********************

#region 导出Excel

//在导出时必须用这
public override void VerifyRenderingInServerForm(Control control)
{

}
/// <summary>
/// 导出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Execle_Button_Click(object sender, EventArgs e)
{
gvCarBase.GridLines = GridLines.Both;
OutToExcel.ToExcel(this.gvCarBase, "Excel的表名");
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: