C#通過DocX創建word


網上有一些基礎的東西,但是比如插入圖片,就沒有找到方案,最終自己摸索出來的。

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.最終效果圖

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM