public class CsvFileStream
{
TextReader stream;
bool EOS = false;
bool EOL = false;
public CsvFileStream(TextReader s)
{
stream= s;
}
public string[] GetNextRow()
{
ArrayList row= new ArrayList();
while (true)
{
string item = GetNextItem();
if (item == null)
return row.Count == 0 ? null : (string[])row.ToArray(typeof(string));
row.Add(item);
}
}
string GetNextItem()
{
if (EOL)
{
// previous item was last in line, start new line EOL = false;
return null;
}
bool quoted = false;
bool predata = true;
bool postdata = false;
StringBuilder sb= new StringBuilder();
while (true)
{
char c = GetNextChar(true);
if (EOS)
return sb.Length > 0 ? sb.ToString() : null;
if ((postdata || !quoted) && c == ',')
// end of item, return return sb.ToString();
if ((predata || postdata || !quoted) && (c == 'x0A' || c == 'x0D'))
{
// we are at the end of the line, eat newline characters and exit EOL = true;
if (c == 'x0D' && GetNextChar(false) == 'x0A')
// new line sequence is 0D0A GetNextChar(true);
return sb.ToString();
}
if (predata && c == ' ')
// whitespace preceeding data, discard continue;
if (predata && c == '"')
{
// quoted data is starting quoted = true;
predata= false;
continue;
}
if (predata)
{
// data is starting without quotes predata = false;
sb.Append(c);
continue;
}
if (c == '"' && quoted)
{
if (GetNextChar(false) == '"')
// double quotes within quoted string means add a quote sb.Append(GetNextChar(true));
else // end-quote reached postdata = true;
continue;
}
// all cases covered, character must be data sb.Append(c);
}
}
char[] buffer = new char[4096];
int pos = 0;
int length = 0;
char GetNextChar(bool eat)
{
if (pos >= length)
{
length= stream.ReadBlock(buffer, 0, buffer.Length);
if (length == 0)
{
EOS= true;
return '';
}
pos= 0;
}
if (eat)
return buffer[pos++];
else return buffer[pos];
}
}
CsvFileWriter.cs
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站制作、成都網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的工布江達網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!public class CsvFileWriter
{
private ArrayList rowAL; //行鏈表,CSV文件的每一行就是一個鏈 private string fileName; //文件名 private Encoding encoding; //編碼 public CsvFileWriter()
{
this.rowAL = new ArrayList();
this.fileName = "";
this.encoding = Encoding.Default;
}
/// <summary> ///
/// </summary> /// <param name="fileName">文件名,包括文件路徑</param> public CsvFileWriter(string fileName)
{
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = Encoding.Default;
}
/// <summary> ///
/// </summary> /// <param name="fileName">文件名,包括文件路徑</param> /// <param name="encoding">文件編碼</param> public CsvFileWriter(string fileName, Encoding encoding)
{
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = encoding;
}
/// <summary> /// row:行,row = 1代表第一行
/// col:列,col = 1代表第一列
/// </summary> public string this[int row, int col]
{
set
{
//對行進行判斷 if (row <= 0)
{
throw new Exception("行數(shù)不能小于0");
}
else if (row > this.rowAL.Count) //如果當前列鏈的行數(shù)不夠,要補齊 {
for (int i = this.rowAL.Count + 1; i <= row; i++)
{
this.rowAL.Add(new ArrayList());
}
}
else
{
}
//對列進行判斷 if (col <= 0)
{
throw new Exception("列數(shù)不能小于0");
}
else
{
ArrayList colTempAL= (ArrayList)this.rowAL[row - 1];
//擴大長度 if (col > colTempAL.Count)
{
for (int i = colTempAL.Count; i <= col; i++)
{
colTempAL.Add("");
}
}
this.rowAL[row - 1] = colTempAL;
}
//賦值 ArrayList colAL = (ArrayList)this.rowAL[row - 1];
colAL[col- 1] = value;
this.rowAL[row - 1] = colAL;
}
}
/// <summary> /// 文件名,包括文件路徑
/// </summary> public string FileName
{
set
{
this.fileName = value;
}
}
/// <summary> /// 文件編碼
/// </summary> public Encoding FileEncoding
{
set
{
this.encoding = value;
}
}
/// <summary> /// 獲取當前大行
/// </summary> public int CurMaxRow
{
get
{
return this.rowAL.Count;
}
}
/// <summary> /// 獲取大列
/// </summary> public int CurMaxCol
{
get
{
int maxCol;
maxCol= 0;
for (int i = 0; i < this.rowAL.Count; i++)
{
ArrayList colAL= (ArrayList)this.rowAL[i];
maxCol= (maxCol > colAL.Count) ? maxCol : colAL.Count;
}
return maxCol;
}
}
/// <summary> /// 添加表數(shù)據(jù)到CSV文件中
/// </summary> /// <param name="dataDT">表數(shù)據(jù)</param> /// <param name="beginCol">從第幾列開始,beginCol = 1代表第一列</param> public void AddData(DataTable dataDT, int beginCol)
{
if (dataDT == null)
{
throw new Exception("需要添加的表數(shù)據(jù)為空");
}
int curMaxRow;
curMaxRow= this.rowAL.Count;
for (int i = 0; i < dataDT.Rows.Count; i++)
{
for (int j = 0; j < dataDT.Columns.Count; j++)
{
this[curMaxRow + i + 1, beginCol + j] = dataDT.Rows[i][j].ToString();
}
}
}
/// <summary> /// 保存數(shù)據(jù),如果當前硬盤中已經(jīng)存在文件名一樣的文件,將會覆蓋
/// </summary> public void Save()
{
//對數(shù)據(jù)的有效性進行判斷 if (this.fileName == null)
{
throw new Exception("缺少文件名");
}
else if (File.Exists(this.fileName))
{
File.Delete(this.fileName);
}
if (this.encoding == null)
{
this.encoding = Encoding.Default;
}
System.IO.StreamWriter sw= new StreamWriter(this.fileName, false, this.encoding);
for (int i = 0; i < this.rowAL.Count; i++)
{
sw.WriteLine(ConvertToSaveLine((ArrayList)this.rowAL[i]));
}
sw.Close();
}
/// <summary> /// 保存數(shù)據(jù),如果當前硬盤中已經(jīng)存在文件名一樣的文件,將會覆蓋
/// </summary> /// <param name="fileName">文件名,包括文件路徑</param> public void Save(string fileName)
{
this.fileName = fileName;
Save();
}
/// <summary> /// 保存數(shù)據(jù),如果當前硬盤中已經(jīng)存在文件名一樣的文件,將會覆蓋
/// </summary> /// <param name="fileName">文件名,包括文件路徑</param> /// <param name="encoding">文件編碼</param> public void Save(string fileName, Encoding encoding)
{
this.fileName = fileName;
this.encoding = encoding;
Save();
}
/// <summary> /// 轉(zhuǎn)換成保存行
/// </summary> /// <param name="colAL">一行</param> /// <returns></returns> private string ConvertToSaveLine(ArrayList colAL)
{
string saveLine;
saveLine= "";
for (int i = 0; i < colAL.Count; i++)
{
saveLine+= ConvertToSaveCell(colAL[i].ToString());
//格子間以逗號分割 if (i < colAL.Count - 1)
{
saveLine+= ",";
}
}
return saveLine;
}
/// <summary> /// 字符串轉(zhuǎn)換成CSV中的格子
/// 雙引號轉(zhuǎn)換成兩個雙引號,然后首尾各加一個雙引號
/// 這樣就不需要考慮逗號及換行的問題
/// </summary> /// <param name="cell">格子內(nèi)容</param> /// <returns></returns> private string ConvertToSaveCell(string cell)
{
cell= cell.Replace(""","""");
return """ + cell + """;
//return ""; }
}
CsvFileReader.cs
public class CsvFileReader
{
public static DataTable Parse(string data, bool headers)
{
return Parse(new StringReader(data), headers);
}
public static DataTable Parse(string data)
{
return Parse(new StringReader(data));
}
public static DataTable Parse(TextReader stream)
{
return Parse(stream, false);
}
public static DataTable Parse(TextReader stream, bool headers)
{
DataTable table= new DataTable();
CsvFileStream csv= new CsvFileStream(stream);
string[] row = csv.GetNextRow();
if (row == null) return null;
if (headers)
{
foreach (string header in row)
{
if (header != null && header.Length > 0 && !table.Columns.Contains(header))
table.Columns.Add(header,typeof(string));
else
table.Columns.Add(GetNextColumnHeader(table),typeof(string));
}
row= csv.GetNextRow();
}
while (row != null)
{
while (row.Length > table.Columns.Count)
table.Columns.Add(GetNextColumnHeader(table),typeof(string));
table.Rows.Add(row);
row= csv.GetNextRow();
}
return table;
}
static string GetNextColumnHeader(DataTable table)
{
int c = 1;
while (true)
{
string h = "Column" + c++;
if (!table.Columns.Contains(h))
return h;
}
}
}
eg.
TextReader reader = new StreamReader(@"D: est.csv");
DataTable dt= CsvFileReader.Parse(reader, false);
當前標題:CreatenewtoolforCSV-創(chuàng)新互聯(lián)
新聞來源:http://muchs.cn/article44/csgjhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航、營銷型網(wǎng)站建設(shè)、App設(shè)計、移動網(wǎng)站建設(shè)、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容