40 lines
1.1 KiB
C#
40 lines
1.1 KiB
C#
using CsvHelper;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JY.Utility
|
|
{
|
|
public class CSVHelper<T>
|
|
{
|
|
/// <summary>
|
|
/// 读取CSV文件
|
|
/// </summary>
|
|
/// <param name="fileName">csv文件名</param>
|
|
/// <returns></returns>
|
|
public static List<T> ReadCSV(string fileName, string strSeparator = "\t")
|
|
{
|
|
if (!File.Exists(fileName)) return null;
|
|
//Nuget获取CsvHelper
|
|
using (var reader = new StreamReader(fileName))
|
|
{
|
|
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture)
|
|
{
|
|
Mode = CsvMode.Escape,
|
|
Escape = '\\',
|
|
Delimiter = strSeparator//设置分隔符号
|
|
};
|
|
using (var csv = new CsvReader(reader, cfg))
|
|
{
|
|
var list = csv.GetRecords<T>().ToList();
|
|
return list;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|