ASP.net中导出Excel表

来源:互联网 发布:sql查询200到300条记录 编辑:程序博客网 时间:2024/05/29 13:54

这是我做的一个消费记录的根据时间 类型的导出方法

带条件的导出

 protected void btnExport_Click(object sender, EventArgs e)
    {
        string shopid = Request.Cookies["shopid"].Value.ToString();
        string beginTime = this.txtStartTime.Text;
        string endTime = this.txtEndTime.Text;
        string cardType = "";
        if (this.DropDownList1.SelectedValue == "")
        {
            cardType = "";
        }
        else
        {
            if (this.DropDownList2.SelectedValue == "")
            {
                cardType = this.DropDownList1.SelectedValue;
            }
            else
            {
                cardType = this.DropDownList2.SelectedValue;
            }
        }
        DataSet ds = new UserPayInfo_WT().UserPayInfoTongJi(shopid, beginTime, endTime, cardType);
        DataTable dt = ds.Tables[0];
        ExportToExcle(dt, "XiaoFeiJiLu.xls");
    }

 

设置表头 带条件的导出

  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        DataRow[] myRow = dt.Select("");

        Response.Write("<table  border=1px>");
        Response.Write(string.Format("<tr><td colspan='5' align='center'>开始时间:{0} 结束时间:{1} 卡类型:{2}; {3}销售汇总表</td></tr>",beginTime,endTime,cardType,shopName));
        Response.Write("<tr><td>物品编码</td><td>物品名称</td><td>常用计量单位</td><td>数量</td><td>消费总额</td></tr>");

        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //Response.Write("<script>alert('" + dt.Columns.Count + "');</script>");
            Response.Write("<tr>");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                Response.Write("<td>" + dt.Rows[i][j].ToString() + "</td>");
            }
            Response.Write("</tr>");
        }
        Response.Write("</table>");
        Response.Flush();
        Response.End();

 

 

原创粉丝点击