C#讀取csv格局文件的辦法。本站提示廣大學習愛好者:(C#讀取csv格局文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#讀取csv格局文件的辦法正文
本文實例講述了C#讀取csv格局文件的辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:
1、CSV文件規矩
1 開首是不留空,以行動單元。
2 可含或不含列名,含列名則居文件第一行。
3 一行數據不跨行,無空行。
4 以半角逗號(即,)作分隔符,列為空也要表達其存在。
5 列內容如存在半角逗號(即,)則用半角引號(即',')將該字段值包括起來。
6 列內容如存在半角引號(即")則應調換成半角雙引號("")本義,並用半角引號(即"")將該字段值包括起來。
7 文件讀寫時引號,逗號操作規矩互逆。
8 內碼格局不限,可為 ASCII、Unicode 或許其他。
9 不支撐特別字符
2、C#讀取csv文件的辦法
//讀CSV文件類,讀取指定的CSV文件,可以導出DataTable
public class CsvStreamReader
{
private ArrayList rowAL; //行鏈表,CSV文件的每行就是一個鏈
private string fileName; //文件名
private Encoding encoding; //編碼
public CsvStreamReader()
{
this.rowAL = new ArrayList();
this.fileName = "";
this.encoding = Encoding.Default;
}
/// <summary>
///
/// </summary>
/// <param name="fileName">文件名,包含文件途徑</param>
public CsvStreamReader(string fileName)
{
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = Encoding.Default;
LoadCsvFile();
}
/// <summary>
///
/// </summary>
/// <param name="fileName">文件名,包含文件途徑</param>
/// <param name="encoding">文件編碼</param>
public CsvStreamReader(string fileName, Encoding encoding)
{
this.rowAL = new ArrayList();
this.fileName = fileName;
this.encoding = encoding;
LoadCsvFile();
}
/// <summary>
/// 文件名,包含文件途徑
/// </summary>
public string FileName
{
set
{
this.fileName = value;
LoadCsvFile();
}
}
/// <summary>
/// 文件編碼
/// </summary>
public Encoding FileEncoding
{
set
{
this.encoding = value;
}
}
/// <summary>
/// 獲得行數
/// </summary>
public int RowCount
{
get
{
return this.rowAL.Count;
}
}
/// <summary>
/// 獲得列數
/// </summary>
public int ColCount
{
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>
/// 獲得某行某列的數據
/// row:行,row = 1代表第一行
/// col:列,col = 1代表第一列
/// </summary>
public string this[int row, int col]
{
get
{
//數據有用性驗證
CheckRowValid(row);
CheckColValid(col);
ArrayList colAL = (ArrayList)this.rowAL[row - 1];
//假如要求列數據年夜於以後行的列時,前往空值
if (colAL.Count < col)
{
return "";
}
return colAL[col - 1].ToString();
}
}
/// <summary>
/// 依據最小行,最年夜行,最小列,最年夜列,來生成一個DataTable類型的數據
/// 行等於1代表第一行
/// 列等於1代表第一列
/// maxrow: -1代表最年夜行
/// maxcol: -1代表最年夜列
/// </summary>
public DataTable this[int minRow, int maxRow, int minCol, int maxCol]
{
get
{
//數據有用性驗證
CheckRowValid(minRow);
CheckMaxRowValid(maxRow);
CheckColValid(minCol);
CheckMaxColValid(maxCol);
if (maxRow == -1)
{
maxRow = RowCount;
}
if (maxCol == -1)
{
maxCol = ColCount;
}
if (maxRow < minRow)
{
throw new Exception("最年夜行數不克不及小於最小行數");
}
if (maxCol < minCol)
{
throw new Exception("最年夜列數不克不及小於最小列數");
}
DataTable csvDT = new DataTable();
int i;
int col;
int row;
//增長列
for (i = minCol; i <= maxCol; i++)
{
csvDT.Columns.Add(i.ToString());
}
for (row = minRow; row <= maxRow; row++)
{
DataRow csvDR = csvDT.NewRow();
i = 0;
for (col = minCol; col <= maxCol; col++)
{
csvDR[i] = this[row, col];
i++;
}
csvDT.Rows.Add(csvDR);
}
return csvDT;
}
}
/// <summary>
/// 檢討行數能否是有用的
/// </summary>
/// <param name="col"></param>
private void CheckRowValid(int row)
{
if (row <= 0)
{
throw new Exception("行數不克不及小於0");
}
if (row > RowCount)
{
throw new Exception("沒有以後行的數據");
}
}
/// <summary>
/// 檢討最年夜行數能否是有用的
/// </summary>
/// <param name="col"></param>
private void CheckMaxRowValid(int maxRow)
{
if (maxRow <= 0 && maxRow != -1)
{
throw new Exception("行數不克不及等於0或小於-1");
}
if (maxRow > RowCount)
{
throw new Exception("沒有以後行的數據");
}
}
/// <summary>
/// 檢討列數能否是有用的
/// </summary>
/// <param name="col"></param>
private void CheckColValid(int col)
{
if (col <= 0)
{
throw new Exception("列數不克不及小於0");
}
if (col > ColCount)
{
throw new Exception("沒有以後列的數據");
}
}
/// <summary>
/// 檢討檢討最年夜列數能否是有用的
/// </summary>
/// <param name="col"></param>
private void CheckMaxColValid(int maxCol)
{
if (maxCol <= 0 && maxCol != -1)
{
throw new Exception("列數不克不及等於0或小於-1");
}
if (maxCol > ColCount)
{
throw new Exception("沒有以後列的數據");
}
}
/// <summary>
/// 載入CSV文件
/// </summary>
private void LoadCsvFile()
{
//對數據的有用性停止驗證
if (this.fileName == null)
{
throw new Exception("請指定要載入的CSV文件名");
}
else if (!File.Exists(this.fileName))
{
throw new Exception("指定的CSV文件不存在");
}
else
{
}
if (this.encoding == null)
{
this.encoding = Encoding.Default;
}
StreamReader sr = new StreamReader(this.fileName, this.encoding);
string csvDataLine;
csvDataLine = "";
while (true)
{
string fileDataLine;
fileDataLine = sr.ReadLine();
if (fileDataLine == null)
{
break;
}
if (csvDataLine == "")
{
csvDataLine = fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
}
else
{
csvDataLine += "\\r\\n" + fileDataLine;//GetDeleteQuotaDataLine(fileDataLine);
}
//假如包括偶數個引號,解釋該行數據中湧現回車符或包括逗號
if (!IfOddQuota(csvDataLine))
{
AddNewDataLine(csvDataLine);
csvDataLine = "";
}
}
sr.Close();
//數據行湧現奇數個引號
if (csvDataLine.Length > 0)
{
throw new Exception("CSV文件的格局有毛病");
}
}
/// <summary>
/// 獲得兩個持續引號釀成單個引號的數據行
/// </summary>
/// <param name="fileDataLine">文件數據行</param>
/// <returns></returns>
private string GetDeleteQuotaDataLine(string fileDataLine)
{
return fileDataLine.WordStr("\\"\\"", "\\"");
}
/// <summary>
/// 斷定字符串能否包括奇數個引號
/// </summary>
/// <param name="dataLine">數據行</param>
/// <returns>為奇數時,前往為真;不然前往為假</returns>
private bool IfOddQuota(string dataLine)
{
int quotaCount;
bool oddQuota;
quotaCount = 0;
for (int i = 0; i < dataLine.Length; i++)
{
if (dataLine[i] == '\\"')
{
quotaCount++;
}
}
oddQuota = false;
if (quotaCount % 2 == 1)
{
oddQuota = true;
}
return oddQuota;
}
/// <summary>
/// 斷定能否以奇數個引號開端
/// </summary>
/// <param name="dataCell"></param>
/// <returns></returns>
private bool IfOddStartQuota(string dataCell)
{
int quotaCount;
bool oddQuota;
quotaCount = 0;
for (int i = 0; i < dataCell.Length; i++)
{
if (dataCell[i] == '\\"')
{
quotaCount++;
}
else
{
break;
}
}
oddQuota = false;
if (quotaCount % 2 == 1)
{
oddQuota = true;
}
return oddQuota;
}
/// <summary>
/// 斷定能否以奇數個引號開頭
/// </summary>
/// <param name="dataCell"></param>
/// <returns></returns>
private bool IfOddEndQuota(string dataCell)
{
int quotaCount;
bool oddQuota;
quotaCount = 0;
for (int i = dataCell.Length - 1; i >= 0; i--)
{
if (dataCell[i] == '\\"')
{
quotaCount++;
}
else
{
break;
}
}
oddQuota = false;
if (quotaCount % 2 == 1)
{
oddQuota = true;
}
return oddQuota;
}
/// <summary>
/// 參加新的數據行
/// </summary>
/// <param name="newDataLine">新的數據行</param>
private void AddNewDataLine(string newDataLine)
{
//System.Diagnostics.Debug.WriteLine("NewLine:" + newDataLine);
////return;
ArrayList colAL = new ArrayList();
string[] dataArray = newDataLine.Split(',');
bool oddStartQuota; //能否以奇數個引號開端
string cellData;
oddStartQuota = false;
cellData = "";
for (int i = 0; i < dataArray.Length; i++)
{
if (oddStartQuota)
{
//由於後面用逗號朋分,所以要加上逗號
cellData += "," + dataArray[i];
//能否以奇數個引號開頭
if (IfOddEndQuota(dataArray[i]))
{
colAL.Add(GetHandleData(cellData));
oddStartQuota = false;
continue;
}
}
else
{
//能否以奇數個引號開端
if (IfOddStartQuota(dataArray[i]))
{
//能否以奇數個引號開頭,不克不及是一個雙引號,而且不是奇數個引號
if (IfOddEndQuota(dataArray[i]) && dataArray[i].Length > 2 && !IfOddQuota(dataArray[i]))
{
colAL.Add(GetHandleData(dataArray[i]));
oddStartQuota = false;
continue;
}
else
{
oddStartQuota = true;
cellData = dataArray[i];
continue;
}
}
else
{
colAL.Add(GetHandleData(dataArray[i]));
}
}
}
if (oddStartQuota)
{
throw new Exception("數據格局有成績");
}
this.rowAL.Add(colAL);
}
/// <summary>
/// 去失落格子的首尾引號,把雙引號釀成單引號
/// </summary>
/// <param name="fileCellData"></param>
/// <returns></returns>
private string GetHandleData(string fileCellData)
{
if (fileCellData == "")
{
return "";
}
if (IfOddStartQuota(fileCellData))
{
if (IfOddEndQuota(fileCellData))
{
return fileCellData.Substring(1, fileCellData.Length - 2).WordStr("\\"\\"", "\\""); //去失落首尾引號,然後把雙引號釀成單引號
}
else
{
throw new Exception("數據引號沒法婚配" + fileCellData);
}
}
else
{
//斟酌形如"" """" """"""
if (fileCellData.Length > 2 && fileCellData[0] == '\\"')
{
fileCellData = fileCellData.Substring(1, fileCellData.Length - 2).WordStr("\\"\\"", "\\""); //去失落首尾引號,然後把雙引號釀成單引號
}
}
return fileCellData;
}
}
願望本文所述對年夜家的C#法式設計有所贊助。