读取CSV文件包含逗号字符串

时间:2014-03-18 12:30:57

标签: c# csv datatable

我有一个csv文件,它有许多这种格式的字符串文件:例如" 1,125" 当我读Csv时这些字符串是空的。

string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = @"SELECT * FROM [" + fileName + "]";
using (OleDbConnection connection = new OleDbConnection(
              @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
              ";Extended Properties=\"Text;HDR=" + header + "\""))
using (OleDbCommand command = new OleDbCommand(sql, connection))
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
     DataTable dataTable = new DataTable();
     dataTable.Locale = CultureInfo.CurrentCulture;
     adapter.Fill(dataTable);
}

我的CSV文件是这样的:


ABS,15,1392 / 12/12501" 1205",KTI1212," 123,5",KK

1 个答案:

答案 0 :(得分:0)

检查此代码:

try
{
    //Read the file
    System.IO.StreamReader f = new System.IO.StreamReader(fileName, System.Text.Encoding.UTF8);
    string line = f.ReadLine();

    //Split text by comma
    string[] tokens = line.Split(',');
    int tokenCount = tokens.Length;

    //Reconstruct the broken strings - tokensList retreives the final values
    System.Collections.ArrayList tokensList = new System.Collections.ArrayList();

    string token = "";
    bool stringFound = false;
    string currentToken;
    for (int i=0; i<tokenCount; i++)
    {
        currentToken = tokens[i];
        if (!stringFound)
        {   
            //Check if not a string
            if ((currentToken.Trim().StartsWith("\"") && !currentToken.Trim().EndsWith("\"")) || currentToken.Trim().Equals("\""))
            {
                stringFound = true;
                token = currentToken;
            }
            else
            {
                //Add the string as final value
                tokensList.Add(currentToken);
            }
        }
        else
        {
            //Reconstruct the string
            token += "," + currentToken;
            if (currentToken.Trim().EndsWith("\""))
            {
                int quoteIndex = currentToken.LastIndexOf("\"");
                if (quoteIndex == 0 || (quoteIndex > 0 && currentToken[quoteIndex-1] != '"'))
                {
                    tokensList.Add(token);
                    stringFound = false;
                    token = "";
                }
            }
        }
    }

}
catch (Exception ex)
{
    Console.WriteLine(ex.StackTrace);
}