c# 读取csv文件 内容含逗号
#region读Csv,静态方法///<summary>///静态方法,读取规则的2维表的Csv成DataSet///</summary>///<paramname="f...
#region 读Csv,静态方法
/// <summary>
/// 静态方法,读取规则的2维表的Csv成DataSet
/// </summary>
/// <param name="fileFullPath">全路径,包括文件名</param>
/// <returns>DataSet</returns>
public static DataTable Csv2DataSet(string fileFullPath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strpath = fileFullPath; //cvs文件路径
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.Default);
while ((strline = mysr.ReadLine()) != null)
{
aryline = strline.Split(new char[] { ',' });
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
#endregion
这段代码没有考虑到内容有逗号的情况,遇到内容含逗号的就不对了,所以各位大神请帮忙在这段代码里面加一些代码,完成这个功能
DEMO-00000001159,1,A212110002,"Binding Rings 13mm (80 Sheets, 100 s)",3,EA,30,2014-8-6 比如这行代码,双引号中间的逗号 是不能被分割的,也就是说 "Binding Rings 13mm (80 Sheets, 100 s)" 这是一段内容,不是两段 展开
/// <summary>
/// 静态方法,读取规则的2维表的Csv成DataSet
/// </summary>
/// <param name="fileFullPath">全路径,包括文件名</param>
/// <returns>DataSet</returns>
public static DataTable Csv2DataSet(string fileFullPath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strpath = fileFullPath; //cvs文件路径
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.Default);
while ((strline = mysr.ReadLine()) != null)
{
aryline = strline.Split(new char[] { ',' });
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
#endregion
这段代码没有考虑到内容有逗号的情况,遇到内容含逗号的就不对了,所以各位大神请帮忙在这段代码里面加一些代码,完成这个功能
DEMO-00000001159,1,A212110002,"Binding Rings 13mm (80 Sheets, 100 s)",3,EA,30,2014-8-6 比如这行代码,双引号中间的逗号 是不能被分割的,也就是说 "Binding Rings 13mm (80 Sheets, 100 s)" 这是一段内容,不是两段 展开
4个回答
展开全部
#region 读Csv,静态方法
/// <summary>
/// 静态方法,读取规则的2维表的Csv成DataSet
/// </summary>
/// <param name="fileFullPath">全路径,包括文件名</param>
/// <returns>DataSet</returns>
public static DataTable Csv2DataSet(string fileFullPath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strpath = fileFullPath; //cvs文件路径
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.Default);
while ((strline = mysr.ReadLine()) != null)
{
List<string> ss = new List<string>();
aryline = strline.Split(new char[] { ',' });
if (strline.IndexOf("\"") > -1)
{
for (int i = 0; i < aryline.Length; i++)
{
if (aryline[i].Contains("\""))
{
ss.Add(aryline[i] + "," + aryline[++i]);
if (i == aryline.Length - 1)
{
break;
}
}
else
{
ss.Add(aryline[i]);
}
}
}
else
{
ss.AddRange(aryline);
}
if (blnFlag)
{
blnFlag = false;
intColCount = ss.Count;
for (int i = 0; i < intColCount; i++)
{
mydc = new DataColumn(ss[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = ss[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
#endregion
2014-09-15
展开全部
先做处理双引号,再做逗号的处理。
否则,把双引号里的逗号先替换其它符合,再处理,最后再换回来。
否则,把双引号里的逗号先替换其它符合,再处理,最后再换回来。
追问
能写出来吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
参考:网页链接
亲测可行
using Microsoft.VisualBasic.FileIO;
using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv"))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
//Processing row
string[] fields = parser.ReadFields();
foreach (string field in fields)
{
//TODO: Process field
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你加了一个子函数
public static DataTable Csv2DataSet(string fileFullPath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strpath = fileFullPath; //cvs文件路径
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.Default);
while ((strline = mysr.ReadLine()) != null)
{
aryline = strToAry(strline);//请注意:此处变了
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
// 请注意:以下为新添加的子函数
private static string[] strToAry(string strLine)
{
string strItem = "";
int iFenHao = 0;
System.Collections.ArrayList lstStr = new System.Collections.ArrayList();
for (int i = 0; i < strLine.Length; i++)
{
string strA = strLine.Substring(i, 1);
if (strA == "\"")
{
iFenHao = iFenHao + 1;
}
if (iFenHao == 2)
{
iFenHao = 0;
}
if (strA == "," && iFenHao == 0)
{
lstStr.Add(strItem);
strItem = "";
}
else
{
strItem = strItem + strA;
}
}
if (strItem.Length > 0)
lstStr.Add(strItem);
return (String[])lstStr.ToArray(typeof(string));
}
public static DataTable Csv2DataSet(string fileFullPath)
{
int intColCount = 0;
bool blnFlag = true;
DataTable mydt = new DataTable("myTableName");
DataColumn mydc;
DataRow mydr;
string strpath = fileFullPath; //cvs文件路径
string strline;
string[] aryline;
System.IO.StreamReader mysr = new System.IO.StreamReader(strpath, Encoding.Default);
while ((strline = mysr.ReadLine()) != null)
{
aryline = strToAry(strline);//请注意:此处变了
if (blnFlag)
{
blnFlag = false;
intColCount = aryline.Length;
for (int i = 0; i < aryline.Length; i++)
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; i < intColCount; i++)
{
mydr[i] = aryline[i];
}
mydt.Rows.Add(mydr);
}
return mydt;
}
// 请注意:以下为新添加的子函数
private static string[] strToAry(string strLine)
{
string strItem = "";
int iFenHao = 0;
System.Collections.ArrayList lstStr = new System.Collections.ArrayList();
for (int i = 0; i < strLine.Length; i++)
{
string strA = strLine.Substring(i, 1);
if (strA == "\"")
{
iFenHao = iFenHao + 1;
}
if (iFenHao == 2)
{
iFenHao = 0;
}
if (strA == "," && iFenHao == 0)
{
lstStr.Add(strItem);
strItem = "";
}
else
{
strItem = strItem + strA;
}
}
if (strItem.Length > 0)
lstStr.Add(strItem);
return (String[])lstStr.ToArray(typeof(string));
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询