2017-08-08 62 views
0

我有下面的代碼重複的字行創建使用互操作

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文檔中選擇我插入表和去佈局和點擊Repea t表頭,它在文檔中重複。

我該如何在代碼中使用Interop來實現相同的功能,因此當表格插入文檔時,表格標題會重複?

回答

-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 
+0

一定要接受你的答案:) – cubrr

+0

嗨user1221989,你能接受這個回答,正如@cubrr建議的那樣?謝謝。如果你不確定怎麼做,請給我打電話。 – halfer

+0

這對我不起作用,標題從不在行 – R2D2