首页 > 其他 > 详细

C# CsvFile 类

时间:2014-07-22 22:42:43      阅读:382      评论:0      收藏:0      [点我收藏+]
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnfleCrawler.Repository
{
    /// <summary>
    /// Class to store one CSV row
    /// </summary>
    public class CsvRow : List<object>
    {
        public string LineText { get; set; }
    }

    /// <summary>
    /// Class to write data to a CSV file
    /// </summary>
    public class CsvFileWriter : StreamWriter
    {
        public char FieldChar { get; private set; }

        public CsvFileWriter(string filename, bool append = false, char fieldChar = ,)
            : base(filename, append, Encoding.GetEncoding("GB18030"))
        {
            this.FieldChar = fieldChar;
        }

        /// <summary>
        /// Writes a single row to a CSV file.
        /// </summary>
        /// <param name="row">The row to be written</param>
        public void WriteRow(CsvRow row)
        {
            var builder = new StringBuilder();
            var vTypes = new Type[] { typeof(Guid), typeof(DateTime) };
            bool firstColumn = true;
            foreach (object value in row)
            {
                string text;
                if (value == null)
                {
                    text = string.Empty;
                }
                else
                {
                    Type type = value.GetType();
                    if (type == vTypes[0])
                    {
                        text = "{" + value + "}";
                    }
                    else if (type == vTypes[1])
                    {
                        //text = ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss");
                        text = ((DateTime)value).ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        text = value.ToString();
                    }
                }

                if (!firstColumn)
                {
                    builder.Append(FieldChar);
                }
                // Implement special handling for values that contain comma or quote
                // Enclose in quotes and double up any double quotes
                if (text.IndexOfAny(new char[] { ", FieldChar }) != -1)
                {
                    builder.AppendFormat("\"{0}\"", text.Replace("\"", "\"\""));
                }
                else
                {
                    builder.Append(text);
                }
                firstColumn = false;
            }
            row.LineText = builder.ToString();
            WriteLine(row.LineText);
            Flush();
        }
    }

    /// <summary>
    /// Class to read data from a CSV file
    /// </summary>
    public class CsvFileReader : StreamReader
    {
        public CsvFileReader(Stream stream)
            : base(stream)
        {
        }

        public CsvFileReader(string filename)
            : base(filename)
        {
        }

        /// <summary>
        /// Reads a row of data from a CSV file
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        public bool ReadRow(CsvRow row)
        {
            row.LineText = ReadLine();
            if (String.IsNullOrEmpty(row.LineText))
                return false;

            int pos = 0;
            int rows = 0;

            while (pos < row.LineText.Length)
            {
                string value;

                // Special handling for quoted field
                if (row.LineText[pos] == ")
                {
                    // Skip initial quote
                    pos++;

                    // Parse quoted value
                    int start = pos;
                    while (pos < row.LineText.Length)
                    {
                        // Test for quote character
                        if (row.LineText[pos] == ")
                        {
                            // Found one
                            pos++;

                            // If two quotes together, keep one
                            // Otherwise, indicates end of value
                            if (pos >= row.LineText.Length || row.LineText[pos] != ")
                            {
                                pos--;
                                break;
                            }
                        }
                        pos++;
                    }
                    value = row.LineText.Substring(start, pos - start);
                    value = value.Replace("\"\"", "\"");
                }
                else
                {
                    // Parse unquoted value
                    int start = pos;
                    while (pos < row.LineText.Length && row.LineText[pos] != ,)
                        pos++;
                    value = row.LineText.Substring(start, pos - start);
                }

                // Add field to list
                if (rows < row.Count)
                    row[rows] = value;
                else
                    row.Add(value);
                rows++;

                // Eat up to and including next comma
                while (pos < row.LineText.Length && row.LineText[pos] != ,)
                    pos++;
                if (pos < row.LineText.Length)
                    pos++;
            }
            // Delete any unused items
            while (row.Count > rows)
                row.RemoveAt(rows);

            // Return true if any columns read
            return (row.Count > 0);
        }
    }
}

//void ReadTest()
//{
//    // Read sample data from CSV file
//    using (CsvFileReader reader = new CsvFileReader("ReadTest.csv"))
//    {
//        CsvRow row = new CsvRow();
//        while (reader.ReadRow(row))
//        {
//            foreach (string s in row)
//            {
//                Console.Write(s);
//                Console.Write(" ");
//            }
//            Console.WriteLine();
//        }
//    }
//}
bubuko.com,布布扣
using AnfleCrawler.Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace AnfleCrawler.Repository
{
    public class CsvRepository : Disposable, IRepository
    {
        //public static void Save(IEnumerable<楼盘> set)
        //{
        //    char xChar = ‘卐‘;
        //    using (var writer1 = new CsvFileWriter("楼盘.txt", fieldChar: xChar))
        //    using (var writer2 = new CsvFileWriter("楼栋.txt", fieldChar: xChar))
        //    using (var writer3 = new CsvFileWriter("房间.txt", fieldChar: xChar))
        //    {
        //        Type type = typeof(楼盘);
        //        var props = type.GetProperties().Where(p => p.Name != "楼栋").ToArray();
        //        foreach (var louPan in set)
        //        {
        //            var row = new CsvRow();
        //            row.Add(props[0].GetValue(louPan));
        //            for (int i = 1; i < props.Length; i++)
        //            {
        //                object val = props[i].GetValue(louPan);
        //                row.Add(val);
        //            }
        //            writer1.WriteRow(row);
        //        }
        //    }
        //}

        private CsvFileWriter _lpWriter, _ldWriter, _fjWriter;
        private Type[] _types = new Type[] { typeof(HousesEntity), typeof(BuildingEntity), typeof(RoomEntity) };
        private Dictionary<Type, PropertyInfo[]> _props;
        private IRepository _sync;

        public CsvRepository(string prefix, IRepository sync = null)
        {
            char xChar = ;
            _lpWriter = new CsvFileWriter(string.Format("{0}楼盘.txt", prefix), true, fieldChar: xChar);
            _ldWriter = new CsvFileWriter(string.Format("{0}楼栋.txt", prefix), true, fieldChar: xChar);
            _fjWriter = new CsvFileWriter(string.Format("{0}房间.txt", prefix), true, fieldChar: xChar);
            _props = new Dictionary<Type, PropertyInfo[]>();
            InitProps();
            _sync = sync;
        }
        protected override void DisposeInternal(bool disposing)
        {
            if (disposing)
            {
                _lpWriter.Dispose();
                _ldWriter.Dispose();
                _fjWriter.Dispose();
            }
        }

        private void InitProps()
        {
            foreach (var type in _types)
            {
                _props.Add(type, type.GetProperties());
            }
        }

        void IRepository.SaveProxy(ProxyEntity entity)
        {
            if (_sync != null)
            {
                _sync.SaveProxy(entity);
            }
        }

        public HousesEntity LoadHouses(Guid hashKey)
        {
            if (_sync != null)
            {
                return _sync.LoadHouses(hashKey);
            }
            return new HousesEntity()
            {
                RowID = hashKey,
            };
        }
        public BuildingEntity LoadBuilding(Guid hashKey, Guid relationID)
        {
            if (_sync != null)
            {
                return _sync.LoadBuilding(hashKey, relationID);
            }
            return new BuildingEntity()
            {
                RowID = hashKey,
                RelationID = relationID,
            };
        }
        public RoomEntity LoadRoom(Guid hashKey, Guid relationID)
        {
            if (_sync != null)
            {
                return _sync.LoadRoom(hashKey, relationID);
            }
            return new RoomEntity()
            {
                RowID = hashKey,
                RelationID = relationID,
            };
        }

        public void Save(HousesEntity entity)
        {
            if (_sync != null)
            {
                _sync.Save(entity);
            }
            lock (_lpWriter)
            {
                var props = _props[_types[0]].Where(p => p.Name != "楼栋").ToArray();
                var row = new CsvRow();
                row.Add(props[0].GetValue(entity));
                for (int i = 1; i < props.Length; i++)
                {
                    object val = props[i].GetValue(entity);
                    row.Add(val);
                }
                _lpWriter.WriteRow(row);
            }
        }
        public void Save(BuildingEntity entity)
        {
            if (_sync != null)
            {
                _sync.Save(entity);
            }
            var vProps = new string[] { "楼盘", "房间" };
            var props = _props[_types[1]].Where(p => !vProps.Contains(p.Name)).ToArray();
            var row = new CsvRow();
            row.Add(props[0].GetValue(entity));
            for (int i = 1; i < props.Length; i++)
            {
                object val = props[i].GetValue(entity);
                row.Add(val);
            }
            _ldWriter.WriteRow(row);
        }
        public void Save(RoomEntity entity)
        {
            if (_sync != null)
            {
                _sync.Save(entity);
            }
            Type type = entity.GetType();
            var props = _props[_types[2]].Where(p => p.Name != "楼栋").ToArray();
            var row = new CsvRow();
            row.Add(props[0].GetValue(entity));
            for (int i = 1; i < props.Length; i++)
            {
                object val = props[i].GetValue(entity);
                row.Add(val);
            }
            _fjWriter.WriteRow(row);
        }

        public void SavePrice(CategoryPriceEntity entity)
        {
            throw new NotSupportedException();
        }
        public void SaveHouselisting(HouselistingEntity entity)
        {
            throw new NotImplementedException();
        }
        public Guid SaveDiscount(DiscountEntity entity)
        {
            throw new NotSupportedException();
        }
        public Guid SaveDiscountInfo(DiscountInfoEntity entity)
        {
            throw new NotSupportedException();
        }
        public void SaveSchool(SchoolEntity entity)
        {
            throw new NotImplementedException();
        }
        public void SaveSchoolHouses(SchoolHousesEntity entity)
        {
            throw new NotImplementedException();
        }
        public void SaveSchoolHouselisting(SchoolHouselistingEntity entity)
        {
            throw new NotImplementedException();
        }
    }
}
View Code

C# CsvFile 类,布布扣,bubuko.com

C# CsvFile 类

原文:http://www.cnblogs.com/Googler/p/3860089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!