博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# CsvFile 类
阅读量:5111 次
发布时间:2019-06-13

本文共 11874 字,大约阅读时间需要 39 分钟。

using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace AnfleCrawler.Repository{    ///     /// Class to store one CSV row    ///     public class CsvRow : List    {        public string LineText { get; set; }    }    ///     /// Class to write data to a CSV file    ///     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;        }        ///         /// Writes a single row to a CSV file.        ///         /// The row to be written        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(); } } /// /// Class to read data from a CSV file /// public class CsvFileReader : StreamReader { public CsvFileReader(Stream stream) : base(stream) { } public CsvFileReader(string filename) : base(filename) { } /// /// Reads a row of data from a CSV file /// /// ///
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();// }// }//}
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
_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
(); 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

转载于:https://www.cnblogs.com/Googler/p/3860089.html

你可能感兴趣的文章
GreenDao数据库的简单使用
查看>>
Starting cloudera-scm-server: * Couldn't start cloudera-scm-server的解决办法(图文详解)
查看>>
Hadoop的ChainMapper和ChainReducer使用案例(链式处理)(四)
查看>>
linux 强制删除yum安装的php7.2
查看>>
uiautomator_python使用汇总
查看>>
tomcat cluster session同步时保存map数据遇到的问题
查看>>
Javascript备忘录-枚举一个对象的所有属
查看>>
Asp.net MVC DefaultModelBinder分析
查看>>
KVM安装
查看>>
w3cschool -css
查看>>
《Entity Framework 6 Recipes》中文翻译系列 (10) -----第二章 实体数据建模基础之两实体间Is-a和Has-a关系建模、嵌入值映射 (转)...
查看>>
又是毕业季I
查看>>
涛涛的Party
查看>>
SQL Server 触发器
查看>>
Silverlight 5 系列学习之一
查看>>
最值栈
查看>>
EXTJS中文乱码
查看>>
POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
查看>>
POJ 2528 Mayor's posters 线段树+离散化
查看>>
将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
查看>>