将HTML表粘贴到Excel中,如何在单元格中保留换行符 [英] Paste a HTML table into Excel, how to keep the line break in cell

查看:688
本文介绍了将HTML表粘贴到Excel中,如何在单元格中保留换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的html表,例如,只有一个单元格,但是当我复制dom节点,并将其粘贴到excel,它将被识别为两行,如何使Excel获得正确的粘贴数据。 / p>

I have a simple html table, for example, just one cell, but when I copy the dom node, and paste it into excel, it will be recognize as two rows, How to make Excel get the correct paste data.

 <table><tr><td>1<br>2</td><tr></table>

我尝试添加css样式

br {mso-data-placement:same-cell;},

但是它只能在IE中使用。

But it only works in IE.

注意,复制一个纯文本是不行的,我需要在单元格上添加颜色,字体信息。

Note, copy a plain text out is not OK, i need to add color, font information on cells.

推荐答案

很多人可能知道,你可以将数据(例如报表)作为Excel文件输出,只需添加正确的内容 - type and content-disposition header:

As many of you probably know, you can output data (a report, for example) as an Excel file, simply by adding right content-type and content-disposition header:

Response.ContentType =application / vnd.ms-excel;

Response.ContentType = "application/vnd.ms-excel";

Response.AppendHeader(content-disposition,inline; filename = report.xls);

Response.AppendHeader("content-disposition", "inline; filename=report.xls");

如果客户端安装了MS Excel,在其中打开而不是Web浏览器。 Excel将解释所有格式化(边框,字体等)和TABLE标签,这可以产生一个不错的,格式化的工作表,而不使用重量级的服务器端控件。

If client has MS Excel installed, your output HTML page will be opened in it instead of web browser. Excel will interpret all formating (borders, fonts etc.) and TABLE tags, which can result a nice, formated worksheet, without using heavyweight server-side controls.

我一直在努力一段时间是多行单元格。我需要在单元格中换行文本,但是当我把< br> 标记插入到HTML输出中时,Excel会将其解释为新行,而不是现有单元格中的换行符

The problem I was struggling for some time was with multi-line cells. I needed to wrap text in cell, but when I put <br> tag into HTML output, Excel interpreted it as a new row, not a line-break in existing cell.

添加到样式表中:

br {mso-data-placement:same-cell;}

我希望它有用:)

提示:您可以使ContentType和标题有条件,以一个文件提供备用HTML / XLS报告。

Tip: You can make ContentType and header conditional, providing alternate HTML/XLS reports with one file.

这篇关于将HTML表粘贴到Excel中,如何在单元格中保留换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆