使用word模板導出word文檔,首先需要在word模板中插入書簽:
根據創建的書簽名和位置,將需要寫入的內容插入到word文件中。
需要引用 Microsoft.Office.Interop.Word;在添加引用-程序集中搜索可以找到。
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using Telerik.Windows.Controls; namespace LawsRegulationDate.LawenforceManagement.DocumentFile { /// <summary> /// Interaction logic for HomePage.xaml /// </summary> public partial class HomePage { public HomePage() { InitializeComponent(); } private void 行政執法首頁_Click(object sender, RoutedEventArgs e) { //模板文件 string urlstring = System.Windows.Forms.Application.StartupPath + "\\File\\WordTemplate\\行政執法案卷(首頁).docx"; if (string.IsNullOrEmpty(urlstring) || !File.Exists(urlstring)) { Message_Box.Show("【行政執法案卷(首頁)】模板不存在!"); return; } else { string fileName = string.Empty; System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog(); fbd.ShowDialog(); if (fbd.SelectedPath != string.Empty) { fileName = fbd.SelectedPath + "\\行政執法案卷(首頁)" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx"; WordEstablish(urlstring, fileName); } else { Message_Box.Show("選擇導出位置"); return; } } } /// <summary> /// 調用生產方法 /// </summary> /// <param name="urlstring">模板文件</param> /// <param name="fileName">新文件</param> public void WordEstablish(string urlstring, string fileName) { try { #region 聲明參數 Dictionary<string, string> DList = new Dictionary<string, string>(); DList.Add("案由", 案由.Text); DList.Add("處理結果", 處理結果.Text); DList.Add("立案年", LAYear.Text); DList.Add("立案月", LAMonth.Text); DList.Add("立案日", LADay.Text); DList.Add("結案年", JAYear.Text); DList.Add("結案月", JAMonth.Text); DList.Add("結案日", JADay.Text); DList.Add("件", JianText.Text); DList.Add("頁", YeText.Text); DList.Add("檔案號", GDText.Text); DList.Add("承辦人", CBText.Text); DList.Add("歸檔年", GDYear.Text); DList.Add("歸檔月", GDMonth.Text); DList.Add("歸檔日", GDDay.Text); DList.Add("保存期限", BCYear.Text); #endregion if (WordClass.ExportWord(urlstring, fileName, DList)) { MessageBox.Show("生成'" + fileName + "'成功!"); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); return; } } } }
WordClass.cs類
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = Microsoft.Office.Interop.Word; using System.Collections.Generic; using System.IO; namespace LawsRegulationDate { public class WordClass { /// <summary> /// 模板生成word /// </summary> /// <param name="templateFile">模板文件</param> /// <param name="fileName">新文件</param> /// <param name="myDictionary">參數數組</param> public static bool ExportWord(string templateFile, string fileName, Dictionary<string, string> myDictionary) { try { //生成word程序對象 Word.Application app = new Word.Application(); //模板文件 string TemplateFile = templateFile; //模板文件拷貝到新文件 File.Copy(TemplateFile, fileName); //生成documnet對象 Word._Document doc = new Word.Document(); object Obj_FileName = fileName; object Visible = false; object ReadOnly = false; object missing = System.Reflection.Missing.Value; //打開文件 doc = app.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref Visible, ref missing, ref missing, ref missing, ref missing); doc.Activate(); #region 聲明參數 if (myDictionary.Count > 0) { object what = Word.WdGoToItem.wdGoToBookmark; object WordMarkName; foreach (var item in myDictionary) { WordMarkName = item.Key; doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref WordMarkName);//光標轉到書簽的位置 doc.ActiveWindow.Selection.TypeText(item.Value);//插入的內容,插入位置是word模板中書簽定位的位置 doc.ActiveWindow.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//設置當前定位書簽位置插入內容的格式 } } #endregion //輸出完畢后關閉doc對象 object IsSave = true; doc.Close(ref IsSave, ref missing, ref missing); return true; } catch { return false; } } } }