添加项目文件。
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
using CsvHelper;
|
||||
using JY.Utility;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace JY.Inspection
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入数据到csv文件
|
||||
/// </summary>
|
||||
/// <param name="filePath">所需存储文件夹路径(取系统所设定值,不带日期文件夹)</param>
|
||||
/// <param name="data">数据源</param>
|
||||
/// <param name="flag">1进站,2出站,3智能电表.csv,4预警</param>
|
||||
/// <returns></returns>
|
||||
public static bool WriteCSV(string filePath, List<T> data, int flag)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath))
|
||||
{
|
||||
filePath = Application.StartupPath + "\\localData\\" + DateTime.Now.ToString("yyyyMMdd");
|
||||
}
|
||||
else
|
||||
{
|
||||
filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd");
|
||||
}
|
||||
if (!Directory.Exists(filePath))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
}
|
||||
switch (flag)
|
||||
{
|
||||
case 1:
|
||||
filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "进站.csv";
|
||||
break;
|
||||
case 2:
|
||||
filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "出站.csv";
|
||||
break;
|
||||
case 3:
|
||||
filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "智能电表.csv";
|
||||
break;
|
||||
default:
|
||||
filePath = filePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "报警.csv";
|
||||
break;
|
||||
}
|
||||
try
|
||||
{
|
||||
var cfg = new CsvHelper.Configuration.CsvConfiguration(CultureInfo.InvariantCulture);
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
cfg.HasHeaderRecord = false;//是否将第一行作为标题
|
||||
}
|
||||
using (var writer = new StreamWriter(filePath, true, Encoding.GetEncoding("GB2312")))
|
||||
{
|
||||
using (var csv = new CsvWriter(writer, cfg))
|
||||
{
|
||||
csv.WriteRecords(data);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Error(ex.ToString());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user