CsvHelper:nuget地址

csv导出类||生成类

    public class CSVHeader
{
public string head1 { get; set; }
public string head2 { get; set; }
public string head3 { get; set; }
public string head4 { get; set; }
public string head5 { get; set; }
}

csv map 类

    public class CSVHeaderMap : CsvClassMap<CSVHeader>
{
public CSVHeaderMap()
{
Map(m => m.head1).Name("head1");
Map(m => m.head2).Name("head2");
Map(m => m.head3).Name("head3");
Map(m => m.head4).Name("head4");
Map(m => m.head5).Name("head5");
}
}

csv operate 类

    public class CSVHelper<T> where T : class
{
private static Logger _logger = NLog.LogManager.GetCurrentClassLogger(); public static string GeneralCSV(List<T> dataList, string csvFileName, string localPath)
{
if (dataList == null || dataList.Count == )
{
Console.WriteLine("no report date");
return "";
}
try
{
string localFileName = string.IsNullOrWhiteSpace(csvFileName) ? $"{DateTime.Now.ToString("yyyyMMddhhssmmm")}.csv" : csvFileName; if (!Directory.Exists(localPath))
{
Directory.CreateDirectory(localPath);
}
string csvContent = "";
using (FileStream fs = new FileStream(Path.Combine(localPath, localFileName), FileMode.Create, FileAccess.ReadWrite))
using (StreamWriter strW = new StreamWriter(fs, Encoding.UTF8))
{ csvContent = GenerateCsvContent(dataList); if (csvContent == null) return "";
strW.Write(csvContent);
}
return csvContent;
}
catch (Exception ex)
{
Console.WriteLine("GeneralCSV",
$"csvFileName:{csvFileName}, Path:{localPath}",
ex.Message);
return "";
}
} static string GenerateCsvContent(List<T> itemList, bool hasHeaderRecord = true)
{
try
{
using (StringWriter sWriter = new StringWriter())
{
CsvConfiguration config = new CsvConfiguration();
config.HasHeaderRecord = hasHeaderRecord;
//char soh = '\u0001';
//config.Delimiter = soh.ToString();
using (var csv = new CsvWriter(sWriter, config))
{
csv.WriteRecords(itemList); return sWriter.ToString();
}
}
}
catch (Exception ex)
{
var itemContent = JsonConvert.SerializeObject(itemList);
Console.Write("HDFSApiHelper.GenerateCsvFile"
, $"itemList:{itemContent}"
, ex.Message);
return null;
}
} public static List<T> GetEntityFromCSV<M>(Stream csvMemory,string delimiter=",") where M : CsvClassMap<T>
{
var csvReader = new StreamReader(csvMemory);
var csvConfig = new CsvConfiguration();
csvConfig.HasHeaderRecord = true;
csvConfig.RegisterClassMap<M>();
CsvReader csv = new CsvReader(csvReader, csvConfig);
return csv.GetRecords<T>().ToList();
} public static List<T> GetEntityFromCSV(Stream csvMemory, string delimiter = ",")
{
var csvReader = new StreamReader(csvMemory);
var temp = csvReader.ReadToEnd();
var csvConfig = new CsvConfiguration()
{
HasHeaderRecord = true,
IgnoreHeaderWhiteSpace = true,
IsHeaderCaseSensitive = false,
Delimiter = delimiter
};
var csv = new CsvReader(csvReader);
try
{
return csv.GetRecords<T>().ToList(); }
catch(Exception ex)
{
_logger.Error(ex.ToString());
}
return null;
}
}

csv test method

            List<CSVHeader> test = new List<CSVHeader>();
test.Add(new CSVHeader() { head1="",head2="",head3="",head4="",head5=""});
test.Add(new CSVHeader() { head1 = "", head2 = "", head3 = "", head4 = "", head5 = "" });
test.Add(new CSVHeader() { head1 = "", head2 = "", head3 = "", head4 = "", head5 = "" });
test.Add(new CSVHeader() { head1 = "", head2 = "", head3 = "", head4 = "", head5 = "" });
test.Add(new CSVHeader() { head1 = "", head2 = "", head3 = "", head4 = "", head5 = "" });
string csvDirectory = @"D:\Jimmy Team Project\Doc\2017-11-20日报表汇总\test\";
//生成csv
CSVHelper<CSVHeader>.GeneralCSV(test,"test.csv", csvDirectory); //csv获取list 数据
var mergeExcelPath = csvDirectory+ @"\result.xlsx";
using (FileStream fs = new FileStream(csvDirectory + @"test.csv", FileMode.Open, FileAccess.Read))
{
List<CSVHeader> date = CSVHelper<CSVHeader>.GetEntityFromCSV<CSVHeaderMap>(fs, ",");
}

Nuget CsvHelper 的使用的更多相关文章

  1. 常用库nuget包集合

    ColorConsole htmlagilitypack.1.4.9.5 经测试效率比 CsQueryLaster 高 csvhelper Extend Devlib系列一套 itextsharp l ...

  2. NuGet在2015中的使用

    NuGet Package Restore  https://docs.nuget.org/Consume/Package-Restore 以https://github.com/andburn/hd ...

  3. CsvHelper文档-1前言

    CsvHelper文档-1前言 英文文档链接地址:CsvHelper Document 开源项目地址:CsvHelper 翻译于2018-1-5,原本可能会随时更新: 每一段代码都是经过我实际测试的, ...

  4. nuget 包管理器

    nuget 是.Net平台上的包管理器, 对于包的发布(打包 package)和消费(下载依赖管理)都有很好的支持, 本文仅仅关注消费端, =======================nuget项目 ...

  5. NetCore利用CsvHelper解析支付宝对账单

    支付宝账单是zip压缩文件流,里面包含了两个.csv文件. 1.请求支付宝账单下载链接,获取到zip文件流. var httpClient = _clientFactory.CreateClient( ...

  6. NuGet镜像上线试运行

    为解决国内访问NuGet服务器速度不稳定的问题,我们用阿里云服务器搭建了一个NuGet镜像,目前已上线试运行. 使用NuGet镜像源的方法如下: 1)NuGet镜像源地址:https://nuget. ...

  7. 通过ProGet搭建一个内部的Nuget服务器

    .NET Core项目完全使用Nuget 管理组件之间的依赖关系,Nuget已经成为.NET 生态系统中不可或缺的一个组件,从项目角度,将项目中各种组件的引用统统交给NuGet,添加组件/删除组件/以 ...

  8. .NET Core 系列5 :使用 Nuget打包类库

    NuGet是个开源项目,项目包括 NuGet VS插件/NuGet Explorer/NuGetServer/NuGet命令行等项目,.NET Core项目完全使用Nuget 管理组件之间的依赖关系, ...

  9. 如何在nuget上传自己的包+搭建自己公司的NuGet服务器(新方法)

    运维相关:http://www.cnblogs.com/dunitian/p/4822808.html#iis 先注册一个nuget账号https://www.nuget.org/ 下载并安装一下Nu ...

随机推荐

  1. 使用Java函数接口及lambda表达式隔离和模拟外部依赖更容易滴单测

    概述 单测是提升软件质量的有力手段.然而,由于编程语言上的支持不力,以及一些不好的编程习惯,导致编写单测很困难. 最容易理解最容易编写的单测,莫过于独立函数的单测.所谓独立函数,就是只依赖于传入的参数 ...

  2. hive 常见时间日期函数的使用

    1.时间戳函数 日期转时间戳:从1970-01-01 00:00:00 UTC到指定时间的秒数 获得当前时区的UNIX时间戳: select unix_timestamp(); 1533716607 ...

  3. Subversion1.8源码安装流程

    为了解决svnamin:Unrecognized record type in stream的问题,决定将Subversion1.7升级为Subversion1.8 Subversion1.8的源码安 ...

  4. node.js学习(一)

    一.assert assert.deepEqual(actual, expected[, message]) 测试 actual 参数与 expected 参数是否深度相等. 原始值使用相等运算符(= ...

  5. GoldenGate 12.3 MA架构介绍系列(2) - 数据同步测试

    安装配置可参考上一篇:http://www.cnblogs.com/margiex/p/8071957.html 安装完成之后,会自动启动ServiceManager服务,此时,可以通过浏览器访问. ...

  6. 为什么List.add()所增加的数据都是一样的

    1. 先上代码: List<Person> list = new ArrayList<>(); Person p = new Person(); try { Class.for ...

  7. Python进阶【第八篇】迭代器和生成器

    一.何谓迭代 如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们称为迭代(Iteration).迭代是一个重复的过程,每次重复即一次迭代,并且每次迭代 ...

  8. linux下的ifconfig命令

    ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置. 1.命令格式: ifconfig [网络设备] [参数] 2.命令功能: ifconfig 命令用来查看和配置网络设 ...

  9. GUID生成函数

    /** * GUID生成函数 * @return string */function create_guid() { $charid = strtoupper(md5(uniqid(mt_rand() ...

  10. 错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 解决方法

    晚上花几分钟在windows下测了下pthread的用法,出现错误 1 error LNK2019: 无法解析的外部符号 __imp__pthread_create,该符号在函数 _main 中被引用 ...