Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I have a WPF datagrid. Also I have a method for copying the grid values.

After copying the values from grid, the line break getting removed from the column content after pasting in to excel.


Regards,
Arunkumar Murugesan

What I have tried:

The below code I'm using for set the grid values as clipboard data.

<pre><pre>if (SelectedRows != null && SelectedRows.Count > 0)
            {
                string Values = "<TABLE><TBODY>";
                string Headers = "";
                if (string.IsNullOrEmpty(Headers))
                {
                    Values += "<TR>";

                    foreach (DataColumn column in DynamicBindingTable.Columns)
                    {
                        Values += "<TD>" + column.ColumnName + "</TD>";
                    }
                    Values += "</TR>";
                }
                for (int i = 0; i < CopiedRows.Count; i++)
                {
                    Values += "<TR>";
                    for (int j = 0; j < CopiedRows[i].ItemArray.Length; j++)
                    {
                        object dynamicValues = CopiedRows[i][j].ToString();
                        Type columnType = DynamicBindingTable.Columns[j].DataType;
                        if (dynamicValues != null)
                        {
                            Values += "<TD>" + dynamicValues.ToString() + "</TD>";
                        }
                    }
                    Values += "</TR>";
                }
                Values += "</TBODY></TABLE>";
                Clipboard.SetData(DataFormats.UnicodeText, (object)Values);
            }


The values are setting into clipboard data. But after pasting the values into excel the line breaks has removed in the content.

For Example, in Grid one column value as,

Dear Customer,
In case you require specific free time deal at destination to be listed. 

Failure to receive confirmation by comp

Free time is considered invalid.

We regret to inform you that we do not have available content.


But after pasting into excel it showing as below(single text)

Dear Customer,In case you require specific free time deal at destination to be listed. Failure to receive confirmation by comp.Free time is considered invalid


Could you please help me resolve this problem?
Posted
Updated 25-Apr-20 5:10am
Comments
Maciej Los 25-Apr-20 10:35am    
What you do is NOT an export to Excel format, but html format. In that case, you have to you detect where a break line is, then to insert <br/> in that place.
[no name] 25-Apr-20 10:51am    
And most probably to add some style information like <br style="mso-data-placement:same-cell;"/>
Maciej Los 25-Apr-20 11:05am    
:thumbsup:
[no name] 25-Apr-20 11:21am    
Thanks and sorry that I stolen now the answer from you :-)
Maciej Los 25-Apr-20 11:32am    
:DCheers!

1 solution

Like Maciej mentioned in the comment to the question you need to replace linenbreaks (linefeeds resp carriage linefeeds) with
<br>

But it is not only a simple <br> you have to add. To inform Excel to keep it in the same cell you need to replace linebreaks with
<br style='mso-data-placement: same-cell'>
For examlpe
<td>Jan<br style='mso-data-placement: same-cell'>uary</td>

I hope it helps.

[Edit1]
Finally a better option with 'embedded' style
<style>
  br {mso-data-placement:same-cell;}
</style>
<table>
  <tr>
    <th>Col 1</th>
    <th>Col 2</th>
  </tr>
  <tr>
    <td>Line1 <br>Line 2</td>
    <td>data for col 2</td>
  </tr>
</table>

[Edit2]
To allow to have <br> and also something like &lt;br&gt; in the source text you need to encode the source text with HtmlEncode. More info about HtmlEncode you will find e.g. here WebUtility.HtmlEncode Method (System.Net) | Microsoft Docs[^]

[Edit 3]
Yet another thing. In case there is a chance that a lot of data are in your grid you should think about using StringBuilder instead of concatenating strings.

I remember a test I made before about 10 years.
Concatenating 50'000 times a string of 10 characters in a loop took 25 Sec. while with StringBuilder it took '0 Seconds' (measured with Environement.TickCount).
 
Share this answer
 
v6
Comments
Maciej Los 25-Apr-20 11:31am    
5ed!
[no name] 25-Apr-20 11:36am    
Thank you Maciej
Patrice T 26-Apr-20 11:30am    
+5
[no name] 26-Apr-20 11:32am    
Thank you Patrice

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900