返回顶部

收藏

C#导出数据到Excel或者Word中的代码片段

更多
private void Page_Load(object sender, System.EventArgs e)
        {
            SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            SqlDataAdapter sda=new SqlDataAdapter();
            sda.SelectCommand=new SqlCommand("select * from txtInsert",con);
            DataSet ds=new DataSet();
            sda.Fill(ds,"emp");
            this.DgSource.DataSource=ds.Tables["emp"];
            this.DgSource.DataBind();
            con.Close();
        }

public void DataGridToExcel(DataGrid grdTemp,DataSet dsTemp)
        {
            grdTemp.AllowPaging=false;   //设置不能分页

            grdTemp.DataSource=dsTemp;  //重新绑定数据源
            grdTemp.DataBind();

            //常规导出方法

            System.IO.StringWriter SW = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter HTW=new System.Web.UI.HtmlTextWriter(SW);
            grdTemp.RenderControl(HTW);

            //Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
            Response.Buffer=true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.ContentType = "application/vnd.ms-excel";
            //Response.ContentType是输出流的 HTTP MIME 类型
            //Response.ContentType     --- word文件
            //application/vnd.ms-excel --- excel文件
            //
            Response.Charset="utf-8";
            Response.ContentEncoding=System.Text.Encoding.GetEncoding("utf-8");
            Response.AddHeader("Content-Disposition", "attachment;filename=aaa.xls");
            //attachment --- 作为附件下载
            //inline --- 在线打开
            //filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
            //进行进行编码,以解决文件名乱码的问题
            Response.Write(SW.ToString());
            Response.Flush();
            Response.Close();
        }

private void Button1_Click(object sender, System.EventArgs e)
        {
            SqlConnection con=new SqlConnection("server=.;database=pubs;uid=sa;pwd=;");
            con.Open();
            SqlDataAdapter sda=new SqlDataAdapter();
            sda.SelectCommand=new SqlCommand("select * from txtInsert",con);           
            DataSet ds=new DataSet();
            sda.Fill(ds,"emp");
            this.DgSource.DataSource=ds.Tables["emp"];           
            this.DataGridToExcel(this.DgSource,ds);
            con.Close();
        }

标签:c#

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2017-09-10 17:26:51通用序列化方法 by Foyon
  2. 2017-09-10 13:53:37C# 显示文件夹下的所有图片文件 by sunqi
  3. 2017-09-10 13:38:23MVC API post方式调用 by 格格巫2012
  4. 2017-09-08 11:39:37C# 读写文件window form操作代码 by dreamit
  5. 2017-09-07 17:23:13C#对文件进行加密解密代码 by 张永磊
  6. 2017-09-07 13:47:23ASP.NET 大文件下载实现代码 by huwei
  7. 2017-09-06 11:54:16c# Fusion Charts 简单操作 by 廖钊权
  8. 2017-09-06 11:24:01C# FTP操作类 by 吴红军
  9. 2017-09-04 12:38:06C# Random 生成不重复随机数 by Merrill
  10. 2017-09-03 12:21:30插入排序算法的C#实现 by fengsweat
  11. 2017-12-06 13:46:24水仙花数 by 阮小七

发表评论