using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.IO; using System.Collections; namespace demo.ClassHelper.CSV { public struct dataLog {//SITEID,PROJECTID,MACHINEID,DATE,TIME,LOT NAME,LOGIN MODE,KEYWORD,ERROR CODE,ERROR MESSAGE,MESSAGE public string SITEID; public string PROJECTID; public string MACHINEID; public string DATE; public string TIME; public string LOTNAME; public string LOGINMODE; public string KEYWORD; public string ERRORCODE; public string ERRORMESSAGE; public string MESSAGE; } class CsvHelper { /// /// 导出报表为Csv /// /// DataTable /// 物理路径 /// 表头 /// 字段标题,逗号分隔 public static bool dt2csv(DataTable dt, string strFilePath, string tableheader, string columname) { try { string strBufferLine = ""; StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8); strmWriterObj.WriteLine(tableheader); strmWriterObj.WriteLine(columname); for (int i = 0; i < dt.Rows.Count; i++) { strBufferLine = ""; for (int j = 0; j < dt.Columns.Count; j++) { if (j > 0) strBufferLine += ","; strBufferLine += dt.Rows[i][j].ToString(); } strmWriterObj.WriteLine(strBufferLine); } strmWriterObj.Close(); return true; } catch { return false; } } /// /// 将Csv读入DataTable /// /// csv文件路径 /// 表示第n行是字段title,第n+1行是记录开始 public static DataTable csv2dt(string filePath, int n, DataTable dt) { StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false); int i = 0, m = 0; reader.Peek(); while (reader.Peek() > 0) { m = m + 1; string str = reader.ReadLine(); if (m >= n + 1) { string[] split = str.Split(','); System.Data.DataRow dr = dt.NewRow(); for (i = 0; i < split.Length; i++) { dr[i] = split[i]; } dt.Rows.Add(dr); } } return dt; } //写入strdata public static void WriteStrdata(string fileName,string strdata) { StreamWriter sw; string strtime= DateTime.Now.ToString("yyyyMMdd")+".csv"; string fileName1 = fileName + "\\" + strtime; if (!File.Exists(fileName)) { string dataHeard = string.Empty; //这个地方是写入CSV的标题栏 注意最后个没有分隔符 dataHeard = "SITEID,PROJECTID,MACHINEID,DATE,TIME,LOT NAME,LOGIN MODE,KEYWORD,ERROR CODE,ERROR MESSAGE,MESSAGE"; sw = File.CreateText(fileName1.ToString()); sw.WriteLine(dataHeard); } else { sw = File.AppendText(fileName); } sw.WriteLine(strdata); sw.Flush(); sw.Close(); } //写入strdata public static void WriteStrdata(string fileName, dataLog strdata) { StreamWriter sw; string strtime = DateTime.Now.ToString("yyyyMMdd") + "_" + strdata.SITEID + "_" + strdata.PROJECTID + "_" + strdata.MACHINEID+ "_"+".csv"; string fileName1 = fileName + "\\" + strtime; if (!File.Exists(fileName1)) { string dataHeard = string.Empty; //这个地方是写入CSV的标题栏 注意最后个没有分隔符 dataHeard = "SITEID,PROJECTID,MACHINEID,DATE,TIME,LOT NAME,LOGIN MODE,KEYWORD,ERROR CODE,ERROR MESSAGE,MESSAGE"; sw = File.CreateText(fileName1.ToString()); sw.WriteLine(dataHeard); } else { sw = File.AppendText(fileName1); } strdata.DATE = DateTime.Now.ToString("MM/dd/yyyy"); strdata.TIME = DateTime.Now.ToString("HH:mm:ss"); string strcutstr = strdata.SITEID + "," + strdata.PROJECTID + "," + strdata.MACHINEID + "," + strdata.DATE + "," + strdata.TIME + "," + strdata.LOTNAME + "," + strdata.LOGINMODE + "," + strdata.KEYWORD + "," + strdata.ERRORCODE + "," + strdata.ERRORMESSAGE + "," + strdata.MESSAGE; sw.WriteLine(strcutstr); sw.Flush(); sw.Close(); } } }