Classic ASP XLS output with carriage return in cel

2019-02-26 19:59发布

问题:

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

回答1:

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

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

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>