C#通過模板匯出Word的兩種方法(超簡單)
方法一:使用Office的組件
==使用該方法必須要安裝Office==
1、制作Word模板
在需要填充內容的地方增加識別符號號,方便之后替換使用,例如 ==[專案名稱]==,其中[]符號和中間的文字可根據個人情況進行修改,
到此模板已經制作完成,是不是很簡單,
2、操作Word
2.1 參考Microsoft.Office.Interop.Word.dll
添加命名空間
using Word = Microsoft.Office.Interop.Word;
2.2 編碼開始
string mubanFile = "模板.docx";
string templatePath = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, mubanFile);
Dictionary<string, string> bookmarks = new Dictionary<string, string>();
var item=xxx;//資料源
//將資料與Word模板中的標簽對應
bookmarks.Add("[姓名]", item.UserName);
bookmarks.Add("[性別]", item.Sex);
bookmarks.Add("[出生年月]", item.BirthDay);
bookmarks.Add("[民族]", item.Ethnic);
bookmarks.Add("[文化程度]", item.EducationalLevel);
bookmarks.Add("[詳細地址]", item.Address);
bookmarks.Add("[電話]", item.Phone);
string wordpath = outputPath + "xx.docx";//匯出word地址
string pdfpath = outputPath + "xx.pdf";//匯出pdf地址
GenerateWord(templatePath, wordpath, pdfpath, bookmarks);
/// <summary>
/// 根據word模板檔案匯出word/pdf檔案
/// </summary>
/// <param name="templateFile">模板路徑</param>
/// <param name="fileNameWord">匯出檔案名稱</param>
/// <param name="fileNamePdf">pdf檔案名稱</param>
/// <param name="bookmarks">模板內書簽集合</param>
public static void GenerateWord(string templateFile, string fileNameWord, string fileNamePdf, Dictionary<string, string> bookmarks)
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
File.Copy(templateFile, fileNameWord, true);
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object Obj_FileName = fileNameWord;
object Visible = false;
object ReadOnly = false;
object missing = System.Reflection.Missing.Value;
object IsSave = true;
object FileName = fileNamePdf;
object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
object LockComments = false;
object AddToRecentFiles = true;
object ReadOnlyRecommended = false;
object EmbedTrueTypeFonts = false;
object SaveNativePictureFormat = true;
object SaveFormsData = false;
object SaveAsAOCELetter = false;
object Encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingSimplifiedChineseGB18030;
object InsertLineBreaks = false;
object AllowSubstitutions = false;
object LineEnding = Microsoft.Office.Interop.Word.WdLineEndingType.wdCRLF;
object AddBiDiMarks = false;
try
{
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();
foreach (string bookmarkName in bookmarks.Keys)
{
string newstr;
string newStrs;
replace(doc, bookmarkName, bookmarks[bookmarkName]);//替換內容
}
//replace(doc, "hello", "shalv");
//此處存盤時,引數可選填,如需另外生成pdf,加入一個引數ref FileName,
doc.SaveAs(ref FileName, ref FileFormat, ref LockComments,
ref missing, ref AddToRecentFiles, ref missing,
ref ReadOnlyRecommended, ref EmbedTrueTypeFonts,
ref SaveNativePictureFormat, ref SaveFormsData,
ref SaveAsAOCELetter, ref Encoding, ref InsertLineBreaks,
ref AllowSubstitutions, ref LineEnding