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

      

  • 相关阅读:
    MySQL事务_transaction
    mysql
    反射跳过泛型检查
    spring java.io.FileNotFoundException cannot be opened because it does not exist
    Servlet中ServletConfig的作用
    Exception in thread "main" java.lang.ClassCastException: java.base/java.util.HashMap$Values cannot be cast to java.base/java.util.ArrayList
    java中字符串和其他数据类型之间使用“+”号连接
    comboBox加载数据
    自动填充
    3号随笔,搭建web环境
  • 原文地址:https://www.cnblogs.com/ChobitsSP/p/3628456.html
Copyright © 2011-2022 走看看