給你加了一個子函式
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
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
mydr = mydt.NewRow();
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();
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)
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
{
mydc = new DataColumn(aryline[i]);
mydt.Columns.Add(mydc);
}
}
mydr = mydt.NewRow();
for (int i = 0; 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
{
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));
}