C# 中的 Word 自动化 - 在 Word 中创建表格

发布于 2024-07-09 07:36:41 字数 170 浏览 6 评论 0原文

我有一个 C# 应用程序,它将打开一个 Word 文档,然后用我拥有的数据(如名称、类等)替换一些预定义的书签。

这一切都只是字符串值。现在我想渲染一个带有动态数字的表格Word 文档的行数。我希望该表位于文档中的特定位置。

我可以为此使用书签吗?如果可以怎么办??? 还有其他方法吗?

I have a C# application which will open a word document and then replace some of the predefined bookmarks with the data which i have like Name,Class etc..

It is all are just string values .Now I want to render a Table with dynamic number of rows to the word document.I want the table in a particular place in the document.

Can i use bookmark for this.If so how ??? IS there any other method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-07-16 07:36:41

是的,您可以使用书签,也可以使用字段将其替换为带有n号的表。 行数和 n 数。 列。

您可以循环遍历字段并获取其范围,并使用范围您可以在字段位置添加表:

//CREATING OBJECTS OF WORD AND DOCUMENT

Word.Application oWord = new Word.Application();

Word.Document oWordDoc = new Word.Document();

foreach (Word.Field myMergeField in oWordDoc.Fields)

{

    iTotalFields++;

    Word.Range rngFieldCode = myMergeField.Code;

    String fieldText = rngFieldCode.Text;    


    // ONLY GETTING THE MAILMERGE FIELDS    
    if (fieldText.StartsWith("tablename"))

    {
        myMergeField.Select();
        oWordDoc.table.add(rngFieldCode,4//for rows,4// for colioulns,ref omising....);
     }
}

Yes, you can use bookmarks and also use fields to replace it with table with n no. of rows and n no. of columns.

You can loop through fields and get its range and using range you can add table at field location:

//CREATING OBJECTS OF WORD AND DOCUMENT

Word.Application oWord = new Word.Application();

Word.Document oWordDoc = new Word.Document();

foreach (Word.Field myMergeField in oWordDoc.Fields)

{

    iTotalFields++;

    Word.Range rngFieldCode = myMergeField.Code;

    String fieldText = rngFieldCode.Text;    


    // ONLY GETTING THE MAILMERGE FIELDS    
    if (fieldText.StartsWith("tablename"))

    {
        myMergeField.Select();
        oWordDoc.table.add(rngFieldCode,4//for rows,4// for colioulns,ref omising....);
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文