- 1 using System;
- 2 using System.Collections.Generic;
- 3 using System.ComponentModel;
- 4 using System.Data;
- 5 using System.Drawing;
- 6 using System.Linq;
- 7 using System.Text;
- 8 using System.Threading.Tasks;
- 9 using System.Windows.Forms;
- 10 using MSWord = Microsoft.Office.Interop.Word;
- 11
- 12 namespace _24_word_and_excel_Operator
- 13 {
- 14 public partial class Form1 : Form
- 15 {
- 16 object oMissing = System.Reflection.Missing.Value;
- 17 public Form1()
- 18 {
- 19 InitializeComponent();
- 20 }
- 21
- 22 private void button1_Click(object sender, EventArgs e)
- 23 {
- 24 //例子1:新建一个word文档,填入自定义的内容
- 25 Example1();
- 26
- 27 //例子2:操作模板的例子
- 28 Example2();
- 29
- 30 //例子3:操作模板的例子,是例子2的一个简单版本
- 31 Example3();
- 32
- 33 //例子4:复杂的例子,操作模板,标签内填值,文档中画表格,表格中填值。文档中填
- 34 Example4(@"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\测试文档.docx");
- 35 }
- 36 /// <summary>
- 37 /// 新建一个word文档,填入自定义的内容
- 38 /// </summary>
- 39 private void Example1()
- 40 {
- 41 //创建一个新的Word应用程序对象
- 42 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
- 43 //创建一个新的Word文档对象,就是创建一个空白的文档
- 44 MSWord.Document doc = wordApp.Documents.Add();
- 45 // 在新文档中添加内容
- 46 MSWord.Paragraph paragraph = doc.Paragraphs.Add();
- 47 paragraph.Range.Text = "Hello, World! This is a sample Word document.";
- 48
- 49 // 保存文档,注意:filePath路径要带上文件名
- 50 string filePath = @"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\Test\test.docx";
- 51 doc.SaveAs(filePath);
- 52
- 53 // 关闭Word应用程序
- 54 wordApp.Quit();
- 55 }
- 56
- 57 /// <summary>
- 58 /// 操作模板的例子
- 59 /// </summary>
- 60 private void Example2()
- 61 {
- 62 // 模板文件路径
- 63 string templatePath = @"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\测试文档.docx";
- 64 // 生成的文档保存路径
- 65 string newDocumentPath = @"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\Test\例子2文档.docx";
- 66
- 67 // 创建Word应用程序对象
- 68 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
- 69
- 70 // 打开模板文档
- 71 Microsoft.Office.Interop.Word.Document templateDocument = wordApp.Documents.Open(templatePath);
- 72
- 73 // 复制模板文档的内容
- 74 templateDocument.Content.Copy();
- 75
- 76 // 创建一个新的空白文档
- 77 Microsoft.Office.Interop.Word.Document newDocument = wordApp.Documents.Add();
- 78
- 79 // 将复制的内容粘贴到新文档中
- 80 newDocument.Content.Paste();
- 81
- 82 // 修改新文档的内容
- 83 MSWord.Paragraph paragraph = newDocument.Content.Paragraphs.Add();
- 84 paragraph.Range.Text = "This is a document generated from a template.";
- 85
- 86 // 保存新文档
- 87 newDocument.SaveAs(newDocumentPath);
- 88
- 89 // 关闭模板文档
- 90 templateDocument.Close();
- 91
- 92 // 关闭Word应用程序
- 93 wordApp.Quit();
- 94 }
- 95 /// <summary>
- 96 /// 操作模板的例子,是例子2的一个简单版本
- 97 /// </summary>
- 98 /// <param name="templatePath"></param>
- 99 /// <param name="outputPath"></param>
- 100 public void Example3()
- 101 {
- 102 string templatePath = @"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\测试文档.docx";
- 103 string outputPath = @"D:\110 - C#实例集合\100 - 实例集合\24 word and excel Operator\bin\Debug\Test\例子3的文档.docx";
- 104
- 105 // 创建Word应用程序对象
- 106 Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
- 107 // 打开模板文档
- 108 MSWord.Document wordDoc = wordApp.Documents.Open(templatePath);
- 109 {
- 110 //这里就是要干什么事了,替换文字,添加内容等操作
- 111 // 替换模板中的文本
- 112 FindAndReplace(wordDoc, "要替换的文字", "替换后的文字");
- 113 // 修改新文档的内容
- 114 MSWord.Paragraph paragraph = wordDoc.Content.Paragraphs.Add();
- 115 paragraph.Range.Text = "This is a document generated from a template.";
- 116 }
- 117
- 118 // 保存为新文档
- 119 wordDoc.SaveAs2(outputPath);
- 120 // 关闭Word应用程序对象
- 121 wordApp.Quit();
- 122 }
- 123
- 124 private void FindAndReplace(MSWord.Document doc, string findText, string replaceText)
- 125 {
- 126 // 将模板设为可编辑
- 127 doc.Activate();
- 128
- 129 // 执行查找和替换操作
- 130 // 可以使用其他查找和替换方法,例如使用Range对象
- 131 object findObj = findText;
- 132 object replaceObj = replaceText;
- 133 object missingObj = Type.Missing;
- 134 doc.Content.Find.Execute(ref findObj, ref missingObj, ref missingObj, ref missingObj, ref missingObj,
- 135 ref missingObj, ref missingObj, ref missingObj, ref missingObj, ref replaceObj, ref missingObj,
- 136 ref missingObj, ref missingObj, ref missingObj, ref missingObj);
- 137 }
- 138 private void Example4(string strPath)
- 139 {
- 140 object oTemplate = strPath;
- 141 MSWord.Application wordApp = new MSWord.Application();// 创建一个新的Word应用程序对象
- 142 wordApp.Visible = true;
- 143 MSWord.Document oDoc = wordApp.Documents.Add(ref oTemplate, ref oMissing, ref oMissing, ref oMissing);// 创建一个新的Word文档对象
- 144
- 145 //填写标签
- 146 object[] bookMark = new object[3];
- 147 bookMark[0] = "Test1";
- 148 bookMark[1] = "Test2";
- 149 oDoc.Bookmarks.get_Item(ref bookMark[0]).Range.Text = "第1个标签";
- 150 oDoc.Bookmarks.get_Item(ref bookMark[1]).Range.Text = "第2个标签";
- 151
- 152
- 153 //定义一个Word中的表格对象
- 154 oDoc.Paragraphs.Last.Range.Text = "直接接入式(正向电能)";
- 155 //用于在Word文档中进行特定的文本插入和调整操作
- 156 object unite = MSWord.WdUnits.wdStory;//这表示将选择操作的范围设为整个文档
- 157 oDoc.Content.InsertAfter("\n");//在文档的内容后面插入一个换行符("\n"),即实现在文档末尾添加一个空行
- 158 wordApp.Selection.EndKey(ref unite, ref oMissing);//将选择操作的焦点移动到文档的结尾,即将光标定位在文档的最后一行。
- 159 wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphLeft;//将光标所在处的段落对齐方式设置为左对齐。
- 160 object WdLine2 = MSWord.WdUnits.wdLine;//换一行;这表示将选择操作的单位设为行。
- 161 wordApp.Selection.MoveDown(ref WdLine2, 6, ref oMissing);//将当前选择向下移动6行。
- 162
- 163 int iRow = 5;
- 164 int iColumn = 3;
- 165 MSWord.Table table = oDoc.Tables.Add(wordApp.Selection.Range, iRow, iColumn, ref oMissing, ref oMissing);
- 166 table.Borders.Enable = 1;
- 167 for (int i = 1; i < iRow + 1; i++)
- 168 {
- 169 if (i == 1)
- 170 {
- 171 table.Cell(i, 1).Range.Text = "电压(V)";
- 172 table.Cell(i, 2).Range.Text = "电流(A)";
- 173 table.Cell(i, 3).Range.Text = "电能误差(%)";
- 174 }
- 175 else
- 176 {
- 177 table.Cell(i, 1).Range.Text = (i + 1).ToString();
- 178 table.Cell(i, 2).Range.Text = (i + 2).ToString();
- 179 table.Cell(i, 3).Range.Text = (i + 3).ToString();
- 180 }
- 181 }
- 182 //该行代码的作用是将表格中所有段落的对齐方式设置为居中对齐
- 183 table.Range.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter;
- 184
- 185 //文档最后填写文字
- 186 oDoc.Paragraphs.Last.Range.Text = "文档最后的位置:\n\r";
- 187
- 188 string docname = string.Empty;
- 189 docname = $"文档名称";
- 190
- 191 //弹出保存文件对话框,保存生成的Word
- 192 SaveFileDialog sfd = new SaveFileDialog
- 193 {
- 194 Filter = "Word Document(*.docx)|*.docx",
- 195 DefaultExt = "Word Document(*.docx)|*.docx",
- 196 FileName = docname
- 197 };
- 198 if (sfd.ShowDialog() == DialogResult.OK)
- 199 {
- 200 object filename = sfd.FileName;
- 201 oDoc.SaveAs(ref filename, ref oMissing, ref oMissing, ref oMissing,
- 202 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- 203 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
- 204 ref oMissing, ref oMissing);
- 205
- 206 // 关闭Word应用程序对象
- 207 wordApp.Quit();
- 208 }
- 209 }
- 210 }
- 211 }