项目--简单导出CSV文件

    //导出
    protected void BtnOutPut_Click(object sender, EventArgs e)
    {
        //角色
        InitialRoles();
        DataTable dt = DBClass.GetDataTable(string.Format(@"SELECT * FROM {0} Order By SN desc", View(Where)));
        StringWriter sw = new StringWriter();
        //4S店不能看到总部价格等相关字段
        if (UMRoleNames.Contains("4S"))
        {
            sw = Get4SStr(dt);
        }
        else
        {
            sw = GetAdminStr(dt);
        }

        sw.Close();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".csv");
        Response.ContentType = "application/ms-excel";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
        Response.Write(sw);
        Response.End();
    }
 //4S店拼接
    protected StringWriter Get4SStr(DataTable dt)
    {
        StringWriter sw = new StringWriter();				
        sw.WriteLine(@"采购订单号,区域,4S店名,品牌,零件编码,零件名称,车型代码,数量,4S成本价,总部采购单价,总部采购合计,4S店节约金额,供应商,要求到货日期,实际到货日期,4S采购折扣");
        foreach (DataRow dr in dt.Rows)
        {
            sw.WriteLine(dr["SN"].ToString()
                + "," + dr["FirstArea"].ToString()
                + "," + dr["ShopName"].ToString()
                + "," + dr["ShopBrand"].ToString()
                + "," + "'" + dr["PNO"].ToString()
                + "," + dr["PName"].ToString()
                + "," + dr["PCarNumber"].ToString()
                + "," + dr["BuyNumber"].ToString()
                + "," + dr["PSRP"].ToString()
                + "," + dr["AdminPrice"].ToString()
                + "," + dr["SUM4SPrice"].ToString()
                + "," + dr["ThriftProfit4S"].ToString()
                + "," + dr["OOffer"].ToString()
                + "," + dr["OJiaoHuoDate"].ToString()
                + "," + dr["RealReachDate"].ToString()
                + "," + dr["Discount4S"].ToString()
                );
        }
        return sw;
    }

  

posted @ 2014-07-07 17:44  步子521  阅读(202)  评论(0编辑  收藏  举报