栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

c# List与Csv文件互转(特性实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c# List与Csv文件互转(特性实现)

c# List与Csv文件互转(特性实现)
public static class CsvOrTsvHelper
	{
		/// List转流(csv,tsv格式)
        /// 
        /// 文件格式
        /// success flag
        public static NPOIMemoryStream SaveDataToCSVFile(this List dataList, string fileFormat) where T : class
        {
            string separator;
            if (fileFormat.ToLower() == "csv")
                separator = ",";
            else
                separator = "t";

            NPOIMemoryStream stream = new NPOIMemoryStream();
            StreamWriter writer = new StreamWriter(stream);

            StringBuilder strColumn = new StringBuilder();
            StringBuilder strValue = new StringBuilder();
            var tp = typeof(T);
            PropertyInfo[] props = tp.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            int[] filter = new int[props.Length];

            try
            {
                //标题
                for (int i = 0; i < props.Length; i++)
                {
                    var itemPropery = props[i];
                    DescriptionAttribute labelAttr = itemPropery.GetCustomAttributes(typeof(DescriptionAttribute), true).FirstOrDefault() as DescriptionAttribute;
                    if (null != labelAttr)
                    {
                        strColumn.Append(labelAttr.Description);
                        strColumn.Append(separator);
                    }
                    else
                    {
                        filter[i] = 1;
                    }
                }
                strColumn.Remove(strColumn.Length - 1, 1);
                writer.WriteLine(strColumn.ToString());

                //内容
                for (int i = 0; i < dataList.Count; i++)
                {
                    var model = dataList[i];
                    strValue.Clear();
                    for (int m = 0; m < props.Length; m++)
                    {
                        var itemPropery = props[m];
                        if (filter[m] == 1) continue;

                        var val = itemPropery.GetValue(model, null);
                        if (m == 0)
                        {
                            strValue.Append(val);
                        }
                        else
                        {
                            strValue.Append(separator);
                            strValue.Append(val);
                        }
                    }
                    writer.WriteLine(strValue.ToString());
                }

                writer.Flush();
                stream.Position = 0;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Dispose();
                }
            }

            return stream;
        }
		/// 
        /// 流转list
        /// 
        /// 文件流
        /// 
        public static List CsvOrTsvConvertToList(this Stream fileStream, bool isCsv = true)
        {
            List list = new List { };
            StreamReader sr = new StreamReader(fileStream, Encoding.GetEncoding("utf-8"));

            string tempText = "";
            int Rows = 1; //标注第几行
            //缓存列索引对应属性名
            Dictionary colIndexMapName = new Dictionary();

            while ((tempText = sr.ReadLine()) != null)
            {
                string[] arr = CsvOrTsvstrToArry(tempText, isCsv ? ',' : 't');
                var obj = Activator.CreateInstance();
                Type type = obj.GetType();
                //获取所有公共字段名
                PropertyInfo[] peroperties = type.GetProperties();
                if (Rows == 1)
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        foreach (var property in peroperties)
                        {
                            var proName = property.Name;
                            object[] objs = property.GetCustomAttributes(typeof(DescriptionAttribute), true);
                            if (objs != null && objs.Length > 0)
                            {
                                var description = ((DescriptionAttribute)objs[0]).Description;
                                if (description == arr[i])
                                {
                                    colIndexMapName.Add(i, proName);
                                }
                            }
                        }
                    }
                }
                else
                {
                    for (int i = 0; i < arr.Length; i++)
                    {
                        foreach (var property in peroperties)
                        {
                            if (colIndexMapName.ContainsKey(i) && property.Name == colIndexMapName[i])
                            {
                                ExcelHelper.ConvertByPropTypeName(property, obj, arr[i]);
                            }
                        }
                    }
                    list.Add(obj);
                }
                Rows++;
            }

            return list;
        }

        private static string[] CsvOrTsvstrToArry(string splitStr, char ignoreChar)
        {
            splitStr = splitStr.Replace("""", "'");
            var newstr = string.Empty;
            List sList = new List();

            bool isSplice = false;
            string[] array = splitStr.Split(new char[] { ignoreChar });
            foreach (var str in array)
            {
                if (!string.IsNullOrEmpty(str) && str.IndexOf('"') > -1)
                {
                    var firstchar = str.Substring(0, 1);
                    var lastchar = string.Empty;
                    if (str.Length > 0)
                    {
                        lastchar = str.Substring(str.Length - 1, 1);
                    }
                    if (firstchar.Equals(""") && !lastchar.Equals("""))
                    {
                        isSplice = true;
                    }
                    if (lastchar.Equals("""))
                    {
                        if (!isSplice)
                            newstr += str;
                        else
                            newstr = newstr + ignoreChar + str;

                        isSplice = false;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(newstr))
                        newstr += str;
                }

                if (isSplice)
                {
                    //添加因拆分时丢失的逗号
                    if (string.IsNullOrEmpty(newstr))
                        newstr += str;
                    else
                        newstr = newstr + ignoreChar + str;
                }
                else
                {
                    sList.Add(newstr.Replace(""", "").Replace("'", """).Trim());//去除字符中的双引号和首尾空格
                    newstr = string.Empty;
                }
            }

            return sList.ToArray();
        }
	}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号