2

I have a Classic ASP script that outputs an HTML table as an XLS file but have had no luck getting a carriage return/line feed to work within a single cell.

For testing I am using code based on Kristof's response to How to output an Excel *.xls file from classic ASP

I've tried every method I know of for inserting a carriage return/line feed. My sample code is as follows:

<%@ Language=VBScript %>
<%  Option Explicit
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-Disposition", "attachment; filename=excelTest.xls"
%>
<table border="1">
    <tr>
        <td>Line1<br />Line2</td>
        <td><p>Line1</p>Line2</td>
        <td><div>Line1</div>Line2</td>
        <td>Line1<%=chr(10)%>Line2</td>
        <td>Line1<%=vbCR%>Line2</td>
        <td>Line1<%=vbLF%>Line2</td>
        <td>Line1<%=vbCRLF%>Line2</td>
        <td>Line1\rLine2</td>
        <td>Line1\nLine2</td>
    </tr>
</table>

I am opening the XLS in Excel 2010. The first 3 examples using HTML tags show the carriage return but split the data across 2 rows. The rest of the examples show the data on one line. Is there any way to populate an Excel field using Classic ASP with a carriage return without it splitting into multiple rows?

EDIT:

To clarify, I am trying to replicate the behavior of typing alt-enter within a cell in Excel. In the screenshot below you can see that the first 3 examples are splitting the data into two cells (rows 1 & 2) while the rest all appear on one line. I am trying to achieve two lines of text in one cell. Is there any other code I can use to insert a line break but keep the data in one cell?

Excel Screenshot

Community
  • 1
  • 1
Code Monkey
  • 335
  • 3
  • 8

1 Answers1

2

Is there is some confusion between the display of mark-up and output of carriage returns in your code.

The markup display gives the appearance of carriage returns for the first three td's

enter image description here

and the fourth, sixth and seventh td's contain carriage returns in the html source

enter image description here

EDIT asp per comment and screenshot supplied

Add the following style to your page and you can see the first cell behaving as desired.

<style>
<!--table
br {mso-data-placement:same-cell;}
tr {vertical-align:top;}
-->
</style>
Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • I was listing all of the code I've tried to render a line break within an Excel cell. None of the methods I've tried have worked. I've added a link to a screenshot for clarification. The first 3 examples with markup break the text onto a new line, but also into a new cell. I need the text to stay in the same cell. – Code Monkey Feb 07 '12 at 13:48
  • That's perfect! Didn't have much hope for resolving this considering how dated the code is. THANKS! – Code Monkey Feb 08 '12 at 20:44
  • No probs, classic asp just won't leave the stage - don't forget to vote it up ;-) – Nicholas Murray Feb 08 '12 at 22:26