0

I'm having some problems converting an excel file to csv format with C# and NPOI library. I have to return an error in case there is a multiline cell like the following


|"" | |text| |text|

The output of the csv file is expected to be: "", text, text,

How can I detect it and throw an error ?

            for (int row = 0; row <= sheet.LastRowNum; row++)
        {
            for (int cell = 0; cell < sheet.GetRow(row).Cells.Count ; cell++)
            {     
                csvData += sheet.GetRow(row).Cells[cell].ToString() + ",";                
            }
            csvData += "\n";
        }
  • Not to be pedantic, but you don't throw an error, you throw an `Exception`. It isn't the best way to deal with flow of control. Is this multiline condition truly exceptional? Maybe you could return an error code, or log the line numbers of bad lines, and allow the program to continue. – John Wu Dec 04 '19 at 07:52
  • yes, exactly.. but i can't figure out how to detect if the string contains '\n' –  Dec 04 '19 at 08:29
  • Have you tried `myString.Contains("\n")`? – John Wu Dec 04 '19 at 08:38
  • yes, doesn't work. i have converted that file from excel to a csv data, but i'm unable to check for a multiline cell and throw an exception –  Dec 04 '19 at 08:46
  • Please edit your question to include the code that you have tried and explain what you mean by "doesn't work." – John Wu Dec 04 '19 at 08:50
  • "doesn't work" mean "myString.Contains("\n")" return always false –  Dec 04 '19 at 08:58
  • You code example does not contain any reference to `Contains`. Please provide a [MCVE]. – John Wu Dec 04 '19 at 09:01

0 Answers0