首页 > 代码库 > 项目--简单导出CSV文件
项目--简单导出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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。