Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi...
data convert to .csv file to list and change of data in .csv file then automatically data can be change in database.
i have a code but it is very difficult so please send me to code easy to understand

code is here......
C#
public List<list><string>> CsvToList(string content)
{
    List<list><string>> records = new List<list><string>>();
    try
    {
        StringReader stringReader = new StringReader(content);

        bool inQoutedString = false;
        List<string> record = new List<string>();
        StringBuilder fieldBuilder = new StringBuilder();
        while (stringReader.Peek() != -1)
        {
            char readChar = (char)stringReader.Read();

            if (readChar == '\n' || (readChar == '\r' && stringReader.Peek() == '\n'))
            {
                // If it's a \r\n combo consume the \n part and throw it away.
                if (readChar == '\r')
                {
                    stringReader.Read();
                }

                if (inQoutedString)
                {
                    if (readChar == '\r')
                    {
                        fieldBuilder.Append('\r');
                    }
                    fieldBuilder.Append('\n');
                }
                else
                {
                    record.Add(fieldBuilder.ToString().TrimEnd());
                    fieldBuilder = new StringBuilder();

                    records.Add(record);
                    record = new List<string>();

                    inQoutedString = false;
                }
            }
            else if (fieldBuilder.Length == 0 && !inQoutedString)
            {
                if (char.IsWhiteSpace(readChar))
                {
                    // Ignore leading whitespace
                }
                else if (readChar == '"')
                {
                    inQoutedString = true;
                }
                else if (readChar == ',')
                {
                    record.Add(fieldBuilder.ToString().TrimEnd());
                    fieldBuilder = new StringBuilder();
                }
                else
                {
                    fieldBuilder.Append(readChar);
                }
            }
            else if (readChar == ',')
            {
                if (inQoutedString)
                {
                    fieldBuilder.Append(',');
                }
                else
                {
                    record.Add(fieldBuilder.ToString().TrimEnd());
                    fieldBuilder = new StringBuilder();
                }
            }
            else if (readChar == '"')
            {
                if (inQoutedString)
                {
                    if (stringReader.Peek() == '"')
                    {
                        stringReader.Read();
                        fieldBuilder.Append('"');
                    }
                    else
                    {
                        inQoutedString = false;
                    }
                }
                else
                {
                    fieldBuilder.Append(readChar);
                }
            }
            else
            {
                fieldBuilder.Append(readChar);
            }
        }
        record.Add(fieldBuilder.ToString().TrimEnd());
        records.Add(record);
    }
    catch (Exception ex)
    {
        base.LogException("UploadCsvFile", "CsvToList", ex);
    }

    return records;
}
Posted
Updated 25-Oct-13 23:12pm
v2
Comments
Richard MacCutchan 26-Oct-13 5:13am    
You need to explain what the problem is. No one is going to send you code just like that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900