通过剪贴板将 HTML 表导入 Excel
我想从我的应用程序将表格数据复制到 Excel。 到目前为止我发现的最简单的方法是使用 HTML 作为中间格式。 阅读这个问题后,我可以保留格式我的数据。 有没有办法也保持列的宽度? 我尝试通过各种方式设置样式:
<td style="width:100;">...</td>
<td style="width:100px;">...</td>
<td style="width:100pt;">...</td>
但无济于事。 有任何想法吗?
为了加分,有没有地方可以找到 Excel 的 HTML“格式”的描述?
[编辑]更新:我编写了一个小工具来转储传输类型“XML 样式表”。 这是一个独立的 XML 文档。 它还包含列宽。 但快速测试表明,即使我在两个表之间剪切和粘贴,Excel 也会完全忽略列宽:( 因此,除非有人可以告诉我更改此行为的选项,否则我想在粘贴时根本不可能设置列格式。
[EDIT2] 我找到了一种方法。粘贴后,您会在右下角看到一个小图标,看起来像工具栏中的粘贴按钮,其中一个是“保留源表的宽度”。 。
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".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在 Excel 和 HTML 之间“往返”,那么为什么不在 Excel 中创建一个简单的工作簿并将其另存为 HTML,然后检查结果呢?
编辑:删除了有关拦截“粘贴”事件的废话; 我刚刚意识到你处于事物的“复制”端,而不是“粘贴”端。
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.
似乎是一个错误,使用剪贴板不会设置列宽度。
解决方案(或解决方法)是在 Excel 中打开 HTML 文件。
以下是重现:
将其复制并粘贴到 Excel 中:
不设置列宽!
将上述 HTML 保存在文件中,然后在 Excel 中打开它,列宽设置正确。
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:
Doesn't set the Column Widths!
Save the above HTML in a file and open it in Excel the Column Widths are set correctly.
我推荐使用 XML 电子表格格式(2002/2003 年的旧格式——生成起来更简单),而不是 Excel 导入 HTML 的功能。
我已经完成了这两项工作,并编写了库来直接从 .NET 导出这两项内容,而且 XML 格式绝对不会让您失眠。
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.
不要使用“style”或“px”,例如:
do not use "style" nor "px", example: