CSV文檔規則
1:數據使用逗號分隔;
2:內容中可以包括用於分隔數據的逗號,此類數據使用雙引號括起;
3:數據中如果包括雙引號,需要使用兩個雙引號;
代碼中沒有使用Remove和Substring等函數對源串進行處理,而是使用了索引來定位字符串值。
下面的代碼將CSV文件解釋出的數據存放到DataTable中以便使用。附計算的活動圖。
/// <summary>
/// csv文件解釋
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCallcsv_Click(object sender, EventArgs e)
{
FileStream fileStream = new FileStream(@"D:\temp\a.csv", FileMode.Open, FileAccess.Read, FileShare.None);
//標識讀取的是否是第一行,第一行是列名
bool columnFlag = true;
//定義新數據行
string strLine = null;
DataTable table = new DataTable();
using (StreamReader sr = new StreamReader(fileStream, System.Text.Encoding.GetEncoding("gb2312")))
{
while (!mysr.EndOfStream)
{
strLine = mysr.ReadLine();
if (!string.IsNullOrEmpty(strLine))
{
//lihx 2013年4月7日 實際上這裏會有問題,因爲CSV中,使用“”括起來的內容包括“,”的時候,逗號不被解釋爲分隔符,比如:“"2-郵局匯款,"”
//讀取第一行,作爲列名
if (columnFlag)
{
columnFlag = false;
int lineStartIndex = 0;
while (lineStartIndex < strLine.Length)
{
table.Columns.Add(getSPValue(sr, ref lineStartIndex, ref strLine));
}
}
else
{
//初始化新行,並放入table中
int itemIndex = 0;
int lineStartIndex = 0;
object[] items = new object[table.Columns.Count];
while (lineStartIndex < strLine.Length && itemIndex < items.Length)
{
items[itemIndex++] = getSPValue(sr, ref lineStartIndex, ref strLine);
}
table.Rows.Add(items);
}
}
}
}
}
}
private static string getSPValue(StreamReader sr, ref int lineStartIndex, ref string strLine)
{
int spIndex = lineStartIndex;
if (lineStartIndex >= strLine.Length)
{
return string.Empty;
}
if (strLine[spIndex] == '\"')
{
lineStartIndex++;
int tmpIndex = -1;
while ((tmpIndex = strLine.IndexOf("\"", spIndex + 1)) == -1)
{
string nextLine = sr.ReadLine();
strLine += "\n" + nextLine;
}
spIndex = tmpIndex;
if (spIndex == -1)
{
spIndex = strLine.Length;
}
}
spIndex = strLine.IndexOf(",", spIndex);
if (spIndex == -1)
{
spIndex = strLine.Length;
}
if (lineStartIndex > -1 && lineStartIndex < strLine.Length)
{
string tmpValue = strLine.Substring(lineStartIndex, spIndex - lineStartIndex);
if (tmpValue != null && tmpValue.Length > 1 && tmpValue.StartsWith("\"") && tmpValue.EndsWith("\""))
{
tmpValue = tmpValue.Substring(1, tmpValue.Length - 2);
if (tmpValue.Length > 0)
{
tmpValue = tmpValue.Replace("\"\"", "\"");
}
}
//考慮到性能不建議使用以下截串的算法。
//strline = strline.Substring(spIndex + 1);
lineStartIndex = spIndex + 1;
return tmpValue;
}
lineStartIndex = spIndex + 1;
return string.Empty;
}