1

I'm writing the program that is rewrite CSV files to the excel spreadsheet. I need two dimensional array to write text into excel, so I use File.ReadAllLines and in foreach line.Split method for it and it works perfect. The problem is, when we want to have line feed inside the excel cell - ReadAllLines is treated line feed and carriage return the same way, so we will have new line in excel also with different cell.

The question is, there is any way to override this method that we can treat LF and CR distinguish? For example, when is the LF don't create new line so the new element in list, just append it to previous string with some kind of identifier for excel.

Here is how I read the file:

public virtual List<List<string>> Read(string inFile)
    {
        var sheetData = new List<List<string>>();

            if (!File.Exists(inFile))
            {
                FileDoesNotExist(inFile);
                return sheetData;
            }
            var lines = File.ReadAllLines(inFile);

            foreach (var line in lines)
            {
                var cells = line.Split(_delimiter).ToList();
                sheetData.Add(cells);
            }

        return sheetData;

    }
reniasa
  • 183
  • 1
  • 9
  • Please include the code – M B Oct 27 '16 at 14:16
  • 2
    Parsing CSV is more than splitting at a comma. Make sure your file is properly formatted (the values must be in double quotes if they can contain an LF) and use a CSV parser, e.g. [TextFieldParser](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser(v=vs.110).aspx). – GSerg Oct 27 '16 at 15:02
  • https://www.dotnetperls.com/textfieldparser or http://stackoverflow.com/questions/6813607/parsing-csv-using-oledb-using-c-sharp – Slai Oct 31 '16 at 09:14

1 Answers1

2

No, you cannot override this behaviour.

The documentation for File.ReadAllLines(...) states:

A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed

File.ReadAllLines uses a StreamReader for its implementation, and StreamReader.ReadLine(...) is hard coded to use \r, \n or \r\n.

You could however use File.ReadAllText(...) and then split on \r, for example:

string[] lines = File.ReadAllText(inFile).Split('\r');

However bear in mind this is not actually a good way to process CSV data unless your data is very strictly created. A library such as Lumenworks CSV Reader or the TextFieldParser is more suited to "real world" data as it handles delimiters and text surrounds correctly. However I do not know of any that allow you to change the row delimiter.

Alsty
  • 817
  • 10
  • 23