using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.IO; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;/// <summary> /// Summary description for db /// </summary> public class db { public string strCn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString; public db() { // // TODO: Add constructor logic here // } public DataTable getData() { DataTable dt = new DataTable(); SqlDataAdapter sqlAda = new SqlDataAdapter("select id ,name from tb4 ", strCn); sqlAda.Fill(dt); return dt; } /// <summary> /// 导出成为Excel /// </summary> /// <param name="exportTargetGridView">目标GridView</param> public static void ExportExcel(GridView exportTargetGridView) { HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.doc"); HttpContext.Current.Response.ContentType = "application/word"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); exportTargetGridView.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); } } 这是一个类,调用 这个类就可以,你试试.
孟子e章的经典作品
呵呵
好久没上孟老师那看看了 今天晚上特意去了看看
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;/// <summary>
/// Summary description for db
/// </summary>
public class db
{
public string strCn = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
public db()
{
//
// TODO: Add constructor logic here
//
}
public DataTable getData()
{
DataTable dt = new DataTable();
SqlDataAdapter sqlAda = new SqlDataAdapter("select id ,name from tb4 ", strCn);
sqlAda.Fill(dt);
return dt;
}
/// <summary>
/// 导出成为Excel
/// </summary>
/// <param name="exportTargetGridView">目标GridView</param>
public static void ExportExcel(GridView exportTargetGridView)
{
HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7; HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.doc"); HttpContext.Current.Response.ContentType = "application/word"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); exportTargetGridView.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End();
}
}
这是一个类,调用 这个类就可以,你试试.