Word/Excel表格转换成HTML/CALS表格的问题

本文介绍如何从剪贴板获取包含单元格合并信息的表格数据,并通过示例展示了如何利用C#语言读取HTMLFormat格式的内容,适用于从Excel复制表格到其他应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将Word或者Excel里的表格拷贝到剪贴板里,数据是使用tab和回车分割的。可以编写一个程序来分析剪贴板的数据,转换成HTML和CALS表格。这在没有合并单元格的情况下工作良好。

在剪贴板数据中,没有发现表示单元格合并的信息。如果在数据中有合并的单元格,以上程序不能处理。

如果从Excel中复制一个表格(带单元格合并),然后粘贴到Word中,合并的单元格被正确地粘贴到Word中。从这种结果猜测,从Excel到Word的复制、粘贴过程中,程序是能取到单元格合并的信息的。是不是它使用了有别于windows的拷贝、粘贴的方法。

朋友们有什么好办法?

 

 

================================================================================

在剪贴板里有很多域,在Excel,Word或者IE里进行拷贝时,这些程序在HTML Format里放了完整的内容(包括表格列合并的信息),可以通过分析这些内容来进行下一步处理。

取得剪贴板HTML Format域内容的C#代码:

using System.Windows.Forms;

IDataObject iData = Clipboard.GetDataObject();
object obj = null;
obj = iData.GetData("HTML Format");
String rtn =obj.ToString();



示例值

Version:1.0
StartHTML:0000000105
EndHTML:0000002328
StartFragment:0000001917
EndFragment:0000002276

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 12">
<link id=Main-File rel=Main-File
href="file:///C:/DOCUME~1/LJINGJ~1.INF/LOCALS~1/Temp/msohtmlclip1/01/clip.htm">
<link rel=File-List
href="file:///C:/DOCUME~1/LJINGJ~1.INF/LOCALS~1/Temp/msohtmlclip1/01/clip_filelist.xml">
<style>
<!--table
{mso-displayed-decimal-separator:"/.";
mso-displayed-thousand-separator:"/,";}
@page
{margin:.75in .7in .75in .7in;
mso-header-margin:.3in;
mso-footer-margin:.3in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
td
{padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:black;
font-size:11.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:瀹嬩綋;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:middle;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:瀹嬩綋;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
</head>

<body link=blue vlink=purple>

<table border=0 cellpadding=0 cellspacing=0 width=144 style='border-collapse:
collapse;width:108pt'>
<!--StartFragment-->
<col width=72 span=2 style='width:54pt'>
<tr height=18 style='height:13.5pt'>
  <td height=18 align=right width=72 style='height:13.5pt;width:54pt'>11 </td>
  <td align=right width=72 style='width:54pt'>12 </td>
</tr>
<tr height=18 style='height:13.5pt'>
  <td height=18 align=right style='height:13.5pt'>21 </td>
  <td align=right>22 </td>
</tr>
<!--EndFragment-->
</table>

</body>

</html>

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值