我有以下代码
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文档,选择我插入的表格,然后转到布局并单击Repeat Table标题,然后在文档中重复该表格。
如何在我的代码中使用Interop实现相同的功能,因此当表格插入文档时,它会重复表格标题?
答案 0 :(得分:-2)
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