【发布时间】: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