replacing Alt Enter character in Excel with HTML line break in a DataList

Problem: Excel cells contain Alt+Enter line breaks. After the Excel file is imported to the SQL Server database, the line break is not showing in a datalist inside of a webpage.

Solution:

Trigger a function for the ItemDataBound event for the DataList. Replace the Alt+Enter character in Excel which is “\n” with HTML line break “<br />”

Example:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{

String temp = ((Label)e.Item.FindControl(“YourControlName”)).Text;

((Label)e.Item.FindControl(“YourControlName “)).Text = temp.Replace(“\n”, “<br />”);

}

Advertisements

2 comments so far

  1. Tarun on

    Gud one!

    I have opposite problem, I am showing data in a datagrid using tag like
    a
    b
    c
    d
    e…
    but when I exported these in excel then tag appeared in the export. how to replace those br tag again with alt+enter fuctionality for excel also…

    waiting for your reply..

  2. nycharles on

    Do you use software like sql server reporting service or integration service to export the excel file or use the code you wrote?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: