網上有一些基礎的東西,但是比如插入圖片,就沒有找到方案,最終自己摸索出來的。
1.首先通過Nuget獲取引用,關鍵字:“DocX”

2.示例代碼
class Program { static void Main(string[] args) { string path = @"C:\Users\Administrator\Desktop\test.docx"; using (var document = DocX.Create(path)) { //文字居中對齊 document.InsertParagraph().Append("自定義word").Alignment = Alignment.center; //文字加粗 document.InsertParagraph().Append("我要加粗").Bold(); //插入表格 Table table=document.AddTable(2,3); table.Rows[0].Cells[0].Paragraphs[0].Append("第一行第一列"); table.Rows[0].Cells[1].Paragraphs[0].Append("第一行第二列"); table.InsertRow(); document.InsertTable(table); //插入圖片 Image image = document.AddImage("d:\\title.jpg"); Picture picture= image.CreatePicture(); document.InsertParagraph().AppendPicture(picture); document.Save(); } Console.WriteLine(path); } }
3.最終效果圖

