zoukankan      html  css  js  c++  java
  • 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;
                }
            }

  • 相关阅读:
    .a包生成64位
    iOS教程
    iOS 难题解决日志------2层控制器 上面的控制器显示透明
    企业级的App发布流程
    如何从oc中去获取一个私有的变量.....
    iOS app的破解原理,就是去除已付费的账户信息的原理是什么?
    Could not launch "app_name"
    GCD时间轴
    Win8自动更新怎么关闭 取消Win8自动更新
    python3 elf文件解析
  • 原文地址:https://www.cnblogs.com/blsong/p/2097574.html
Copyright © 2011-2022 走看看