2 回答
![?](http://img1.sycdn.imooc.com/533e4bec0001ae5302000200-100-100.jpg)
TA贡献1856条经验 获得超5个赞
由于文档结果相同,我使用以下方法将 ID 复制到新文档。
请注意段落/表格/等。数组从元素索引 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 来跟踪内存中加载的 DOCX 文件和我的应用程序中显示的 HTML 表示),这对我的情况非常有用。
![?](http://img1.sycdn.imooc.com/533e4c0500010c7602000200-100-100.jpg)
TA贡献1797条经验 获得超6个赞
尽管上面的这种方法在大型文档上非常慢,所以我不得不以不同的方式做:
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
之前的版本处理一个小文档大约需要 1500 毫秒,而这个版本在大约 400 毫秒内渲染同一个文档!
- 2 回答
- 0 关注
- 171 浏览
添加回答
举报