Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste a HTML table into Excel, how to keep the line break in cell

Tags:

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> 

I tried to add css style

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

But it only works in IE.

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

like image 252
virsir Avatar asked Oct 08 '12 02:10

virsir


People also ask

How do I paste HTML data into Excel?

Copy html data. Paste it in excel (data looks scattered ,don't panic) At the end of excel you will paste option. Click on "Text import wizard" and click on Delimited option and then select "tab" as delimiter and data will correctly formatted.

How do you paste in Excel without line breaks?

Select the cell you want to copy, press the F2 key or double click to get into the edit mode, select the cell content and press the Ctrl + C keys to copy it. 2. Open the TXT file or other files you will paste the content, then press the Ctrl + V keys.

How do you enter a line break into one of your cells data?

To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.


1 Answers

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.AppendHeader(“content-disposition“, “inline; filename=report.xls“); 

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.

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.

add into a stylesheet:

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

Then it works like a charm. I hope it useful :)

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

like image 195
Himanshu padia Avatar answered Sep 30 '22 19:09

Himanshu padia