asp.net 导出CSV

原文:http://www.cnblogs.com/wcj1984abc/archive/2011/03/17/1986872.html

前台代码

<asp:Button   runat="server" ID="btnDC" Text ="导出CSV" onclick="btnDC_Click"/>

后台代码

  protected void btnDC_Click(object sender, EventArgs e)
        {
            DataTable dt = WorkOrderQuery.FindMyOrderList(358, 1, 100, "");//数据源
            try
            {
                StringWriter swCSV = new StringWriter();
                //列名
                swCSV.WriteLine("工单编号,工单标题,工单类型,创建时间,当前状态,当前节点名称,当前处理人 ");

                //遍历datatable导出数据
                foreach (DataRow drTemp in dt.Rows)
                {
                    StringBuilder sbText = new StringBuilder();
                    sbText = AppendCSVFields(sbText, drTemp["WORK_ORDER_ID"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["work_order_title"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["type_name"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["create_time"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["status_name"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["point_name"].ToString());
                    sbText = AppendCSVFields(sbText, drTemp["username"].ToString());
                    //去掉尾部的逗号
                    sbText.Remove(sbText.Length - 1, 1);

                    //写datatable的一行
                    swCSV.WriteLine(sbText.ToString());
                }
                //下载文件
                DownloadFile(Response, swCSV.GetStringBuilder(), "RuleDaySum.csv");

                swCSV.Close();
                Response.End();

            }
            catch (Exception ex)
            {
                 throw ex;
            }
        }

        /// <summary>
        /// csv添加逗号 用来区分列
        /// </summary>
        /// <param name="argFields">字段</param>
        /// <returns>添加后内容</returns>
        private StringBuilder AppendCSVFields(StringBuilder argSource, string argFields)
        {
            return argSource.Append(argFields.Replace(",", " ").Trim()).Append(",");
        }


        /// <summary>
        /// 弹出下载框
        /// </summary>
        /// <param name="argResp">弹出页面</param>
        /// <param name="argFileStream">文件流</param>
        /// <param name="strFileName">文件名</param>
        public static void DownloadFile(HttpResponse argResp, StringBuilder argFileStream, string strFileName)
        {
            try
            {
                string strResHeader = "attachment; filename=" + Guid.NewGuid().ToString() + ".csv";
                if (!string.IsNullOrEmpty(strFileName))
                {
                    strResHeader = "inline; filename=" + strFileName;
                }
                argResp.AppendHeader("Content-Disposition", strResHeader);//attachment说明以附件下载,inline说明在线打开
                argResp.ContentType = "application/ms-excel";
                argResp.ContentEncoding = Encoding.GetEncoding("GB2312"); // Encoding.UTF8;//
                argResp.Write(argFileStream);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

posted on 2011-07-04 16:59  风雨者2  阅读(521)  评论(0编辑  收藏  举报

导航