csv内存流文件流

Posted 光阴易逝,岂容我待

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csv内存流文件流相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace BFF.ExportData
{
    public class ExportFile
    {
        #region Export
        public static MemoryStream StreamToCSV<T>(IEnumerable<T> dataSource, string fileName)
            where T : class
        {
            var result = string.Empty;

            //Header
            StringBuilder headerRow = new StringBuilder();
            var properties = typeof(T).GetProperties().Select(p => new
            {
                PropertyInfo = p,
                HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute
            })
                                                            .Where(p => p.HeaderAttr != null)
                                                            .OrderBy(p => p.HeaderAttr.Order)
                                                            .ThenBy(p => p.PropertyInfo.Name)
                                                            .ToList();


            var propertiesCount = properties.Count();

            for (int i = 0; i < propertiesCount; i++)
            {
                if (i < propertiesCount - 1)
                    headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(",");
                else
                    headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine);
            }

            //Data
            StringBuilder fields = new StringBuilder();
            foreach (var item in dataSource)
            {
                if (item == null)
                    continue;
                for (int i = 0; i < propertiesCount; i++)
                {
                    var propertyValue = properties[i].PropertyInfo.GetValue(item, null);
                    var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString();
                    if (i < propertiesCount - 1)
                        formatStringToCSVForm(fields, displayPropertyValue, false);
                    else
                        formatStringToCSVForm(fields, displayPropertyValue, true);
                }
                fields.Append(Environment.NewLine);
            }
            //build
            result = headerRow.ToString() + fields.ToString();
            //return result;

            byte[] bytetxt = Encoding.UTF8.GetBytes(result);
            MemoryStream memstream = new MemoryStream();
            memstream.Write(bytetxt, 0, bytetxt.Length);
            memstream.Seek(0, SeekOrigin.Begin);
            return memstream;
        }

        public static void ExportToCSV<T>(IEnumerable<T> dataSource, string fileName)
            where T : class
        {
            var result = string.Empty;

            //Header
            StringBuilder headerRow = new StringBuilder();
            var properties = typeof(T).GetProperties().Select(p => new
            {
                PropertyInfo = p,
                HeaderAttr = p.GetFirstCustomAttribute<ExportHeaderAttribute>() as ExportHeaderAttribute
            })
                                                            .Where(p => p.HeaderAttr != null)
                                                            .OrderBy(p => p.HeaderAttr.Order)
                                                            .ThenBy(p => p.PropertyInfo.Name)
                                                            .ToList();


            var propertiesCount = properties.Count();

            for (int i = 0; i < propertiesCount; i++)
            {
                if (i < propertiesCount - 1)
                    headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(",");
                else
                    headerRow.Append(properties[i].HeaderAttr.DisplayName).Append(Environment.NewLine);
            }

            //Data
            StringBuilder fields = new StringBuilder();
            foreach (var item in dataSource)
            {
                if (item == null)
                    continue;
                for (int i = 0; i < propertiesCount; i++)
                {
                    var propertyValue = properties[i].PropertyInfo.GetValue(item, null);
                    var displayPropertyValue = propertyValue == null ? string.Empty : propertyValue.ToString();
                    if (i < propertiesCount - 1)
                        formatStringToCSVForm(fields, displayPropertyValue, false);
                    else
                        formatStringToCSVForm(fields, displayPropertyValue, true);
                }
                fields.Append(Environment.NewLine);
            }
            //build
            result = headerRow.ToString() + fields.ToString();
            //return result;

            byte[] bytetxt = Encoding.UTF8.GetBytes(result);
            FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            f.Write(bytetxt,0,bytetxt.Length);
            f.Close();
        }

      

        private static void formatStringToCSVForm(StringBuilder sb, string field, bool isLast)
        {

            if (string.IsNullOrEmpty(field))
                sb.Append(",");
            else
            {
                sb.AppendFormat("\"{0}\"", field.Replace("\"", "\"\""));
                if (!isLast)
                    sb.Append(",");
            }
        }
        #endregion
    }
}

 

以上是关于csv内存流文件流的主要内容,如果未能解决你的问题,请参考以下文章

R中的流处理大型csv文件

将大型CSV流写入内存中的ZipOutputStream会占用与CSV或潜在zip的大小相同的内存吗?

如何压缩流csv文件php

java怎么将生成的文件放入内存?

如何使用 node.js 顺序读取 csv 文件(使用流 API)

java把数据库读取的数据通过流写入到csv文件里,请问怎么写代码?请求。