예제 #1
0
        public static string GetContent <T>(IEnumerable <T> entities, CsvConfig config) where T : new()
        {
            if (config == null)
            {
                config = new CsvConfig(typeof(T));
            }

            CsvPropertyInfo[] csvPropertyInfos = GetPropertyInfos <T>();
            StringBuilder     sb = new StringBuilder();

            if (config.HasHeader)
            {
                List <string> titles = new List <string>();
                foreach (CsvPropertyInfo csvPi in csvPropertyInfos)
                {
                    titles.Add(CsvContext.FixField(csvPi.Attribute.Name, config.Delimiter));
                    //sb.Append(CsvContext.FixField(csvPi.Attribute.Name, config.Delimiter) + config.Delimiter);
                }
                sb.AppendLine(string.Join(config.Delimiter, titles.ToArray()));
            }

            foreach (T entity in entities)
            {
                sb.AppendLine(GetLine(entity, config, csvPropertyInfos));
            }
            return(sb.ToString());
        }
예제 #2
0
        //public static DataTable GetDataTable(string path)
        //{
        //    return GetDataTable(path,",", Encoding.Default);
        //}

        public static DataTable GetDataTable(string path, string delimiter, Encoding encoding)
        {
            if (path.GetIsOleDb())
            {
            }

            DataTable dt = new DataTable();

            dt.Locale = CultureInfo.InvariantCulture;
            using (StreamReader sr = new StreamReader(path, encoding))
            {
                string   firstLine = sr.ReadLine();
                string[] titles    = CsvContext.GetFields(firstLine, delimiter);

                for (int i = 0; i < titles.Length; i++)
                {
                    dt.Columns.Add(titles[i]);
                }

                while (!sr.EndOfStream)
                {
                    string   line   = sr.ReadLine();
                    string[] fields = CsvContext.GetFields(line, delimiter);
                    DataRow  dr     = dt.NewRow();
                    for (int i = 0; i < fields.Length && i < dt.Columns.Count; i++)
                    {
                        dr[i] = fields[i];
                    }
                    dt.Rows.Add(dr);
                }
            }
            return(dt);
        }
예제 #3
0
        public static T ReadRecord <T>(string line, string[] titles, CsvConfig config) where T : new()
        {
            CsvRecord           record     = CsvContext.GetRecords(line, config.Delimiter).First();
            List <PropertyInfo> properties = typeof(T).GetProperties().ToList();

            return(ReadRecord <T>(record, properties));
        }
예제 #4
0
 public static T[] ReadFile <T>(string path, string tableName, CsvConfig config)
     where T : new()
 {
     if (path.GetIsOleDb())
     {
         DataTable table = CsvContext.GetDataTable(path, tableName);
         return(ReadDataTable <T>(table, config));
     }
     else
     {
         return(ReadFile <T>(path, config));
     }
 }
예제 #5
0
        public static string GetLine <T>(T entity, CsvConfig config, CsvPropertyInfo[] csvPropertyInfos) where T : new()
        {
            if (config == null)
            {
                config = new CsvConfig(typeof(T));
            }
            //StringBuilder sb = new StringBuilder();
            List <string> fields = new List <string>();

            foreach (CsvPropertyInfo csvPi in csvPropertyInfos)
            {
                fields.Add(CsvContext.FixField(csvPi.PropertyInfo.GetValue(entity, null).ToString(), config.Delimiter));
            }
            return(string.Join(config.Delimiter, fields.ToArray()));
        }
예제 #6
0
        public static DataTable GetDataTable(string path, string delimiter, Encoding encoding)
        {
            if (path.GetIsOleDb())
            {
            }

            DataTable dt = new DataTable();

            dt.Locale = CultureInfo.InvariantCulture;
            using (StreamReader sr = new StreamReader(path, encoding))
            {
                string   firstLine = sr.ReadLine().Trim();
                string[] titles    = CsvContext.GetRecords(firstLine, delimiter).First().ToArray();

                for (int i = 0; i < titles.Length; i++)
                {
                    dt.Columns.Add(titles[i]);
                }

                string fixedLine = string.Empty;
                while (!sr.EndOfStream)
                {
                    string line = sr.ReadLine().Trim();
                    //skip empty lines
                    if (line == string.Empty)
                    {
                        continue;
                    }
                    //read lines continuously
                    int lastDoubleQuote = line.LastIndexOf('"');
                    { }
                    string[] fields = CsvContext.GetRecords(line, delimiter).First().ToArray();
                    DataRow  dr     = dt.NewRow();
                    for (int i = 0; i < fields.Length && i < dt.Columns.Count; i++)
                    {
                        dr[i] = fields[i];
                    }
                    dt.Rows.Add(dr);
                }
            }
            return(dt);
        }
예제 #7
0
        public static T ReadLine <T>(string line, string[] titles, CsvConfig config) where T : new()
        {
            string[]            fields = CsvContext.GetFields(line, config.Delimiter);
            T                   result = new T();
            Type                t      = typeof(T);
            List <PropertyInfo> pis    = t.GetProperties().ToList();

            for (int i = 0; i < fields.Length; i++)
            {
                PropertyInfo pi = null;
                if (config.MappingType == CsvMappingType.Title)
                {
                    pi = GetPropertyInfo(pis, i, titles[i], config);
                }

                if (config.MappingType == CsvMappingType.Order)
                {
                    pi = GetPropertyInfo(pis, i);
                }

                if (pi != null)
                {
                    pi.SetValue(result, fields[i]);
                }
            }

            foreach (PropertyInfo pi in pis)
            {
                //If the property is adorned with CsvOriginalFieldsAttribute, set the value to fields
                if (pi.IsDefined(typeof(CsvOriginalFieldsAttribute), true))
                {
                    pi.SetValue(result, fields, null);
                }
            }
            return(result);
        }
예제 #8
0
        private static void WriteFile <T>(StreamWriter sw, T[] objects, CsvConfig config) where T : new()
        {
            CsvPropertyInfo[] properties = GetPropertyInfos <T>();


            if (config.HasHeader)
            {
                foreach (CsvPropertyInfo csvPi in properties)
                {
                    sw.Write(CsvContext.FixField(csvPi.Attribute.Name, config.Delimiter) + config.Delimiter);
                }
                sw.WriteLine();
            }

            foreach (T o in objects)
            {
                foreach (CsvPropertyInfo csvPi in properties)
                {
                    sw.Write(CsvContext.FixField(csvPi.PropertyInfo.GetValue(o, null).ToString(), config.Delimiter) + config.Delimiter);
                }
                sw.WriteLine();
            }
            sw.Flush();
        }