C# CsvFile 类
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[])
{
text = "{" + value + "}";
}
else if (type == vTypes[])
{
//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 }) != -)
{
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 = ;
int rows = ; 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 > );
}
}
} //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<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[]].Where(p => p.Name != "楼栋").ToArray();
var row = new CsvRow();
row.Add(props[].GetValue(entity));
for (int i = ; 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[]].Where(p => !vProps.Contains(p.Name)).ToArray();
var row = new CsvRow();
row.Add(props[].GetValue(entity));
for (int i = ; 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[]].Where(p => p.Name != "楼栋").ToArray();
var row = new CsvRow();
row.Add(props[].GetValue(entity));
for (int i = ; 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();
}
}
}
C# CsvFile 类的更多相关文章
- Java类的继承与多态特性-入门笔记
相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...
- C#中的CSV文件读写
目录 CSV文件标准 文件示例 RFC 4180 简化标准 读写CSV文件 使用CsvHelper 使用自定义方法 基于简化标准的写CSV文件 使用TextFieldParser解析CSV文件 使用正 ...
- C++ 可配置的类工厂
项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...
- Android请求网络共通类——Hi_博客 Android App 开发笔记
今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- PHP-解析验证码类--学习笔记
1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ...
随机推荐
- Http简析
HTTP协议 属于应用层的面向对象的协议 HTTP协议的主要特点 支持C/S(客户/服务器)模式. 简单快速:客户向服务器请求服务时,只需传送请求方法和路径.请求方法常用的有GET.HEAD.POST ...
- HM中再增加一路自己的entropy coder
compressSlice 中一开始的entropy coder 设置: // set entropy coder if( m_pcCfg->getUseSBACRD() ) { m_pcSba ...
- vsftp搭配iptables的配置
[similarface@InnerTest vsftpd]$ ll total 48 -rw------- 1 root root 125 Mar 23 02:26 ftpusers -rw-r-- ...
- 大S《美容大王》内容80%都是没用的东西
揭20位明星真实人品 我在她大陆的企划公司工作过,见过她N次了,对人态度蛮不错的,而且很爱开玩笑,但感觉很假很抠门.只要一回公司她就黏着请她吃饭.她很主动很霸道,她和周渝民交往,原因之一就是周什么都听 ...
- listview去掉底部多出的边框黑色
listview去掉底部多出的边框黑色 android:fadingEdge="none" //去掉listview黑色底边 listview.setDivider(null);
- 游戏引擎/GUI的设计与实现-常见GUI架构
以X Window为代表的客户/服务器架构. X Window通常是指X服务器及封装了通信协议的客户端库.服务器端主要负责输入事件的分发,窗口层次的管理,以及显示输出的处理,其它功能基本上都是在客户端 ...
- redis命令集合
一.连接控制 QUIT 关闭连接 AUTH (仅限启用时)简单的密码验证 二.适合全体类型的命令 EXISTS key 判断一个键是否存在;存在返回 1;否则返回0;DEL key 删除某个key,或 ...
- Question store (Repeated review)
题目36 - ACM在线评测系统http://acm.nyist.net/JudgeOnline/problem.php?pid=36 用户名密码INVATION 讲道理太卡 第一:要注意不同的函数 ...
- 丢失Ref Edit Control的解决方法
2010版本excel编制的带有控件的VBA,换在别的电脑使用有可能会出现“找不到工程或库”的错误,在VBE编辑器,打开工具——引用,可以看到“丢失 Ref Edit Control”解决方法是,先把 ...
- var isObj = length === undefined || i
这个其实是因为你前面那个===是肯定为false导致的,所以执行到了i那一步了var length=undefined;var a=length===undefined || i;这样你不定义i也是不 ...