【问题标题】:Create repeating rows in word using interop使用互操作在 word 中创建重复行
【发布时间】:2017-08-08 18:53:20
【问题描述】:

我有以下代码

string html  = @"<table>
<tr>
<td>Column1</td>
<td>Column2</td>
</tr>
<tr>
<td>Column1 Data</td>
<td>Column 2 Data</td>
</tr></table>";

这只是一个包含 html 表格的字符串(我实际拥有的表格非常大,我从存储过程中获取)

下面的代码读取这个html字符串并将其插入到word文档中

public static bool CreateFormattedWord(string html)
    {
        Application wordApp = new Application();       
        wordApp.Visible = true;
        Document doc = wordApp.Documents.Add();      
        object missing = Type.Missing;
        ContentControl contentControl = doc.ContentControls.Add(WdContentControlType.wdContentControlRichText, ref missing);     
        contentControl.Range.InsertFile(SaveToTemporaryFile(html), ref missing, ref missing, ref missing, ref missing);       

        Console.Read();
        return true;
    }

    public static string SaveToTemporaryFile(string html)
    {
        string htmlTempFilePath = System.IO.Path.Combine(System.IO.Path.GetTempPath(), string.Format("{0}.html", System.IO.Path.GetRandomFileName()));
        using (StreamWriter writer = File.CreateText(htmlTempFilePath))
        {
            html = string.Format("<html>{0}</html>", html);
            writer.WriteLine(html);
        }
        return htmlTempFilePath;
    }

我可以转到创建的 Word 文档,选择我插入的表格并转到布局并单击重复表标题,它会在文档中重复该表。

如何在我拥有的代码中使用 Interop 实现相同的功能,以便在将表格插入文档时重复表格标题?

【问题讨论】:

    标签: c# .net interop office-interop


    【解决方案1】:
     var tableID = doc.Tables[1];
       tableID.Rows[1].HeadingFormat = -1; 
    

    上面的代码似乎可以解决问题

    我从两个链接得到答案

    https://stackoverflow.com/questions/16032867/accessing-a-table-by-name-in-word-using-c-sharp 
    
    https://stackoverflow.com/questions/1816957/how-can-i-create-a-header-in-a-table-for-each-new-page-with-word-interop
    

    【讨论】:

    • 嗨 user1221989,你能接受这个答案吗,正如@cubrr 所建议的那样?谢谢。如果您不确定如何操作,请联系我。
    • 这对我不起作用,标题永远不会在行上重复
    • 投反对票,见上文。请尽量回复在此平台上为您提供帮助的人 - 内容最好尽可能以协作方式进行策划。
    猜你喜欢
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多