views:

638

answers:

2

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".

+1  A: 

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.

Gary McGill
Good idea. That really helped me to figure out a couple of things ... except the column width.
Aaron Digulla
+1  A: 

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.

richardtallent
Can you post a link with an explanation of that format?
Aaron Digulla
http://msdn.microsoft.com/en-us/library/bb226687(office.11).aspxhttp://msdn.microsoft.com/en-us/library/bb226693(office.11).aspx
Edo