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
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
相关文章推荐
- 导出EXCEL代码(C#)
- C#实现GridView导出Excel实例代码
- 导出EXCEL代码(C#)
- C#导出EXCEL文件代码1
- 用C#.net编写导入导出EXCEL文件的代码。编绎运行后,错误提示: 找不到可安装的ISAM
- C#导入导出数据到Excel的通用类代码
- C#导入导出EXCEL文件的代码实例
- C#导出EXCEL代码1
- C#从实体对象集合中导出Excel的代码
- C#导出导入到Excel的代码
- GridView导出到Excel或Word文件的C#代码
- C# Excel导出后台代码
- c# 代码创建Excel文件 并且 将数据导出Excel
- 用C#代码把SQL 数据库的表导出到Excel
- c# 代码创建Excel文件 并且 将数据导出Excel
- C#导入导出数据到Excel的通用类代码
- C#导出Excel文件实例代码
- C#导出到Excel的代码
- C# 导出 Excel 封装类 代码
- C#导出到Excel的代码,现共享给大家