Docx讀寫Word


  Docx.dll功能比較強大,具備以下功能:

  1. 創建新的word文檔或者讀取已有的world文檔
  2. 替換書簽處內容;
  3. 插入表格或者在已有表格新增數據行;
  4. 插入圖片,輕松設置圖片大小
  5. 保存或者另存為;

      分別對應代碼如下:

1. 創建新的word文檔或者讀取已有的world文檔

 //打開已有文檔
 DocX document = DocX.Load(@"./Report.docx");

 //創建新文檔
 DocX document = DocX.Create(@"./Report.docx");

  

2. 替換書簽處內容;

document.Bookmarks["Unit"].SetText("xxxxxx");  

  

3. 插入表格或者在已有表格新增數據行;

//添加新的Table
Table table = document.AddTable(4, 4);
table.Rows[0].Cells[0].Paragraphs[0].Append("1");
table.Rows[0].Cells[1].Paragraphs[0].Append("2");
table.Rows[1].Cells[0].Paragraphs[0].Append("3");
table.Rows[1].Cells[1].Paragraphs[0].Append("4");

//在已有Table中,新增數據行
Table table = document.Tables[0];//按照索引獲取,當前文檔的第幾個表格,從0開始
Row row = table.InsertRow(); //按照Table的格式生成一個新行
row.Height = 30;
Cell cell = row.Cells[0]; //第一個單元格
cell.VerticalAlignment = VerticalAlignment.Center;
cell.Paragraphs[0].Alignment = Alignment.center;
cell.Paragraphs[0].Append("xxxxx");

  

4. 插入圖片,輕松設置圖片大小;

Image image = document.AddImage("F:\\1.jpg");

Picture picture = image.CreatePicture();
picture.Width = 200;
picture.Height = 200;

Paragraph paragraphPic = document.Bookmarks["Picture"].Paragraph;
paragraphPic.Alignment = Alignment.center;
paragraphPic.AppendPicture(picture);

  

5. 保存或者另存為;

//保存 
document.Save();
//另存為
document.SaveAs(".\\OilReport2.docx");

  完整Demo下載


免責聲明!

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



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