1. 程式人生 > 其它 >C#匯出資料—使用Word模板書籤的使用

C#匯出資料—使用Word模板書籤的使用

前言

本文主要介紹C#使用標籤替換的方法匯出資料,匯出的資料模板使用Word文件。

模板建立

首先建立一個Word文件,然後建立一個基礎模板。然後將上方選單切換到插入選單。

然後在想填充資料的地方新增書籤,如下圖,游標在年的前方,點選上方的書籤按鈕。

書籤全部新增完如下圖所示:

書籤預設是看不到的,我們可以開啟檔案下的選項頁面,然後在視圖裡勾選書籤選項,讓書籤顯示出來,如下圖:

勾選後,書籤位置會有一個豎線顯示,結果如下圖所示:

程式碼實現

新建一個專案WordExport。

然後Nuget新增引用Microsoft.Office.Interop.Word。

然後在頁面裡新增一個按鈕,然後在點選事件裡實現如下程式碼:

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 private void Button_Click(object sender, RoutedEventArgs e) { try { string
wordTemplatePath = System.Windows.Forms.Application.StartupPath + @"\Word模板.docx"; if (File.Exists(wordTemplatePath)) { System.Windows.Forms.FolderBrowserDialog dirDialog = new System.Windows.Forms.FolderBrowserDialog(); dirDialog.ShowDialog(); if (dirDialog.SelectedPath != string.Empty) { string newFileName = dirDialog.SelectedPath +
@"\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".docx"; Dictionary<string, string> wordLableList = new Dictionary<string, string>(); wordLableList.Add("年", "2021"); wordLableList.Add("月", "9"); wordLableList.Add("日", "18"); wordLableList.Add("星期", "六"); wordLableList.Add("標題", "Word匯出資料"); wordLableList.Add("內容", "我是內容——Kiba518"); Export(wordTemplatePath, newFileName, wordLableList); MessageBox.Show("匯出成功!"); } else { MessageBox.Show("請選擇匯出位置"); } } else { MessageBox.Show("Word模板檔案不存在!"); } } catch (Exception Ex) { MessageBox.Show(Ex.ToString()); return; } } public static void Export(string wordTemplatePath, string newFileName, Dictionary<string, string> wordLableList) { Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application(); string TemplateFile = wordTemplatePath; File.Copy(TemplateFile, newFileName); _Document doc = new Document(); object obj_NewFileName = newFileName; object obj_Visible = false; object obj_ReadOnly = false; object obj_missing = System.Reflection.Missing.Value; doc = app.Documents.Open(ref obj_NewFileName, ref obj_missing, ref obj_ReadOnly, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_Visible, ref obj_missing, ref obj_missing, ref obj_missing, ref obj_missing); doc.Activate(); if (wordLableList.Count > 0) { object what = WdGoToItem.wdGoToBookmark; foreach (var item in wordLableList) { object lableName = item.Key; if (doc.Bookmarks.Exists(item.Key)) { doc.ActiveWindow.Selection.GoTo(ref what, ref obj_missing, ref obj_missing, ref lableName);//游標移動書籤的位置 doc.ActiveWindow.Selection.TypeText(item.Value);//在書籤處插入的內容 doc.ActiveWindow.Selection.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;//設定插入內容的Alignment } } } object obj_IsSave = true; doc.Close(ref obj_IsSave, ref obj_missing, ref obj_missing); }

程式碼裡我們模擬了一個標籤要替換的內容字典,然後呼叫Microsoft.Office.Interop.Word名稱空間下的類,實現對Word模板的書籤的替換。

執行專案,如下圖:

點選匯出按鈕,匯出Word文件如下:

----------------------------------------------------------------------------------------------------

到此,C#匯出資料—使用Word模板就已經介紹完了。

程式碼已經傳到Github上了,歡迎大家下載。

Github地址:https://github.com/kiba518/WordExport

----------------------------------------------------------------------------------------------------

注:此文章為原創,任何形式的轉載都請聯絡作者獲得授權並註明出處!
若您覺得這篇文章還不錯,請點選下方的推薦】,非常感謝!

出處:https://www.cnblogs.com/kiba/p/15309344.html

您的資助是我最大的動力!
金額隨意,歡迎來賞!
款後有任何問題請給我留言。

如果,您認為閱讀這篇部落格讓您有些收穫,不妨點選一下右下角的推薦按鈕。
如果,您希望更容易地發現我的新部落格,不妨點選一下綠色通道的關注我。(●'◡'●)

如果你覺得本篇文章對你有所幫助,請給予我更多的鼓勵,求打 付款後有任何問題請給我留言!!!

因為,我的寫作熱情也離不開您的肯定支援,感謝您的閱讀,我是【Jack_孟】!