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;
                }
            }

  • 相关阅读:
    eclipse卡死在search for main types 20 files to index
    多线程并发执行任务,取结果归集。终极总结:Future、FutureTask、CompletionService、CompletableFuture
    同步中的四种锁synchronized、ReentrantLock、ReentrantReadWriteLock、StampedLock
    根据CPU核心数确定线程池并发线程数
    将DataTable转换成CSV文件
    C#中DataTable转化为List<T>解析
    DotNet项目中的一些常用验证操作
    DotNet处理服务器路径的方法
    C#运用ThoughtWorks生成二维码
    C#枚举类型的常用操作总结
  • 原文地址:https://www.cnblogs.com/blsong/p/2097574.html
Copyright © 2011-2022 走看看