using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.Common;
using System.Data.SqlClient;
using System.IO;
public partial class ExportWE : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(\"server=.;uid=sa;pwd=;database=pubs\");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
public override void VerifyRenderingInServerForm(Control control)
{
}
void bind()
{
SqlDataAdapter sda = new SqlDataAdapter(\"select * from publishers\", con);
DataSet ds = new DataSet();
sda.Fill(ds,\"publishers\");
GridView1.DataSource = ds;
GridView1.DataBind();
}
private void ExportWordOrExcel(string ExportType, GridView gv)
{
string filetype = \"\";
if (ExportType == \"application/vnd.ms-word\")
filetype = \".doc\";
if (ExportType == \"application/vnd.ms-excel\")
filetype = \".xls\";
Response.Clear();
//Response.Charset = \"utf-8\";
Response.Charset = \"GB2312\";
Response.AppendHeader(\"Content-Disposition\", \"attachment;filename=Pig,Please name\" + filetype);
Response.ContentType = ExportType;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.AllowPaging = false;
bind();
gv.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
gv.AllowPaging = true;
bind();
}
///下面是导出到Word里面,要是做导出到Excel和这个一样的
protected void ExportWord_Click(object sender, EventArgs e)
{
ExportWordOrExcel(\"application/vnd.ms-word\", this.GridView1);
}
}