1

I am using Sitecore 8.0 . We have one Export of data feature using Excel. For all the fields the data is getting exported in a cell properly ,but for the Multiline text field the text is getting shifted to a next line of the cell .

Here is the code to generate excel file data:

public static string GetCSVFile(DataTable dt)
    {
        //Build the CSV file data as a Comma separated string.
            string csv = string.Empty;
            StringBuilder strbld = new StringBuilder();
            foreach (DataColumn column in dt.Columns)
            {
            //Add the Header row for CSV file.
            strbld.Append(column.ColumnName + ',');
            }

            //Add new line.
            strbld.Append("\r\n");

            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn column in dt.Columns)
                {
                    //Add the Data rows.
                    strbld.Append(row[column.ColumnName].ToString().Replace(",", ";") + ',');
                }

                //Add new line.
                strbld.Append("\r\n");
            }

            csv = strbld.ToString();
            return csv;
        
    }

Below is the screenshot of generated excel file for the multiline text field highlighted in yellow: enter image description here

Can anyone please tell how to do place this multiline text field in a single cell .

Thanks

1 Answer 1

1

To achieve this, you can use the below regular expressions code to remove all the occurrences of \n,\r (new line) or \t (tab) from the string.

string replacement = Regex.Replace(s, @"\t|\n|\r", "");

So, in your code, you can use it like below:

foreach (DataColumn column in dt.Columns)
                {
                    //Add the Data rows.
                    string colData= Regex.Replace(row[column.ColumnName].ToString(), @"\t|\n|\r", "");
                    strbld.Append(colData.Replace(",", ";") + ',');
                } 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.