c# - Format a string to display properly on Excel

08
2014-07
  • Raj

    Guys I have a string which contains COMMA(,) , DOT(.) , MULTIPLE SPACES CHARACTERS etc.

    I am (through my ExportToExcel function) trying to display this string in a cell of an excel sheet. However due to these characters my display is not proper as apparently these cause text of my string to change column.

    What I did till now ?

    I tried LENGTHY and INEFFECTIVE technique to split the string and include quotes and all eg. of one of my UNSUCCESSFUL trial

                        if (o.Description.Contains(','))
                        {
                            StringBuilder sb = new StringBuilder();
                            var splits = o.Description.Split(',');
                            foreach (var split in splits)
                            {
                                sb.Append('"');
                                sb.Append(split);
                                sb.Append(',');
                            }
                            var desc = sb.ToString();
                            desc = desc.Remove(desc.Length - 1);
                            desc = desc + '"';
    
                            // Handling multiple spaces.
                            desc = Regex.Replace(desc, "[\n\r\t]", " ");
                            desc = Regex.Replace(desc, "[\n]", " ");
                            desc = Regex.Replace(desc, "[\r\n]", " ");
    
    
                            dtRow[(int)ProductRangeExportToExcel.Description] = desc;
                            //handle issues hre
                        }
    

    I want a consice way to acheive this :

    How can I format my string in C# so as to display it properly in a cell of the excel sheet ?

  • Answers
    Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

    Related Question


    Related Answers