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 ...
随机推荐
- CSS 关于IE6 margin 为负数 负值的时候 正常显示的方法
一定要加position: relative; 有时候比如margin-left的负数,还需要加上如 float:left 属性.
- easyui datagrid 设置列宽
<script> $(document).ready(function () { alert("sdf"); ...
- DRUPAL点滴
1 admin/config/user-interface/xxx在hook_menu里定义这样一个path,就会在configuration界面看到xxx的链接 2 $news_items = db ...
- AlphaGo实现原理
AlphaGo已经打败了李世石9段,如果你也懂它背后的原理,或许某一天你也可以开发出一款AI来打败dota或者LOL的世界冠军. Mastering the game of Go with deep ...
- 悟透javascript读书笔记
1.undefined,null,0,"" 这四个值转换为逻辑值时是false,其他无论简单类型值,对象或者函数转换过来都是true 2.如图 第一个是“声明了一个变量,给变量赋 ...
- 能源项目xml文件 -- springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 使用XIB实现一个简单view
技术处女贴 欢迎来探讨 转帖请注明出处 http://www.cnblogs.com/andy-zhou/p/4962135.html 微信: @Andy 1. AppDelegate AppDele ...
- 6/6 Sprint2 看板和燃尽图
页面头部的修改
- php 正则匹配中文
在javascript中,要判断字符串是中文是很简单的.比如:var str = "php编程";if (/^[\u4e00-\u9fa5]+$/.test(str)) {aler ...
- c# form的设定
1. 窗体的显示位置startPosition CenterScreen 窗体在当前居中 CenterParent 窗体在其父窗体居中 WindowDefaultBounds 窗体定期在windows ...