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
        }
    }
  • 相关阅读:
    mybatis学习成长之路(一)
    badboy页面脚本发生错误,解决方案
    资料下载地址大全
    excel2003和excel2007文件的创建和读取
    文件的上传下载
    读取.properties的内容1
    Java的垃圾回收机制
    Bell数和Stirling数
    Catalan数计算及应用
    [算法]循环赛日程表
  • 原文地址:https://www.cnblogs.com/honghong75042/p/5908363.html
Copyright © 2011-2022 走看看