/// 解析/// </summary>
/// <param name="Source">csv字符串</param>
/// <returns></returns>
public ArrayList SplitCSV(string csvStr)
{
//获取联系人记录内容
#region
int sp = csvStr.IndexOf("\r\n");
string head = csvStr.Substring(0, sp - 1);
string record = csvStr.Substring(sp + 2);
#endregion
char[] s = record.ToCharArray();
System.Text.StringBuilder strCol = new System.Text.StringBuilder();
ArrayList arLne = new ArrayList();
ArrayList arAll = new ArrayList();
int cnter = 0; //双引号计数器
for ( int i = 0 ; i < s.Length ; i++)
{
if ( s[i] == '\"') //遇到双引号(字段结束符)
{
cnter++; //计数器加一
strCol.Append(s[i]); //此引号加入当前列
}
else if ( s[i] == ',') //遇到逗号(列结束符)
{
if (IsColumeOver(cnter)) //此逗号是列结束符,计数器清零,将当前列加入当前行
{
cnter = 0;
arLne.Add(strCol);
strCol = new System.Text.StringBuilder();
}
else //此逗号不是列结束符,逗号加入当前列
{
strCol.Append(s[i]);
}
}
else if (s[i] == '\r') //遇到回车(行结束符)
{
if (IsLineOver(cnter)) //此回车是行结束符,计数器清零,将当前行加入结果数组
{
cnter = 0;
arAll .Add(arLne);
arLne = new ArrayList();
}
else //此回车不是行结束符,\r回车符加入当前列
{
strCol.Append(s[i]);
}
}
else //正常数据(非字段、列、行结束符)
{
strCol.Append(s[i]);
}
}
return arAll ;
}
/// <summary>
/// 当前列是否已读完
/// </summary>
/// <param name="cnter"></param>
/// <returns></returns>
private bool IsColumeOver(double cnter)
{
return System.Math.IEEERemainder((double)cnter,2) == 0;
}
/// <summary>
/// 当前行是否已读完
/// </summary>
/// <param name="cnter"></param>
/// <returns></returns>
private bool IsLineOver(double cnter)
{
return System.Math.IEEERemainder((double)cnter,2) == 0;
}