[英]C# Populate word template with OpenXml
我想創建一個新的Word文檔,該文檔基於帶有內容控件的模板。
我需要填充那些內容控件(文本)。
我只找到了如何生成新的Word文檔,卻沒有基於模板。
您有任何鏈接或教程嗎?
也許我使用OpenXml填寫模板不對嗎?
// Create a Wordprocessing document.
using (WordprocessingDocument myDoc =
WordprocessingDocument.Create("d:/dev/test.docx",
WordprocessingDocumentType.Document))
{
// Add a new main document part.
MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
//Create Document tree for simple document.
mainPart.Document = new Document();
//Create Body (this element contains
//other elements that we want to include
Body body = new Body();
//Create paragraph
Paragraph paragraph = new Paragraph();
Run run_paragraph = new Run();
// we want to put that text into the output document
Text text_paragraph = new Text("Hello World!");
//Append elements appropriately.
run_paragraph.Append(text_paragraph);
paragraph.Append(run_paragraph);
body.Append(paragraph);
mainPart.Document.Append(body);
// Save changes to the main document part.
mainPart.Document.Save();
}
使用OpenXML是正確的方法,因為您無需安裝MS Word就可以處理文檔-考慮服務器場景。 但是,它的學習曲線陡峭而乏味。 以我的拙見,最好的方法是要求預算來購買第三方工具包,並專注於業務領域而不是OpenXML調整。
在您的示例中,您在Word中有一個模板,想要用數據更新(合並)它。 對於某些場景,我已經使用Docentric Toolkit完成了,並且效果很好。 一旦了解了其工作原理,便可以快速創建解決方案。 如果您在DT遇到麻煩,伙計們不會讓您失望的。 請參閱此鏈接( http://www.codeproject.com/Articles/759408/Creating-Word-documents-in-Net-using-Docentric-Too ),以了解如何使用它。 默認情況下,它創建.docx,但是您也可以獲取固定的最終文檔(pdf或xps)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.