How do I handle line breaks in a CSV file using C#?

前端 未结 14 1887
情书的邮戳
情书的邮戳 2020-12-15 08:43

I have an Excel spreadsheet being converted into a CSV file in C#, but am having a problem dealing with line breaks. For instance:

\"John\",\"23\",\"555-555         


        
14条回答
  •  执念已碎
    2020-12-15 09:16

    I've used this piece of code recently to parse rows from a CSV file (this is a simplified version):

    private void Parse(TextReader reader)
        {
            var row = new List();
            var isStringBlock = false;
            var sb = new StringBuilder();
    
            long charIndex = 0;
            int currentLineCount = 0;
    
            while (reader.Peek() != -1)
            {
                charIndex++;
    
                char c = (char)reader.Read();
    
                if (c == '"')
                    isStringBlock = !isStringBlock;
    
                if (c == separator && !isStringBlock) //end of word
                {
                    row.Add(sb.ToString().Trim()); //add word
                    sb.Length = 0;
                }
                else if (c == '\n' && !isStringBlock) //end of line
                {
                    row.Add(sb.ToString().Trim()); //add last word in line
                    sb.Length = 0;
    
                    //DO SOMETHING WITH row HERE!
    
                    currentLineCount++;
    
                    row = new List();
                }
                else
                {
                    if (c != '"' && c != '\r') sb.Append(c == '\n' ? ' ' : c);
                }
            }
    
            row.Add(sb.ToString().Trim()); //add last word
    
            //DO SOMETHING WITH LAST row HERE!
        }
    

提交回复
热议问题