本文中用C#来操作Word,包括:
创建Word;
插入文字,选择文字,编辑文字的字号、粗细、颜色、下划线等;
设置段落的首行缩进、行距;
设置页面页边距和纸张大小;
设置页眉、页码;
插入图片,设置图片宽高以及给图片添加标题;
插入表格,格式化表格,往表格中插入数据;
保存Word,打印Word;
重新打开Word等。
Visual studio版本:Visual Studio 2012(2010应该也可以)
准备工作:
/* 1. 添加引用COM里面的 Microsoft Word 12.0 Object. Library 引用(12.0表示Word 2007版本)
2. 导命名空间
using MSWord =Microsoft.Office.Interop.Word; using System.IO; using System.Reflection;
3. 把引用中的Microsoft.Office.Interop.Word的“属性”中的嵌入互操作设为False */
以下是全部代码:(代码有点长,但请不要有压力,直接复制进去就能直接成功运行)
1 using System;
2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 using MSWord = Microsoft.Office.Interop.Word; 7 using System.IO; 8 using System.Reflection; 9 10 namespace Console_WordSkill_All 11 { 12 class Program 13 { 14 static void Main(string[] args) 15 { 16 object path; //文件路径变量 17 string strContent; //文本内容变量 18 MSWord.Application wordApp; //Word应用程序变量 19 MSWord.Document wordDoc; //Word文档变量 20 21 path = Environment.CurrentDirectory + "\\MyWord_Print.doc"; 22 wordApp = new MSWord.ApplicationClass(); //初始化 23 24 wordApp.Visible = true;//使文档可见 25 26 //如果已存在,则删除 27 if (File.Exists((string)path)) 28 { 29 File.Delete((string)path); 30 } 31 32 //由于使用的是COM库,因此有许多变量需要用Missing.Value代替 33 Object Nothing = Missing.Value; 34 wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); 35 36 #region 页面设置、页眉图片和文字设置,最后跳出页眉设置 37 38 //页面设置 39 wordDoc.PageSetup.PaperSize = MSWord.WdPaperSize.wdPaperA4;//设置纸张样式为A4纸 40 wordDoc.PageSetup.Orientation = MSWord.WdOrientation.wdOrientPortrait;//排列方式为垂直方向 41 wordDoc.PageSetup.TopMargin = 57.0f; 42 wordDoc.PageSetup.BottomMargin = 57.0f; 43 wordDoc.PageSetup.LeftMargin = 57.0f; 44 wordDoc.PageSetup.RightMargin = 57.0f; 45 wordDoc.PageSetup.HeaderDistance = 30.0f;//页眉位置 46 47 //设置页眉 48 wordApp.ActiveWindow.View.Type = MSWord.WdViewType.wdNormalView;//普通视图(即页面视图)样式 49 wordApp.ActiveWindow.View.SeekView = MSWord.WdSeekView.wdSeekPrimaryHeader;//进入页眉设置,其中页眉边距在页面设置中已完成 50 wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphRight;//页眉中的文字右对齐 51 52 53 //插入页眉图片(测试结果图片未插入成功) 54 wordApp.Selection.ParagraphFormat.Alignment = MSWord.WdParagraphAlignment.wdAlignParagraphCenter; 55 string headerfile = @"C:\Users\xiahui\Desktop\OficeProgram\3.jpg"; 56 MSWord.InlineShape shape1 = wordApp.ActiveWindow.ActivePane.Selection.InlineShapes.AddPicture(headerfile, ref Nothing, ref Nothing, ref Nothing); 57 shape1.Height = 5;//强行设置貌似无效,图片没有按设置的缩放——图片的比例并没有改变。 58 shape1.Width = 20; 59 wordApp.ActiveWindow.ActivePane.Selection.InsertAfter(" 文档页眉");//在页眉的图片后面追加几个字 60 61 //去掉页眉的横线 62 wordApp.ActiveWindow.ActivePane.Selection.ParagraphFormat.Borders[MSWord.WdBorderType.wdBorderBottom].LineStyle = MSWord.WdLineStyle.wdLineStyleNone; 63 wordApp.ActiveWindow.ActivePane.Selection.Borders[MSWord.WdBorderType.wdBorderBottom].Visible = false; 64 wordApp.ActiveWindow.ActivePane.View.SeekView = MSWord.WdSeekView.wdSeekMainDocument;//退出页眉设置 65 #endregion 66 67 #region 页码设置并添加页码 68 69 //为当前页添加页码 70 MSWord.PageNumbers pns = wordApp.Selection.Sections[1].Headers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers;//获取当前页的号码 71 pns.NumberStyle = MSWord.WdPageNumberStyle.wdPageNumberStyleNumberInDash;//设置页码的风格,是Dash形还是圆形的 72 pns.HeadingLevelForChapter = 0; 73 pns.IncludeChapterNumber = false; 74 pns.RestartNumberingAtSection = false; 75 pns.StartingNumber = 0; //开始页页码? 76 object pagenmbetal = MSWord.WdPageNumberAlignment.wdAlignPageNumberCenter;//将号码设置在中间 77 object first = true; 78 wordApp.Selection.Sections[1].Footers[MSWord.WdHeaderFooterIndex.wdHeaderFooterEvenPages].PageNumbers.Add(ref pagenmbetal, ref first); 79 80 #endregion 81 82 #region 行间距与缩进、文本字体、字号、加粗、斜体、颜色、下划线、下划线颜色设置 83 84 wordApp.Selection.ParagraphFormat.LineSpacing = 16f;//设置文档的行间距 85 wordApp.Selection.ParagraphFormat.FirstLineIndent = 30;//首行缩进的长度 86 //写入普通文本 87 strContent = "我是普通文本\n