如何使用互操作和 C# 将行从一个表复制到 Word 2007 中的另一个表
我在一个word文档中有一张主表。
表的第一列有键。我必须根据用户选择的键(行)创建另一个表。
用户可以多次选择一个键(行)。
Table1:
TaskName Data Group
abc data1 group1
pqr data2 group2
lmn data3 group3
TaskName 列是关键列,用户可以选择 abc,pqr,abc,pqr,lmn
这应该生成如下表格:
TaskName Data Group
abc data1 group1
pqr data2 group2
abc data1 group1
pqr data2 group2
lmn data3 group3
我无法使用 table.Cell().Range .Text
通过执行此格式设置会丢失。
I have a master table in one word document.
First column of the table has keys. I have to create another table based upon the keys (rows) selected by the user.
The user can select a key (row) more than one time.
Table1:
TaskName Data Group
abc data1 group1
pqr data2 group2
lmn data3 group3
TaskName column is the key column, A user can select abc,pqr,abc,pqr,lmn
This should generate a table as follows:
TaskName Data Group
abc data1 group1
pqr data2 group2
abc data1 group1
pqr data2 group2
lmn data3 group3
I cannot use table.Cell().Range.Text
as by doing this formatting is lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我首先尝试使用 Word.Selection,但我无法找到一种方法可以一次获取一行并复制粘贴它。可以使用 Word.Selection 来完成。
我想到的下一件事是一次复制粘贴一个单元格,但范围属性保留了表格单元格结构。我研究了单词模型,并试图找出每行后面是否有某种终止字符来将其区分为行尾。有这样一个字符,但它适用于所有细胞。要找到该字符,请单击office按钮(主页旁边),单击单词选项,单击显示,在屏幕上始终显示这些格式标记部分,勾选显示所有格式标记。这将显示 Word 文档中的所有不可打印字符。这是您将能够看到的符号 ¤。
该符号保存单元格的结构信息,公开的 ANSI 字符为 13 + 7。13 是段落标记,7 是“单元格结束”标记。这包含指向文件中单元结构管理的更多信息。在Word 2007中,这两个字符显示为一个字符,因此我们需要做的就是从单元格的范围中删除该字符。
我创建了一个列表来保存主表中我需要的所有单元格。
之后,以下代码从 Range 中删除最后一个字符。
目的是获取格式化文本,并通过从每个单元格 Range 中删除该字符,我们只能指向该特定单元格的结构,而不是整个表格。
如果您尝试仅使用格式化文本属性运行循环,您将收到表损坏错误。希望这有帮助。
I tried first using Word.Selection, but I could not figure out a way in which I can take one row at a time and copy paste it. It might be possible to do using Word.Selection.
The next thing I thought about was copy pasting a cell at a time, but Range Property keeps a hold of table cell structure. I looked into the Word Model and tried to find out if there is some kind of termination character after every row to distinguish it as end of row. There is such a character but it's for all the cells. To find this character out, click on the office button(besides Home), click on word options, click on display,in the section always show these formatting marks on the screen, tick show all formatting marks. This displays all the non-printable characters in a word document. This is the symbol you'll be able to see ¤.
This symbol holds the structural information of the cell and the exposed ANSI characters are 13 + 7. 13 is a paragraph mark and 7 is the "end-of-cell"marker. This holds further information that points to cell structure management in the file. In Word 2007 these two characters appear as one character, so what we need to do is drop this character from the cell's Range.
I created a list to hold all the cells that I need from the master Table.
After that the following code removes the last character from Range
The purpose was to obtain formatted text and by removing the character from each cell Range , we are able to point to only that particular cell's structure and not the whole tables.
If you try to run a loop with only formatted text property, you will get a table corrupt error. Hope this helps.