zoukankan      html  css  js  c++  java
  • 将GridView中内容导入到Word

    以下是引用片段:
      -----

         //重写此方法,否则会出现类型"GridView"的控件"GridView1"必须放在具有runat=server的窗体标记内."的错误.
        public override void VerifyRenderingInServerForm(Control control)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=.;database=northwind;uid=sa;pwd=");
            SqlDataAdapter da = new SqlDataAdapter("select * from customers", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            this.GridView1.DataSource = ds;
            this.GridView1.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc");
            Response.ContentEncoding = System.Text.Encoding.UTF7;
            Response.ContentType = "application/ms-word";
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            this.GridView1.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }


    导入到EXCEL与之类似:
    以下是引用片段:
      ----- 

         Response.Clear();
        Response.Buffer = true;
        Response.Charset 
    = "GB2312";
        Response.AppendHeader(
    "Content-Disposition""attachment;filename=FileName.xls");
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType 
    = "application/ms-excel";//设置输出文件类型为excel文件。 
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter 
    = new System.Web.UI.HtmlTextWriter(oStringWriter);
        
    this.GridView1.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End();


    http://www.cnblogs.com/guodapeng/archive/2007/09/27/908312.html  C#导出Excel汇总
  • 相关阅读:
    如何快速转载CSDN及博客园中的博客
    Ubuntu18.04连不网 报"有线连接未托管"
    Ubuntu18.04的网络配置
    vim基本操作
    Git更新远程仓库代码到本地(转)
    POJ 3253 Fence Repair
    POJ 2503 Babelfish
    POJ 2002 Squares
    POJ 1840 Eqs
    POJ 3274 Gold Balanced Lineup
  • 原文地址:https://www.cnblogs.com/Caesar/p/1023247.html
Copyright © 2011-2022 走看看