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.
If you really need to do a copy-paste, it would be better to edit the 'Cell properties' in the excel sheet before copying the data. Right-Click on the column name in the excel and select cell properties. Make the changes as applicable for your data and save. Now you can copy your data and hope that it is interpreted correctly in MS-Excel. I haven't used it for HTML content, though.
What about using a macro with e.g. the following code?
•
TransformingPaste1()
: however, this fails on my machine (still multiple cells)mso-data-placement:same-cell
•
TransformingPaste2()
: pastes in single cell on my machine, keeping the formatting etc., but results in a space rather than a newline because you're still pasting HTMLvbCrLf
•
TransformingPaste3()
: pastes in single cell on my machine, with a newline, but loses the formatting etc. (current implementation) – see note with link!Modify to what best suits your needs, e.g. using regex to perform the replacements, but I hope this gets you on your way :]
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:
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:
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.
It looks like Firefox entirely ignores the mso-data-placement directive; it doesn't appear in the Firebug style panel. Perhaps that's why it won't paste as you expect?