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);
    }
}

相关文章:

  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-12
  • 2022-12-23
  • 2021-08-24
  • 2021-12-19
  • 2022-01-02
  • 2021-12-03
  • 2022-12-23
相关资源
相似解决方案