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(@"\r") || str.Contains(@"\n"))

            {

                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

    }

}

  

你可能感兴趣的:(Writer)