C# 从DataSet导出到Excel

    查出数据并导出到excel,测试了下,可以生成。  

   
    //从DataSet到出到Excel
    protected void Button1_Click(object sender, EventArgs e)
    {
        string sqlStr = "select * from IoApply ";
     //string sqlStr = "select id as 序号,(case Gender when 0 then '女' when 1 then '男' end) as 性别  from OpenDay order by RegisterTime desc";
            DataTable DtOut = DataAccess.ExecuteSql("OpenDay.ConnectionString", sqlStr).Tables[0];
            Response.Clear();
            Response.ContentEncoding = Encoding.Default;
            Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode("OpenDay"+System.DateTime.Now.ToString("MM.dd")+".xls"));
            Response.ContentType = "application/octet-stream";

            for (int i = 0; i < DtOut.Columns.Count; i++)
            {
                Response.Write(DtOut.Columns[i].ColumnName.ToString() + "\t");
            }

            Response.Write(Environment.NewLine);
            for (int i = 0; i < DtOut.Rows.Count; i++)
            {
               
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < DtOut.Columns.Count; j++)
                {
                    sb.Append(DtOut.Rows[i][j].ToString());
                    sb.Append("\t");
                }
                Response.Write(sb.ToString());
                Response.Write(Environment.NewLine);
            }   

            Response.End();
        }     
       
    }

转载于:https://www.cnblogs.com/lcq135/archive/2008/05/12/1194097.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值