예제 #1
0
        public static CsvData ObjectToCsvData(object obj)
        {
            CsvData csvData = CsvData.Create();

            Type t = obj.GetType();

            if (!typeof(IEnumerable).IsAssignableFrom(t))
            {
                throw new ArgumentException("对象没有实现IEnumerable");
            }

            //build header
            FieldInfo[] obj_fields = t.GetFields();
            foreach (var field in obj_fields)
            {
                csvData.Header.Add(field.Name);
            }

            var obj_props = t.GetProperties();

            foreach (PropertyInfo prop in obj_props)
            {
                csvData.Header.Add(prop.Name);
            }

            //build body
            IEnumerable enumerable = (IEnumerable)obj;
            IEnumerator record_it  = enumerable.GetEnumerator();

            while (record_it.MoveNext())
            {
                object item = record_it.Current;
                if (item == null)
                {
                    continue;
                }
                //行内数据
                List <string> recordBuilder = new List <string>();
                Type          itemType      = item.GetType();
                FieldInfo[]   fields        = itemType.GetFields();
                foreach (FieldInfo field in fields)
                {
                    string recordItem = field.GetValue(item).ToString();
                    recordBuilder.Add(recordItem);
                }
                PropertyInfo[] propInfos = itemType.GetProperties();
                foreach (PropertyInfo prop in propInfos)
                {
                    string recordItem = prop.GetValue(item, null).ToString();
                    recordBuilder.Add(recordItem);
                }

                csvData.InsertData(recordBuilder.ToArray());
            }

            return(csvData);
        }
예제 #2
0
        public static CsvData Create(IList header = null)
        {
            CsvData self = new CsvData();

            if (header == null)
            {
                self.Header = new List <string>();
            }
            self.data = new List <CsvRecord>();
            return(self);
        }
예제 #3
0
        public static CsvData Read(string csv)
        {
            Reset();
            csvContent = csv;
            //行 列
            List <List <string> > dataTable  = new List <List <string> >();
            List <string>         recordData = new List <string>();

            dataTable.Add(recordData);

            string token = null;

            while ((token = GetToken()) != null)
            {
                //换行增加新纪录
                if (token == LFSTR)
                {
                    recordData = new List <string>();
                    dataTable.Add(recordData);
                }
                else
                {
                    recordData.Add(token);
                }
            }

            //移除所有尾部空行
            for (int i = dataTable.Count - 1; i >= 0; i--)
            {
                if (dataTable[i].Count == 0)
                {
                    dataTable.RemoveAt(i);
                }
                else
                {
                    break;
                }
            }

            List <string> header = null;

            if (dataTable.Count > 1)
            {
                header = dataTable[0];
            }
            CsvData csvData = CsvData.Create(header);

            for (int i = 1; i < dataTable.Count; i++)
            {
                csvData.InsertData(dataTable[i].ToArray());
            }

            return(csvData);
        }
예제 #4
0
        public static string Write(CsvData csvData)
        {
            StringBuilder sb = new StringBuilder();

            //header
            WriteLine(sb, csvData.Header);
            //body
            foreach (CsvRecord item in csvData)
            {
                WriteLine(sb, item);
            }
            string rtn = sb.ToString();

            sb.Clear();
            return(rtn);
        }
예제 #5
0
        public static IList <T> CsvDataToObject <T>(CsvData data) where T : new()
        {
            Type     objType = typeof(T);
            List <T> list    = new List <T>();

            //把csvData的Record导出到object
            foreach (CsvRecord record in data)
            {
                T tobj = new T();
                for (int i = 0; i < record.FieldCount; i++)
                {
                    SetValue(tobj, record.GetName(i), record[i]);
                }
                list.Add(tobj);
            }
            return(list);
        }
예제 #6
0
 public CsvRecord(CsvData csvData, IEnumerable <string> data)
 {
     this.csvData = csvData;
     this.data    = new List <string>(data);
 }