一、CsvHelper 库 可通过nuget进行安装
二、封装导入导出方法
public class MyCsvHelper
{
public static bool WriteCsv<T>(List<T> datas,string filePath= "logs/TagData/tag.csv")
{
try
{
var currentPath = Directory.GetCurrentDirectory();
var path = Path.Combine(currentPath, filePath);
//判断文件是否存在
if (!Directory.Exists(Path.GetDirectoryName( path)))
{
Directory.CreateDirectory(Path.GetDirectoryName(path));
}
//判断文件是否存在
if (!File.Exists(path))
{
var file = File.Create(path);
file.Close();
}
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var writer = new StreamWriter(path, false, Encoding.GetEncoding("GB18030")))
{
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.WriteRecords(datas);
}
}
return true;
}
catch(Exception ex)
{
}
return false;
}
public static List<T> ReadCsv<T>(string filePath)
{
try
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
using (var reader = new StreamReader(filePath, Encoding.GetEncoding("GB18030")))
using (var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture)
{
HeaderValidated = null,
MissingFieldFound = null,
PrepareHeaderForMatch = args => args.Header.ToLower()
}))
{
var foos = csv.GetRecords<T>().ToList();
return foos.ToList();
}
}
catch
{
}
return new List<T>();
}
}