2 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
由于文檔結(jié)果相同,我使用以下方法將 ID 復(fù)制到新文檔。
請(qǐng)注意段落/表格/等。數(shù)組從元素索引 1 開始,而不是 0。
string fn = Path.GetTempPath() + TmpPrefix +GUID() + ".html";
Document temp = new Document();
// Copy whole old document to new document
temp.Range().InsertXML(doc.Range().XML);
// copy IDs assuming the documents are identical and have same amount of elements
for (int i = 1; i <= temp.Tables.Count; i++) {
temp.Tables[i].ID = doc.Tables[i].ID;
Range sRange = doc.Tables[i].Range;
Range tRange = temp.Tables[i].Range;
for(int j = 1; j <= tRange.Cells.Count; j++)
{
tRange.Cells[j].ID = sRange.Cells[j].ID;
}
}
for(int i=1; i <= temp.Paragraphs.Count; i++)
{
temp.Paragraphs[i].ID = doc.Paragraphs[i].ID;
}
// Save new temp document as HTML
temp.SaveAs2(fn, WdSaveFormat.wdFormatFilteredHTML);
temp.Close();
return fn;
由于我不需要輸出的 DOCX 文件中的 ID(我只使用 ID 來跟蹤內(nèi)存中加載的 DOCX 文件和我的應(yīng)用程序中顯示的 HTML 表示),這對(duì)我的情況非常有用。

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊
盡管上面的這種方法在大型文檔上非常慢,所以我不得不以不同的方式做:
public static string RenderHTMLFile(Document doc)
{
string fn = Path.GetTempPath() + TmpPrefix +GUID() + ".html";
var vba = doc.VBProject;
var module = vba.VBComponents.Add(Microsoft.Vbe.Interop.vbext_ComponentType.vbext_ct_StdModule);
var code = Properties.Resources.HTMLRenderer;
module.CodeModule.AddFromString(code);
var dataMacro = Word.Run("renderHTMLCopy", fn);
return fn;
}
Properties.Resources.HTMLRenderer帶有以下VB代碼的txt文件在哪里:
Sub renderHTMLCopy(ByVal path As String)
'
' renderHTMLCopy Macro
'
'
Selection.WholeStory
Selection.Copy
Documents.Add
Selection.PasteAndFormat wdPasteDefault
ActiveDocument.SaveAs2 path, WdSaveFormat.wdFormatFilteredHTML
ActiveDocument.Close False
End Sub
之前的版本處理一個(gè)小文檔大約需要 1500 毫秒,而這個(gè)版本在大約 400 毫秒內(nèi)渲染同一個(gè)文檔!
- 2 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報(bào)