zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    【Java学习】向上和向下转型
    【Java学习】java抽象类的作用
    【Java学习】追踪
    【Java学习】@Override 解释
    【Java学习】Java 中带包(创建及引用)的类的编译与调试
    【Java学习】相关基础算法
    【Java学习】import和import static的区别
    【testNG学习】testng.xml文件
    Reshape the Matrix
    Distribute Candies
  • 原文地址:https://www.cnblogs.com/honghong75042/p/5908363.html
Copyright © 2011-2022 走看看