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 == 0) { 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="1",head2="2",head3="3",head4="4",head5="5"}); test.Add(new CSVHeader() { head1 = "1", head2 = "2", head3 = "3", head4 = "4", head5 = "5" }); test.Add(new CSVHeader() { head1 = "1", head2 = "2", head3 = "3", head4 = "4", head5 = "5" }); test.Add(new CSVHeader() { head1 = "1", head2 = "2", head3 = "3", head4 = "4", head5 = "5" }); test.Add(new CSVHeader() { head1 = "1", head2 = "2", head3 = "3", head4 = "4", head5 = "5" }); 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, ","); }
时间: 2024-10-16 04:07:46