c# export to excel and preserve carriage returns inside cell

B

Bryan Ax

I'm trying to fix a bug in someone else's code. He's got a string saved
in a database such as:

<xsl:variable name="Var1">
<xsl:choose>
<xsl:when test="@SomeAttribute &gt; 1'">
<xsl:value-of select="'Y'"/>
</xsl:when>
<xsl:blush:therwise>
<xsl:value-of select="'N'"/>
</xsl:blush:therwise>
</xsl:choose>
</xsl:variable>

I'm trying to export this value from the database to Excel, and
maintain the carriage returns inside a cell in Excel. I found that in
order to have the xsl appear in the cell, I had to call
Server.HtmlEncode on the value that comes back from the database, but
have not found a means to replace the \r\n that I see in my string
value in C# with a value that Excel will like to create line breaks
inside of the cell.

The code is not working directly with an Excel object, but instead is
filling a result set into a datagrid and then streaming the datagrid to
an excel workbook using:

Response.Clear();
Response.Buffer= true;
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = string.Empty;

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw);
dg.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();

Any ideas?

Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top