• CsvWriter


    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Web;
    
    namespace BuaaOnlineJudge
    {
        public static class CsvWriter
        {
            #region Constants
            private const char DefaultDelimiter = ',';
            private const char DefaultQuote = '"';
            private const char DefaultEscape = '"';
            #endregion
    
            #region Write
    
            public static TextWriter WriteRows(IEnumerable<string[]> rows, params string[] cols)
            {
                TextWriter sw = new StringWriter();
                Write(sw, cols);
                Write(sw, rows);
                return sw;
            }
    
            public static void Write(TextWriter writer, IEnumerable<string[]> rows)
            {
                foreach (var row in rows)
                {
                    Write(writer, row, DefaultDelimiter);
                }
            }
    
            public static void Write(TextWriter writer, IEnumerable<string> row)
            {
                Write(writer, row, DefaultDelimiter);
            }
    
            public static void Write(TextWriter writer, IEnumerable<string> row, char delimiter)
            {
                bool start = false;
                foreach (string str in row)
                {
                    if (start == true)
                        writer.Write(delimiter);
                    else
                        start = true;
                    writer.Write(CsvString(str, delimiter));
                }
                writer.WriteLine();
            }
    
            private static string CsvString(string str, char delimiter)
            {
                if (string.IsNullOrEmpty(str)) return string.Empty;
                if (str.Contains(delimiter.ToString()) || str.Contains(DefaultQuote.ToString()) || str.Contains(@"
    ") || str.Contains(@"
    "))
                {
                    var sb = new StringBuilder();
                    sb.Append(DefaultEscape);
                    foreach (char c in str)
                    {
                        if (c == DefaultQuote) sb.Append(DefaultQuote);
                        sb.Append(c);
                    }
                    sb.Append(DefaultEscape);
                    return sb.ToString();
                }
                return str;
            }
    
            #endregion
    
            #region web
    
            public static void DownLoadCsv(HttpResponse rsp, IEnumerable<string[]> rows, string name, params string[] cols)
            {
                string strResHeader = "attachment; filename=" + Guid.NewGuid().ToString() + ".csv";
                if (!string.IsNullOrEmpty(name))
                {
                    strResHeader = "attachment; filename=" + HttpUtility.UrlEncode(name, Encoding.UTF8) + ".csv";
                }
                rsp.AddHeader("Content-Disposition", strResHeader);//attachment说明以附件下载,inline说明在线打开
                rsp.ContentType = "application/ms-excel";
                rsp.ContentEncoding = Encoding.GetEncoding("GB2312"); // Encoding.UTF8;//
                TextWriter sw = WriteRows(rows, cols);
                rsp.Write(sw);
                rsp.End();
            }
    
            private static IEnumerable<string> CastString(IEnumerable<object> objs)
            {
                foreach (var obj in objs)
                {
                    yield return obj is string ? (string)obj : obj.ToString();
                }
            }
    
            #endregion
        }
    }
    

      

  • 相关阅读:
    针对数据库索引的优化
    acd
    HDOJ 5045 Contest
    《计算机时代》2015年第7期刊登出《基于数据仓库星形模式的广东省快速公路一张网资金结算情况分析系统》
    为什么大多数编程语言中的数组都从0開始
    十年,青春就是一转眼的事
    电子商务系统的设计与实现(十四):菜单高亮
    最近1个月的财务计划没有做好,囧啊
    最近1个月的财务计划没有做好,囧啊
    雷观(十九):我的人生观
  • 原文地址:https://www.cnblogs.com/ChobitsSP/p/3628456.html
Copyright © 2020-2023  润新知