Aaron Digulla
Aaron Digulla

Reputation: 328556

Importing HTML Table into Excel via clipboard

I want to copy tabular data to Excel from my app. The most simple way I found so far was using HTML as the intermediary format. After reading this question, I could preserve the formatting of my data. Is there a way to keep the width of the columns, too? I tried to set the style in various ways:

<td style="width:100;">...</td>
<td style="width:100px;">...</td>
<td style="width:100pt;">...</td>

but to no avail. Any ideas?

For bonus points, is there a place where I can find a description of Excel's HTML "format"?

[EDIT] Update: I've written a small tool to dump the transfer type "XML Stylesheet". This is a self contained XML document. It also contains column widths. But a quick test shows that Excel ignores the column widths completely, even when I cut&paste between two tables :( So unless someone can tell me an option to change this behavior, I guess it's simply not possible to format the columns while pasting.

[EDIT2] I've found a way. After pasting, you get a little icon in the lower right corner which looks like the paste button in the toolbar. Here, you can select some options. One is "Keep width of source table".

Upvotes: 4

Views: 6075

Answers (4)

Jeremy Thompson
Jeremy Thompson

Reputation: 65544

Seems like a bug, using the Clipboard doesn't set the Column Widths.

The solution (or workaround) is to Open the HTML file in Excel.

Here is a repro:

Copy this and paste it into Excel:

<html><body><center>
<table border="1" cellpadding="0" cellspacing="0">
<tr>
<td width=64><strike>testing</strike></td>
<td width=64>one</td>
<td width=233>two</td>
<td width=64>three</td>
</tr>
</table></center></body></html>

Doesn't set the Column Widths!

Save the above HTML in a file and open it in Excel the Column Widths are set correctly.

Upvotes: 1

Raul Lea&#241;o Martinet
Raul Lea&#241;o Martinet

Reputation: 2113

do not use "style" nor "px", example:

<table border="1" cellpadding="5" cellspacing="0">
<tr>
    <td width="100">test</td>
    <td width="100">test</td>
    <td width="100">test</td>
    <td width="500">test</td>
</tr>
</table>

Upvotes: 3

richardtallent
richardtallent

Reputation: 35363

I recommend the XML Spreadsheet format (the old 2002/2003 one--simpler to generate) over Excel's ability to import HTML.

I've done both and written libraries to export both directly from .NET, and the XML format is definitely less likely to make you lose sleep.

Upvotes: 5

Gary McGill
Gary McGill

Reputation: 27516

You can "round trip" from Excel to HTML and back, so why not create a simple workbook in Excel and save it as HTML, then examine the results?


EDIT: removed nonsense about intercepting the "paste" event; I just realised you're at the "copy" side of things, not the "paste" side.

Upvotes: 2

Related Questions