C# 動態生成word文檔

本文以一個簡單的小例子,簡述利用C#語言開發word表格相關的知識,僅供學習分享使用,若有不足之處,還請指正。ide

在工程中引用word的動態庫

在項目中,點擊項目名稱右鍵-->管理NuGet程序包,打開NuGet包管理器窗口,進行搜索下載便可,以下圖所示:學習

涉及知識點

  1. _Application: 表示word應用程序的接口,對應的實現類是Application類。
  2. _Document:表示一個word文檔,經過_Application對應的文檔接口進行建立。
  3. Paragraph:表示一個段落,經過_Document對象的相關方法進行建立。
  4. Table:表示一個表格,經過_Document對象的相關方法進行建立。
  5. Range:表示一個區域,能夠是一個段落,也能夠是一個表格,也能夠是一個單元格,能夠Range.select()將光標移動到當前區域。
  6. 移動焦點:wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);//移動焦點

生成文檔效果圖

核心代碼

  1 using Microsoft.Office.Interop.Word;
  2 using System;
  3 using System.Collections.Generic;
  4 using System.Data;
  5 using System.IO;
  6 using System.Linq;
  7 using System.Reflection;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 
 11 namespace ETWord
 12 {
 13     public class WordHelper
 14     {
 15         public static void CreateWordFile(string filePath)
 16         {
 17             
 18             try
 19             {
 20                 CreateFile(filePath);
 21                 //
 22                 MessageFilter.Register();
 23                 object wdLine = WdUnits.wdLine;
 24                 object oMissing = Missing.Value;
 25                 object fileName = filePath;
 26                 object heading2 = WdBuiltinStyle.wdStyleHeading2;
 27                 object heading3 = WdBuiltinStyle.wdStyleHeading3;
 28                 
 29                 _Application wordApp = new Application();
 30                 wordApp.Visible = true;
 31                 _Document wordDoc = wordApp.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
 32                 System.Data.DataTable dtDepts = DatabaseHelper.getDept();
 33                 int ii = 0;
 34                 foreach (DataRow dr in dtDepts.Rows)
 35                 {
 36                     string dept = dr["dept"].ToString();
 37                     Paragraph oPara0 = wordDoc.Content.Paragraphs.Add(ref oMissing);
 38                     oPara0.Range.Text = string.Format("{0}-{1}", ii + 1, dept);
 39                     //oPara0.Range.Font.Bold = 1;
 40                     //oPara0.Format.SpaceAfter = 5;
 41                     oPara0.Range.Select();
 42                     oPara0.set_Style(ref heading2);
 43                     oPara0.Range.InsertParagraphAfter();
 44                     System.Data.DataTable dtTemplate = DatabaseHelper.getTemplateByDept(dept);
 45                     int jj = 0;
 46                     foreach (DataRow dr1 in dtTemplate.Rows)
 47                     {
 48                         string template = dr1["template"].ToString();
 49                         string user1 = dr1["user1"].ToString();
 50                         string remark = dr1["remark"].ToString();
 51                         System.Data.DataTable dtData = DatabaseHelper.getDataByDeptAndTemplate(dept, template);
 52                         int count = dtData.Rows.Count;
 53                         int row = count + 4;
 54                         int column = 5;
 55                         object ncount = 1;
 56 
 57                         wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
 58                         wordApp.Selection.TypeParagraph();
 59                         Paragraph oPara1 = wordDoc.Content.Paragraphs.Add(ref oMissing);
 60                         oPara1.Range.Select();
 61                         oPara1.Range.Text = string.Format("{0}-{1}、{2}", ii + 1, jj + 1, template);
 62                         //oPara1.Range.Font.Bold = 1;
 63                         //oPara1.Format.SpaceAfter = 5;
 64                         oPara1.set_Style(ref heading3);
 65                         oPara1.Range.InsertParagraphAfter();
 66                         wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);
 67                         wordApp.Selection.TypeParagraph();
 68                         //設置表格
 69                         Table table = wordDoc.Tables.Add(wordApp.Selection.Range, row, column, ref oMissing, ref oMissing);
 70                        
 71                         table.Borders.OutsideLineStyle = WdLineStyle.wdLineStyleSingle;
 72                         table.Borders.InsideLineStyle = WdLineStyle.wdLineStyleSingle;
 73                         table.Range.Font.Bold = 0;
 74                         table.PreferredWidthType = WdPreferredWidthType.wdPreferredWidthAuto;
 75                         table.Columns[1].Width = 60f;
 76                         table.Columns[2].Width = 100f;
 77                         table.Columns[3].Width = 100f;
 78                         table.Columns[4].Width = 60f;
 79                         table.Columns[5].Width = 100f;
 80                         //列的合併
 81                         Cell cell = table.Cell(1, 2);
 82                         cell.Merge(table.Cell(1, 5));
 83                         Cell cell2 = table.Cell(2, 2);
 84                         cell2.Merge(table.Cell(2, 5));
 85                         Cell cell3 = table.Cell(3, 2);
 86                         cell3.Merge(table.Cell(3, 5));
 87                         //賦值
 88                         table.Cell(1, 1).Range.Text = "流程名稱:";
 89                         table.Cell(2, 1).Range.Text = "使用人:";
 90                         table.Cell(3, 1).Range.Text = "流程說明:";
 91                         table.Cell(4, 1).Range.Text = "節點";
 92                         table.Cell(4, 2).Range.Text = "節點名";
 93                         table.Cell(4, 3).Range.Text = "處理人員";
 94                         table.Cell(4, 4).Range.Text = "處理方式";
 95                         table.Cell(4, 5).Range.Text = "跳轉信息";
 96                         table.Cell(1, 2).Range.Text = template;
 97                         table.Cell(2, 2).Range.Text = user1;
 98                         table.Cell(3, 2).Range.Text = remark;
 99                         int kk = 5;
100                         foreach (DataRow dr2 in dtData.Rows)
101                         {
102                             table.Cell(kk, 1).Range.Text = (kk - 4).ToString();
103                             table.Cell(kk, 2).Range.Text = dr2["NodeName"].ToString();
104                             table.Cell(kk, 3).Range.Text = dr2["DoName"].ToString();
105                             table.Cell(kk, 4).Range.Text = dr2["DoType"].ToString();
106                             table.Cell(kk, 5).Range.Text = string.Empty;
107                             kk++;
108                         }
109                         table.Cell(kk - 1, 5).Range.Select();
110 
111                         wordApp.Selection.MoveDown(ref wdLine, ref ncount, ref oMissing);//移動焦點
112                         wordApp.Selection.TypeParagraph();//插入段落
113 
114                         jj++;
115                     }
116                     ii++;
117                 }
118 
119                 //保存
120                 wordDoc.Save();
121                 wordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
122                 wordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
123                 MessageFilter.Revoke();
124 
125             }
126             catch (Exception e)
127             {
128                 Console.WriteLine(e.Message);
129                 Console.WriteLine(e.StackTrace);
130 
131             }
132         }
133 
134         private static void CreateFile(string filePath)
135         {
136             if (!File.Exists(filePath))
137             {
138                 using (FileStream fs = File.Create(filePath))
139                 {
140 
141                 }
142             }
143         }
144     }
145 }
View Code

關於word開發中字體大小

知足中文出版中使用字號做爲字體大小的單位的須要,它容許用戶同時使用「號」和 「磅」做爲字體大小的單位。字體

磅值與字號之間的換算關係以下:ui

word中設置寬度時,採用磅爲單位,磅與釐米之間的換算關係以下:spa

 

備註

  1.  插入多個表格時,表格容易嵌套,主要是因爲往下移動的行數不對,後來經過選中表格右下角的單元格,將光標移動到表格右下角,而後再往下移動兩行,便可解決表格嵌套的問題。
  2. 單元格合併問題,當單元格合併時,單元格的位置也隨之改變,如:水平方向第二,三兩個單元格合併,則原來的第四個單元格的座標就會變成第三個單元格。
  3. 開發運行須要在電腦上安裝office組件,或者也能夠安裝wps。

關於源碼下載連接code

相關文章
相關標籤/搜索