使用C#在Word中按名称访问表 [英] Accessing a table by name in Word using c#

查看:114
本文介绍了使用C#在Word中按名称访问表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含很多表的文档.我想使用C#和Microsoft.Office.Interop.Word修改这些表.我读到我可以使用Tables接口遍历所有表.

I have a document containing lots of tables. I want to modify those tables using C# and Microsoft.Office.Interop.Word. I read that I can iterate trough all tables using the Tables interface.

问题:是否可以为表指定名称/ID并通过其名称访问表? (例如Powerpoint自动化中的形状).

Question: is it possible to give the table a name/ID and access the table by its name? (like with shapes in Powerpoint automation).

问题2:如果Microsoft.Office.Interop.Word无法实现,那么诸如Aspose Words之类的替代方案是否提供了更便捷的方式?

Question 2: If it's not possible with Microsoft.Office.Interop.Word, do the alternatives like Aspose Words offer a more convenient way?

推荐答案

是否可以为表指定名称/ID并通过其名称访问表?

is it possible to give the table a name/ID and access the table by its name?

Table对象确实提供了 ID 属性,但这仅是另存为网页的文档.我假设这不是您想要的.

The Table object does offer an ID property however this is only documents saved as a web page. I am assuming this is not what you want.

仍然有可能,但是API并未提供它;您需要自己创建此功能.有两种方法:

It is still possible however it is not provided by the API; you need to create this feature yourself. There are two ways:

一个:使用Interop,您可以检索表的哈希码,该哈希码(希望是)返回唯一的ID.例如:

One: Using Interop, you can retrieve the table's hashcode which (hopefully) returns a unique ID. For example:

Word.Application wordApp = new Word.Application();
Word.Document wordDoc = wordApp.Documents.Open(@"C:\Users\username\Documents\HasTables.docx");
var tableID = wordDoc.Tables[1].GetHashCode();

一种彻底的解决方案可能是将文档中所有表的ID存储在Dictionary或具有键/值对映射的其他集合类型中.现在请注意,这些ID不会在Word会话中持久存在...因此,如果您需要能够标识同一会话之外的表,则需要另一种方法.

A thorough solution might be to store the IDs of all tables in the document within a Dictionary or another collection type which has a map of key/value pairs. Now note that these IDs do not persist across Word sessions...so if you need to be able to identify a table beyond the same session, you will need to another method.

两个:使用OpenXML(对于.docx格式)和/或WordML(对于Word 2003 XML,如果不能简单地将文件升级到.docx),则分别为每个文件创建自己的Id或Name属性w:tbl元素.您可以使用自己的名称空间来消除歧义.这也将在Word会话中持续存在.与Interop相比,这是一个不同的世界,但在我看来,这是更好的途径(性能,可用的文档).

Two: Using OpenXML (for .docx format) and/or WordML (for Word 2003 XML, if you cannot simply upgrade the ocument to .docx) create your own Id or Name attribute for each w:tbl element. You can disambiguate by using your own namespace. This will also persist across Word sessions. This is a different world then Interop, but in my opinion, is the much better route (Performance, available documentation).

如果Microsoft.Office.Interop.Word无法实现,那么诸如Aspose Words之类的替代方案是否提供了更便捷的方法?

If it's not possible with Microsoft.Office.Interop.Word, do the alternatives like Aspose Words offer a more convenient way?

否,Aspose在其 TableCollection 中没有提供类似的功能或对象.

No, Aspose does not offer anything like this in their TableCollection or Table objects.

这篇关于使用C#在Word中按名称访问表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆