前言

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

模板建立

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

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

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

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

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

程式碼實現

新建一個專案WordExport。

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

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

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