使用代码格式化word文档 - 小众知识

使用代码格式化word文档

2022-12-26 07:53:32 苏内容
  标签: word
阅读:1747

  #region 动态生成Word文档并填充数据
       
        /// <summary>
        /// 动态生成Word文档并填充数据
        /// </summary>
        /// <returns>返回自定义信息</returns>
        public static string CreateWordFile1()
        {
            string message = "";
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                string dir = System.Web.HttpContext.Current.Server.MapPath("");//首先在类库添加using System.web的引用
                if (!Directory.Exists(dir + "\\file"))
                {
                    Directory.CreateDirectory(dir + "\\file");  //创建文件所在目录
                }
                string name = DateTime.Now.ToLongDateString() + ".doc";
                object filename = dir + "\\file\\" + name;  //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);

               
                添加页眉方法一:
                //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "***有限公司" );//页眉内容

                //添加页眉方法二:
                if (WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdNormalView || WordApp.ActiveWindow.ActivePane.View.Type == Microsoft.Office.Interop.Word.WdViewType.wdOutlineView)
                {
                    WordApp.ActiveWindow.ActivePane.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdPrintView;
                }
                WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageHeader;
                string sHeader = "页眉内容";
                WordApp.Selection.HeaderFooter.LinkToPrevious = false;
                WordApp.Selection.HeaderFooter.Range.Text = sHeader;
                WordApp.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;


                //WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐  
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置

                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距

                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);//移动焦点
                WordApp.Selection.TypeParagraph();//插入段落

                //文档中创建表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range, 12, 3, ref oMissing, ref oMissing);
                //设置表格样式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleThickThinLargeGap;
                newTable.Borders.InsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;
                newTable.Columns[1].Width = 100f;
                newTable.Columns[2].Width = 220f;
                newTable.Columns[3].Width = 105f;

                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "产品详细信息表";
                newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                //合并单元格
                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;//垂直居中
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//水平居中

                //填充表格内容
                newTable.Cell(2, 1).Range.Text = "产品基本信息";
                newTable.Cell(2, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorDarkBlue;//设置单元格内字体颜色
                //合并单元格
                newTable.Cell(2, 1).Merge(newTable.Cell(2, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

                //填充表格内容
                newTable.Cell(3, 1).Range.Text = "品牌名称:";
                newTable.Cell(3, 2).Range.Text = "BrandName";
                //纵向合并单元格
                newTable.Cell(3, 3).Select();//选中一行
                object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount = 5;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                WordApp.Selection.Cells.Merge();

                //插入图片
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("images//picture.jpg")))
                {
                    string FileName = System.Web.HttpContext.Current.Server.MapPath("images//picture.jpg");//图片所在路径
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    object Anchor = WordDoc.Application.Selection.Range;
                    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度
                    WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高度
                }
                //将图片设置为四周环绕型
                Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                newTable.Cell(12, 1).Range.Text = "产品特殊属性";
                newTable.Cell(12, 1).Merge(newTable.Cell(12, 3));
                //在表格中增加行
                WordDoc.Content.Tables[1].Rows.Add(ref oMissing);

                WordDoc.Paragraphs.Last.Range.Text = "文档创建时间:" + DateTime.Now.ToString();//“落款”
                WordDoc.Paragraphs.Last.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;

                //文件保存
                WordDoc.SaveAs(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);
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                message = name + "文档生成成功";
            }
            catch
            {
                message = "文件导出异常!";
            }
            return message;
        }
        #endregion

        #region 创建并打开一个空的word文档进行编辑
       
        /// <summary>
        /// 创建并打开一个空的word文档进行编辑
        /// </summary>
        public static void OpenNewWordFileToEdit()
        {
            object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application WordApp;
            Microsoft.Office.Interop.Word.Document WordDoc;
            WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            WordApp.Visible = true;
            WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        }
        #endregion

 

 

 

 

 object oMissing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application WordApp;
            Microsoft.Office.Interop.Word.Document WordDoc;
            WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();

            #region 文档格式设置
            WordApp.ActiveDocument.PageSetup.LineNumbering.Active = 0;//行编号
            WordApp.ActiveDocument.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//页面方向
            WordApp.ActiveDocument.PageSetup.TopMargin = WordApp.CentimetersToPoints(float.Parse("2.54"));//上页边距
            WordApp.ActiveDocument.PageSetup.BottomMargin = WordApp.CentimetersToPoints(float.Parse("2.54"));//下页边距
            WordApp.ActiveDocument.PageSetup.LeftMargin = WordApp.CentimetersToPoints(float.Parse("3.17"));//左页边距
            WordApp.ActiveDocument.PageSetup.RightMargin = WordApp.CentimetersToPoints(float.Parse("3.17"));//右页边距
            WordApp.ActiveDocument.PageSetup.Gutter = WordApp.CentimetersToPoints(float.Parse("0"));//装订线位置
            WordApp.ActiveDocument.PageSetup.HeaderDistance = WordApp.CentimetersToPoints(float.Parse("1.5"));//页眉
            WordApp.ActiveDocument.PageSetup.FooterDistance = WordApp.CentimetersToPoints(float.Parse("1.75"));//页脚
            WordApp.ActiveDocument.PageSetup.PageWidth = WordApp.CentimetersToPoints(float.Parse("21"));//纸张宽度
            WordApp.ActiveDocument.PageSetup.PageHeight = WordApp.CentimetersToPoints(float.Parse("29.7"));//纸张高度
            WordApp.ActiveDocument.PageSetup.FirstPageTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
            WordApp.ActiveDocument.PageSetup.OtherPagesTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
            WordApp.ActiveDocument.PageSetup.SectionStart = Microsoft.Office.Interop.Word.WdSectionStart.wdSectionNewPage;//节的起始位置:新建页
            WordApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 0;//页眉页脚-奇偶页不同
            WordApp.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0;//页眉页脚-首页不同
            WordApp.ActiveDocument.PageSetup.VerticalAlignment = Microsoft.Office.Interop.Word.WdVerticalAlignment.wdAlignVerticalTop;//页面垂直对齐方式
            WordApp.ActiveDocument.PageSetup.SuppressEndnotes = 0;//不隐藏尾注
            WordApp.ActiveDocument.PageSetup.MirrorMargins = 0;//不设置首页的内外边距
            WordApp.ActiveDocument.PageSetup.TwoPagesOnOne = false;//不双面打印
            WordApp.ActiveDocument.PageSetup.BookFoldPrinting = false;//不设置手动双面正面打印
            WordApp.ActiveDocument.PageSetup.BookFoldRevPrinting = false;//不设置手动双面背面打印
            WordApp.ActiveDocument.PageSetup.BookFoldPrintingSheets = 1;//打印默认份数
            WordApp.ActiveDocument.PageSetup.GutterPos = Microsoft.Office.Interop.Word.WdGutterStyle.wdGutterPosLeft;//装订线位于左侧
            WordApp.ActiveDocument.PageSetup.LinesPage = 40;//默认页行数量
            WordApp.ActiveDocument.PageSetup.LayoutMode = Microsoft.Office.Interop.Word.WdLayoutMode.wdLayoutModeLineGrid;//版式模式为“只指定行网格”
            #endregion

            #region 段落格式设定
            WordApp.Selection.ParagraphFormat.LeftIndent = WordApp.CentimetersToPoints(float.Parse("0"));//左缩进
            WordApp.Selection.ParagraphFormat.RightIndent = WordApp.CentimetersToPoints(float.Parse("0"));//右缩进
            WordApp.Selection.ParagraphFormat.SpaceBefore = float.Parse("0");//段前间距
            WordApp.Selection.ParagraphFormat.SpaceBeforeAuto = 0;//
            WordApp.Selection.ParagraphFormat.SpaceAfter = float.Parse("0");//段后间距
            WordApp.Selection.ParagraphFormat.SpaceAfterAuto = 0;//
            WordApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距
            WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;//段落2端对齐
            WordApp.Selection.ParagraphFormat.WidowControl = 0;//孤行控制
            WordApp.Selection.ParagraphFormat.KeepWithNext = 0;//与下段同页
            WordApp.Selection.ParagraphFormat.KeepTogether = 0;//段中不分页
            WordApp.Selection.ParagraphFormat.PageBreakBefore = 0;//段前分页
            WordApp.Selection.ParagraphFormat.NoLineNumber = 0;//取消行号
            WordApp.Selection.ParagraphFormat.Hyphenation = 1;//取消段字
            WordApp.Selection.ParagraphFormat.FirstLineIndent = WordApp.CentimetersToPoints(float.Parse("0"));//首行缩进
            WordApp.Selection.ParagraphFormat.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevelBodyText;
            WordApp.Selection.ParagraphFormat.CharacterUnitLeftIndent = float.Parse("0");
            WordApp.Selection.ParagraphFormat.CharacterUnitRightIndent = float.Parse("0");
            WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = float.Parse("0");
            WordApp.Selection.ParagraphFormat.LineUnitBefore = float.Parse("0");
            WordApp.Selection.ParagraphFormat.LineUnitAfter = float.Parse("0");
            WordApp.Selection.ParagraphFormat.AutoAdjustRightIndent = 1;
            WordApp.Selection.ParagraphFormat.DisableLineHeightGrid = 0;
            WordApp.Selection.ParagraphFormat.FarEastLineBreakControl = 1;
            WordApp.Selection.ParagraphFormat.WordWrap = 1;
            WordApp.Selection.ParagraphFormat.HangingPunctuation = 1;
            WordApp.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;
            WordApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 1;
            WordApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 1;
            WordApp.Selection.ParagraphFormat.BaseLineAlignment = Microsoft.Office.Interop.Word.WdBaselineAlignment.wdBaselineAlignAuto;
            #endregion

            #region 字体格式设定
            WordApp.Selection.Font.NameFarEast = "华文中宋";
            WordApp.Selection.Font.NameAscii = "Times New Roman";
            WordApp.Selection.Font.NameOther = "Times New Roman";
            WordApp.Selection.Font.Name = "宋体";
            WordApp.Selection.Font.Size = float.Parse("14");
            WordApp.Selection.Font.Bold = 0;
            WordApp.Selection.Font.Italic = 0;
            WordApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
            WordApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
            WordApp.Selection.Font.StrikeThrough = 0;//删除线
            WordApp.Selection.Font.DoubleStrikeThrough = 0;//双删除线
            WordApp.Selection.Font.Outline = 0;//空心
            WordApp.Selection.Font.Emboss = 0;//阳文
            WordApp.Selection.Font.Shadow = 0;//阴影
            WordApp.Selection.Font.Hidden = 0;//隐藏文字
            WordApp.Selection.Font.SmallCaps = 0;//小型大写字母
            WordApp.Selection.Font.AllCaps = 0;//全部大写字母
            WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
            WordApp.Selection.Font.Engrave = 0;//阴文
            WordApp.Selection.Font.Superscript = 0;//上标
            WordApp.Selection.Font.Subscript = 0;//下标
            WordApp.Selection.Font.Spacing = float.Parse("0");//字符间距
            WordApp.Selection.Font.Scaling = 100;//字符缩放
            WordApp.Selection.Font.Position = 0;//位置
            WordApp.Selection.Font.Kerning = float.Parse("1");//字体间距调整
            WordApp.Selection.Font.Animation = Microsoft.Office.Interop.Word.WdAnimation.wdAnimationNone;//文字效果
            WordApp.Selection.Font.DisableCharacterSpaceGrid = false;
            WordApp.Selection.Font.EmphasisMark = Microsoft.Office.Interop.Word.WdEmphasisMark.wdEmphasisMarkNone;

            #endregion




Paragraph 对象'代表所选内容、范围或文档中的一个段落。Paragraph 对象是 Paragraphs 集合的一个成员。Paragraphs 集合包含所选内容、范围或文档中的所有段落。


方法
CloseUp'清除指定段落前的任何间距。
Indent'为一个或多个段落增加一个级别的缩进。
IndentCharWidth'将段落缩进指定的字符数。
IndentFirstLineCharWidth'将一个或多个段落的首行缩进指定的字符数。
JoinList'将列表段落与距指定段落前后最近的列表进行连接。
ListAdvanceTo'为列表中的段落设置列表级别。
Next'返回一个 Paragraph 对象,该对象代表下一段。
OpenOrCloseUp'切换段前间距。
OpenUp'为指定段落设置 12 磅的段前间距。
Outdent'为一个或多个段落删除一个级别的缩进。
OutlineDemote'对指定段落应用下一个标题级别样式(从“标题 1”到“标题 8”)。
OutlineDemoteToBody'通过应用“正文”样式将指定段落降级为正文文本。
OutlinePromote'对指定段落应用上一个标题级别样式(从“标题 1”到“标题 8”)。
Previous'将上一段作为一个 Paragraph 对象返回。
Reset'删除手动段落格式(不使用样式应用的格式)。
ResetAdvanceTo'将使用自定义列表级别的段落重置为原始级别设置。
SelectNumber'选择列表中的编号或项目符号。
SeparateList'将列表分隔为两个单独的列表。对于编号的列表,新列表将从起始编号(通常为 1)开始重新编号。
Space1'为指定段落设置单倍行距。
Space15'为指定段落设置 1.5 倍行距。
Space2'为指定段落设置 2 倍行距。
TabHangingIndent'将悬挂缩进量设置为指定的制表位数。
TabIndent'将指定段落的左缩进量设置为指定的制表位数。


属性
AddSpaceBetweenFarEastAndAlpha'如果 Microsoft Word 将自动在指定段落的日文和拉丁文文字之间添加空格,则该属性值为 True。如果仅对于某些指定段落将该属性设置为 True,则该属性会返回 wdUndefined。Long 类型,可读写。
AddSpaceBetweenFarEastAndDigit'如果 Microsoft Word 将自动在指定段落的日文文字和数字之间添加空格,则该属性值为 True。如果仅对于某些指定段落将该属性设置为 True,则该属性会返回 wdUndefined。Long 类型,可读写。
Alignment'返回或设置一个 WdParagraphAlignment 常量,该常量代表指定段落的对齐方式,可读写。
Application'返回一个代表 Microsoft Word 应用程序的 Application 对象。
AutoAdjustRightIndent'如果 Microsoft Word 会根据您指定的每行字符数自动调整指定段落的右缩进,则该属性值为 True。如果只将某些指定段落的 AutoAdjustRightIndent 属性设置为 True,则该属性会返回 wdUndefined。Long 类型,可读写。
BaseLineAlignment'返回或设置一个 WdBaselineAlignment 常量,该常量代表行中字体的垂直位置,可读写。
Borders'返回一个 Borders 集合,该集合代表指定段落的所有边框。
CharacterUnitFirstLineIndent'返回或设置首行或悬挂缩进的值(以字符为单位)。用正值设置首行缩进,用负值设置悬挂缩进。Single 类型,可读写。
CharacterUnitLeftIndent'该属性返回或设置指定段落的左缩进量(以字符为单位)。Single 类型,可读写。
CharacterUnitRightIndent'该属性返回或设置指定段落的右缩进量(以字符为单位)。Single 类型,可读写。
Creator'返回一个 32 位整数,该整数代表在其中创建特定对象的应用程序。只读 Long 类型。
DisableLineHeightGrid'如果该属性的值为 True,则当指定每页的行数时,Microsoft Word 会将指定段落中的字符与行网格对齐。如果只将某些指定段落的 DisableLineHeightGrid 属性设置为 True,则返回 wdUndefined。Long 类型,可读写。
DropCap'返回一个 DropCap 对象,该对象代表指定段落中格式为首字下沉的大写字母。只读。
FarEastLineBreakControl'如果为 True,则 Microsoft Word 会将东亚语言文字的换行规则应用于指定的段落。如果只将某些指定段落的 FarEastLineBreakControl 属性设定为 True,则返回 wdUndefined。Long 类型,可读写。
FirstLineIndent'返回或设置首行缩进或悬挂缩进的大小(以磅值表示)。用正数设置首行缩进的尺寸,用负数设置悬挂缩进的尺寸。Single 类型,可读写。
Format'返回或设置一个 ParagraphFormat 对象,该对象代表指定的一个或多个段落的格式。
HalfWidthPunctuationOnTopOfLine'如果为 True,则 Microsoft Word 会将指定段落行首的标点符号改为半角字符。如果仅将某些指定段落的该属性设置为 True,则此属性将返回 wdUndefined。Long 类型,可读写。
HangingPunctuation'如果为 True,则指定段落中的标点将可以溢出边界。如果仅将某些指定段落的该属性设置为 True,则返回 wdUndefined。Long 类型,可读写。
Hyphenation'如果指定的段落进行自动断字,则该属性值为 True。如果指定的段落不进行自动断字,则该属性值为 False。可读写 Long 类型。
ID'在当前文档另存为网页时,返回或设置指定对象的标识标签。可读写 String 类型。
IsStyleSeparator'如果段落包含特殊隐藏段落标记,而利用该标记 Microsoft Word 可以显示合并不同段落样式的段落,则该属性值为 True。Boolean 类型,只读。
KeepTogether'在 Microsoft Word 对文档重新分页时,如果指定段落中的所有行都位于同一页上,则该属性值为 True。可读写 Long 类型。
KeepWithNext'在 Microsoft Word 对文档重新分页时,如果指定段落与它的下一段位于同一页上,则该属性值为 True。可读写 Long 类型。
LeftIndent'返回或设置一个 Single 类型的值,该值代表指定段落的左缩进值(以磅为单位)。可读写。
LineSpacing'返回或设置指定段落的行距(以磅为单位)。Single 类型,可读写。
LineSpacingRule'返回或设置指定段落的行距。可读写 WdLineSpacing 类型。
LineUnitAfter'返回或设置指定段落的段后间距(以网格线为单位)。可读写 Single 类型。
LineUnitBefore'返回或设置指定段落的段前间距(以网格线为单位)。可读写 Single 类型。
ListNumberOriginal'返回一个 Integer 类型的值,该值代表段落的原始列表级别。只读。
MirrorIndents'返回或设置一个 Long 类型的值,该值代表左缩进和右缩进的宽度是否相同。该属性值可以是 True、False 或 wdUndefined。可读写。
NoLineNumber'如果取消指定段落的行号,则该属性值为 True。可读写 Long 类型。
OutlineLevel'返回或设置指定段落的大纲级别。可读写 WdOutlineLevel 类型。
PageBreakBefore'如果在指定段落前插入了分页符,则该属性值为 True。可读写 Long 类型。
Parent'返回一个 Object 类型值,该值代表指定 Paragraph 对象的父对象。
Range'返回一个 Range 对象,该对象代表指定段落中包含的文档部分。
ReadingOrder'返回或设置指定段落的读取次序而不改变其对齐方式。可读写 WdReadingOrder 类型。
RightIndent'返回或设置指定段落的右缩进(以磅为单位)。可读写 Single 类型。
Shading'返回一个 Shading 对象,该对象引用指定段落的底纹格式。
SpaceAfter'返回或设置指定段落或文本栏后面的间距(以磅为单位)。可读/写 Single 类型。
SpaceAfterAuto'如果 Microsoft Word 自动设置指定段落的段后间距,则该属性为 True。可读/写 Long 类型。
SpaceBefore'返回或设置指定段落的段前间距(以磅为单位)。可读/写 Single 类型。
SpaceBeforeAuto'如果 Microsoft Word 自动设置指定段落的段前间距,则该属性为 True。可读/写 Long 类型。
Style'返回或设置指定对象的样式。可读写 Variant 类型。
TabStops'返回或设置 TabStops 集合,该集合表示指定段落的所有自定义制表位。可读写。
TextboxTightWrap'返回或设置一个 WdTextboxTightWrap 常量,该常量代表文本环绕形状或文本框的紧密程度。可读写。
WidowControl'在 Word 对文档重新分页时,如果指定段落的首行和末行与段落的其他各行同页,则该属性值为 True。可读写 Long 类型。
WordWrap'如果 Microsoft Word 在指定段落或文本框架的西文单词中间断字换行,则该属性值为 True。可读写 Long 类型。

编程中 word 所有属性

word文档工程变量的

     //合并单元格
   table.Cell(2, 2).Merge(table.Cell(2, 3));

//单元格分离
    object Rownum = 2;
    object Columnnum = 2;
    table.Cell(2, 2).Split(ref Rownum, ref Columnnum);

//单元格对齐方式
     WApp.Selection.Cells.VerticalAlignment =Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalCenter;

//插入表行
     table.Rows.Add(ref missing);

//分页 object ib = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;
    WApp.Selection.InsertBreak(ref ib);

//换行
     WApp.Selection.TypeParagraph();

 


二、word文档设置

WApp.ActiveDocument.PageSetup.LineNumbering.Active =0;//行编号
            WApp.ActiveDocument.PageSetup.Orientation =Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//页面方向
            WApp.ActiveDocument.PageSetup.TopMargin =WApp.CentimetersToPoints(float.Parse("2.54"));//上页边距
            WApp.ActiveDocument.PageSetup.BottomMargin = WApp.CentimetersToPoints(float.Parse("2.54"));//下页边距
            WApp.ActiveDocument.PageSetup.LeftMargin = WApp.CentimetersToPoints(float.Parse("3.17"));//左页边距
            WApp.ActiveDocument.PageSetup.RightMargin = WApp.CentimetersToPoints(float.Parse("3.17"));//右页边距
            WApp.ActiveDocument.PageSetup.Gutter = WApp.CentimetersToPoints(float.Parse("0"));//装订线位置
            WApp.ActiveDocument.PageSetup.HeaderDistance = WApp.CentimetersToPoints(float.Parse("1.5"));//页眉
            WApp.ActiveDocument.PageSetup.FooterDistance = WApp.CentimetersToPoints(float.Parse("1.75"));//页脚
            WApp.ActiveDocument.PageSetup.PageWidth = WApp.CentimetersToPoints(float.Parse("21"));//纸张宽度
            WApp.ActiveDocument.PageSetup.PageHeight = WApp.CentimetersToPoints(float.Parse("29.7"));//纸张高度
            WApp.ActiveDocument.PageSetup.FirstPageTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
            WApp.ActiveDocument.PageSetup.OtherPagesTray = Microsoft.Office.Interop.Word.WdPaperTray.wdPrinterDefaultBin;//纸张来源
            WApp.ActiveDocument.PageSetup.SectionStart = Microsoft.Office.Interop.Word.WdSectionStart.wdSectionNewPage;//节的起始位置:新 建页
            WApp.ActiveDocument.PageSetup.OddAndEvenPagesHeaderFooter = 0;//页眉页脚-奇偶页不同
            WApp.ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter = 0;//页眉页脚-首页不同
            WApp.ActiveDocument.PageSetup.VerticalAlignment = Microsoft.Office.Interop.Word.WdVerticalAlignment.wdAlignVerticalTop;//页 面垂直对齐方式
            WApp.ActiveDocument.PageSetup.SuppressEndnotes =0;//不隐藏尾注
            WApp.ActiveDocument.PageSetup.MirrorMargins = 0;//不设置首页的内外边距
            WApp.ActiveDocument.PageSetup.TwoPagesOnOne = false;//不双面打印
            WApp.ActiveDocument.PageSetup.BookFoldPrinting =false;//不设置手动双面正面打印
            WApp.ActiveDocument.PageSetup.BookFoldRevPrinting =false;//不设置手动双面背面打印
            WApp.ActiveDocument.PageSetup.BookFoldPrintingSheets = 1;//打印默认份数
            WApp.ActiveDocument.PageSetup.GutterPos = Microsoft.Office.Interop.Word.WdGutterStyle.wdGutterPosLeft;//装订线位于左侧
            WApp.ActiveDocument.PageSetup.LinesPage = 40;//默认页行数量
            WApp.ActiveDocument.PageSetup.LayoutMode = Microsoft.Office.Interop.Word.WdLayoutMode.wdLayoutModeLineGrid;//版式模式为 “只指定行网格”

   三、光标移动

//移动光标
//光标下移3行 上移3行
            object unit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
            object count = 3;
            WApp.Selection.MoveEnd(ref unit,ref count);
            WApp.Selection.MoveUp(ref unit, ref count, ref missing);
           
//Microsoft.Office.Interop.Word.WdUnits说明
            //wdCell                  A cell.
            //wdCharacter             A character.
            //wdCharacterFormatting   Character formatting.
            //wdColumn                A column.
            //wdItem                  The selected item.
            //wdLine                  A line. //行
            //wdParagraph             A paragraph.
            //wdParagraphFormatting   Paragraph formatting.
            //wdRow                   A row.
            //wdScreen                The screen dimensions.
            //wdSection               A section.
            //wdSentence              A sentence.
            //wdStory                 A story.
            //wdTable                 A table.
            //wdWindow                A window.
            //wdWord                  A word.

//录制的vb宏
            //     ,移动光标至当前行首
            //    Selection.HomeKey unit:=wdLine
            //    '移动光标至当前行尾
            //    Selection.EndKey unit:=wdLine
            //    '选择从光标至当前行首的内容
            //    Selection.HomeKey unit:=wdLine, Extend:=wdExtend
            //    '选择从光标至当前行尾的内容
            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend
            //    '选择当前行
            //    Selection.HomeKey unit:=wdLine
            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend
            //    '移动光标至文档开始
            //    Selection.HomeKey unit:=wdStory
            //    '移动光标至文档结尾
            //    Selection.EndKey unit:=wdStory
            //    '选择从光标至文档开始的内容
            //    Selection.HomeKey unit:=wdStory, Extend:=wdExtend
            //    '选择从光标至文档结尾的内容
            //    Selection.EndKey unit:=wdStory, Extend:=wdExtend
            //    '选择文档全部内容(从WholeStory可猜出Story应是当前文档的意思)
            //    Selection.WholeStory
            //    '移动光标至当前段落的开始
            //    Selection.MoveUp unit:=wdParagraph
            //    '移动光标至当前段落的结尾
            //    Selection.MoveDown unit:=wdParagraph
            //    '选择从光标至当前段落开始的内容
            //    Selection.MoveUp unit:=wdParagraph, Extend:=wdExtend
            //    '选择从光标至当前段落结尾的内容
            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
            //    '选择光标所在段落的内容
            //    Selection.MoveUp unit:=wdParagraph
            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
            //    '显示选择区的开始与结束的位置,注意:文档第1个字符的位置是0
            //    MsgBox ("第" & Selection.Start & "个字符至第" & Selection.End & "个字符")
            //    '删除当前行
            //    Selection.HomeKey unit:=wdLine
            //    Selection.EndKey unit:=wdLine, Extend:=wdExtend
            //    Selection.Delete
            //    '删除当前段落
            //    Selection.MoveUp unit:=wdParagraph
            //    Selection.MoveDown unit:=wdParagraph, Extend:=wdExtend
            //    Selection.Delete


//表格的光标移动
//光标到当前光标所在表格的地单元格
WApp.Selection.Tables[1].Cell(1, 1).Select();
//unit对象定义
object unith = Microsoft.Office.Interop.Word.WdUnits.wdRow;//表格行方式
            object extend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;/**extend对光标移动区域进行扩展选择
            object unitu = Microsoft.Office.Interop.Word.WdUnits.wdLine;//文档行方式,可以看成表格一行.不过和wdRow有区别
            object unitp = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;//段落方式,对于表格可以选择到表格行后的换车符,对于跨行合并的行选择,我能找到的最简单方式
            object count=1;//光标移动量
下面代码演示对于存在合并单元格的选择操作.合并单元格的选择问题一直是word的bug.部分object对象参照上面代码

 

上面这个是表格合并样式.要如何才能选择2行标题栏尼.看下面代码


//定位到表格第1单元格
WApp.Selection.Tables[1].Cell(1, 1).Select();
//定位到第1个单元格第1个字符前         
WApp.Selection.HomeKey(ref unith, ref missing);
//扩展到行尾,选择表第1行           
WApp.Selection.EndKey(ref unith, ref extend);
//定义表格标题的行数量,titlerow为参数           
object strtitlerow=titlerow-1;
//移动光标选择第1行的末尾段落标记           
WApp.Selection.MoveDown(ref unitp, ref count, ref extend);
//选择下一行,因为合并的原因,如表格标题最后列是合并,只选择了2行的部分
            WApp.Selection.MoveDown(ref unitu, ref strtitlerow, ref extend);
//扩展到该行的末端,保证合并行能全部选择到
            WApp.Selection.EndKey(ref unith, ref extend);
//复制选择内容到剪贴板
            WApp.Selection.Copy();
//下面是移动光标到任何位置并粘贴内容.我程序中目的是到表格换页的时候自动插入下一页的表头.
            WApp.Selection.Tables[1].Cell(System.Convert.ToInt32(strRownum), 1).Select();
            WApp.Selection.HomeKey(ref unith, ref missing);
            WApp.Selection.Paste();
四、段落格式设定
//段落格式设定
            WApp.Selection.ParagraphFormat.LeftIndent = WApp.CentimetersToPoints(float.Parse("0"));//左缩进
            WApp.Selection.ParagraphFormat.RightIndent = WApp.CentimetersToPoints(float.Parse("0"));//右缩进
            WApp.Selection.ParagraphFormat.SpaceBefore =float.Parse("0");//段前间距
            WApp.Selection.ParagraphFormat.SpaceBeforeAuto =0;//
            WApp.Selection.ParagraphFormat.SpaceAfter = float.Parse("0");//段后间距
            WApp.Selection.ParagraphFormat.SpaceAfterAuto = 0;//
            WApp.Selection.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;//单倍行距
            WApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphJustify;// 段落2端对齐
            WApp.Selection.ParagraphFormat.WidowControl = 0;//孤行控制
            WApp.Selection.ParagraphFormat.KeepWithNext = 0;//与下段同页
            WApp.Selection.ParagraphFormat.KeepTogether = 0;//段中不分页
            WApp.Selection.ParagraphFormat.PageBreakBefore = 0;//段前分页
            WApp.Selection.ParagraphFormat.NoLineNumber = 0;//取消行号
            WApp.Selection.ParagraphFormat.Hyphenation = 1;//取消段字
            WApp.Selection.ParagraphFormat.FirstLineIndent = WApp.CentimetersToPoints(float.Parse("0"));//首行缩进
            WApp.Selection.ParagraphFormat.OutlineLevel = Microsoft.Office.Interop.Word.WdOutlineLevel.wdOutlineLevelBodyText;
            WApp.Selection.ParagraphFormat.CharacterUnitLeftIndent = float.Parse("0");
            WApp.Selection.ParagraphFormat.CharacterUnitRightIndent = float.Parse("0");
            WApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = float.Parse("0");
            WApp.Selection.ParagraphFormat.LineUnitBefore = float.Parse("0");
            WApp.Selection.ParagraphFormat.LineUnitAfter = float.Parse("0");
            WApp.Selection.ParagraphFormat.AutoAdjustRightIndent = 1;
            WApp.Selection.ParagraphFormat.DisableLineHeightGrid =0;
            WApp.Selection.ParagraphFormat.FarEastLineBreakControl =1;
            WApp.Selection.ParagraphFormat.WordWrap = 1;
            WApp.Selection.ParagraphFormat.HangingPunctuation = 1;
            WApp.Selection.ParagraphFormat.HalfWidthPunctuationOnTopOfLine = 0;
            WApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndAlpha = 1;
            WApp.Selection.ParagraphFormat.AddSpaceBetweenFarEastAndDigit = 1;
            WApp.Selection.ParagraphFormat.BaseLineAlignment = Microsoft.Office.Interop.Word.WdBaselineAlignment.wdBaselineAlignAuto;

五、字体格式设定
//字体格式设定
            WApp.Selection.Font.NameFarEast = "华文中宋";
            WApp.Selection.Font.NameAscii = "Times New Roman";
            WApp.Selection.Font.NameOther = "Times New Roman";
            WApp.Selection.Font.Name = "宋体";
            WApp.Selection.Font.Size = float.Parse("14");
            WApp.Selection.Font.Bold = 0;
            WApp.Selection.Font.Italic = 0;
            WApp.Selection.Font.Underline = Microsoft.Office.Interop.Word.WdUnderline.wdUnderlineNone;
            WApp.Selection.Font.UnderlineColor = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
            WApp.Selection.Font.StrikeThrough =0;//删除线
            WApp.Selection.Font.DoubleStrikeThrough = 0;//双删除线
            WApp.Selection.Font.Outline =0;//空心
            WApp.Selection.Font.Emboss = 0;//阳文
            WApp.Selection.Font.Shadow = 0;//阴影
            WApp.Selection.Font.Hidden = 0;//隐藏文字
            WApp.Selection.Font.SmallCaps = 0;//小型大写字母
            WApp.Selection.Font.AllCaps = 0;//全部大写字母
            WApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorAutomatic;
            WApp.Selection.Font.Engrave = 0;//阴文
            WApp.Selection.Font.Superscript = 0;//上标
            WApp.Selection.Font.Subscript = 0;//下标
            WApp.Selection.Font.Spacing = float.Parse("0");//字符间距
            WApp.Selection.Font.Scaling = 100;//字符缩放
            WApp.Selection.Font.Position = 0;//位置
            WApp.Selection.Font.Kerning = float.Parse("1");//字体间距调整
            WApp.Selection.Font.Animation = Microsoft.Office.Interop.Word.WdAnimation.wdAnimationNone;//文字效果
            WApp.Selection.Font.DisableCharacterSpaceGrid =false;
            WApp.Selection.Font.EmphasisMark = Microsoft.Office.Interop.Word.WdEmphasisMark.wdEmphasisMarkNone;

六、终于找到了获取光标位置的东东。那里找到的忘了,感谢提供的老大。放到这里供大家参考。
有了这个和上面内容,相信大家对word文档的控制应该到了随心所欲的地步,爽啊
获取的c#语法 //get_Information
Selection.get_Information(WdInformation.wdActiveEndPageNumber)
//关于行号-页号-列号-位置
            //information 属性
            //返回有关指定的所选内容或区域的信息。variant 类型,只读。
            //expression.information(type)
            //expression 必需。该表达式返回一个 range 或 selection 对象。
            //type long 类型,必需。需要返回的信息。可取下列 wdinformation 常量之一:
            //wdactiveendadjustedpagenumber 返回页码,在该页中包含指定的所选内容或区域的活动结尾。如果设置了一个起始页码,并对页码进行了手工调整,则返回调整过的页码。
            //wdactiveendpagenumber 返回页码,在该页中包含指定的所选内容或区域的活动结尾,页码从文档的开头开始计算而不考虑对页码的任何手工调整。
            //wdactiveendsectionnumber 返回节号,在该节中包含了指定的所选内容或区域的活动结尾。
            //wdatendofrowmarker 如果指定的所选内容或区域位于表格的行结尾标记处,则本参数返回 true。
            //wdcapslock 如果大写字母锁定模式有效,则本参数返回 true。
            //wdendofrangecolumnnumber 返回表格列号,在该表格列中包含了指定的所选内容或区域的活动结尾。
            //wdendofrangerownumber 返回表格行号,在该表格行包含了指定的所选内容或区域的活动结尾。
            //wdfirstcharactercolumnnumber 返回指定的所选内容或区域中第一个字符的位置。如果所选内容或区域是折叠的,则返回所选内容或区域右侧紧接着的字符编号。
            //wdfirstcharacterlinenumber 返回所选内容中第一个字符的行号。如果 pagination 属性为 false,或 draft 属性为 true,则返回 - 1。
            //wdframeisselected 如果所选内容或区域是一个完整的图文框文本框,则本参数返回 true。
            //wdheaderfootertype 返回一个值,该值表明包含了指定的所选内容或区域的页眉或页脚的类型,如下表所示。 值 页眉或页脚的类型
            //- 1 无
            //0 偶数页页眉
            //1 奇数页页眉
            //2 偶数页页脚
            //3 奇数页页脚
            //4 第一个页眉
            //5 第一个页脚
            //wdhorizontalpositionrelativetopage 返回指定的所选内容或区域的水平位置。该位置是所选内容或区域的左边与页面的左边之间的距离,以磅为单位。如果所选内容或区域不可见,则返回 - 1。
            //wdhorizontalpositionrelativetotextboundary 返回指定的所选内容或区域相对于周围最近的正文边界的左边的水平位置,以磅为单位。如果所选内容或区域没有显示在当前屏幕,则本参数返回 - 1。
            //wdinclipboard 有关此常量的详细内容,请参阅 microsoft office 98 macintosh 版的语言参考帮助。
            //wdincommentpane 如果指定的所选内容或区域位于批注窗格,则返回 true。
            //wdinendnote 如果指定的所选内容或区域位于页面视图的尾注区内,或者位于普通视图的尾注窗格中,则本参数返回 true。
            //wdinfootnote 如果指定的所选内容或区域位于页面视图的脚注区内,或者位于普通视图的脚注窗格中,则本参数返回 true。
            //wdinfootnoteendnotepane 如果指定的所选内容或区域位于页面视图的脚注或尾注区内,或者位于普通视图的脚注或尾注窗格中,则本参数返回 true。详细内容,请参阅前面的 wdinfootnote 和 wdinendnote 的说明。
            //wdinheaderfooter 如果指定的所选内容或区域位于页眉或页脚窗格中,或者位于页面视图的页眉或页脚中,则本参数返回 true。
            //wdinmasterdocument 如果指定的所选内容或区域位于主控文档中,则本参数返回 true。
            //wdinwordmail 返回一个值,该值表明了所选内容或区域的的位置,如下表所示。值 位置
            //0 所选内容或区域不在一条电子邮件消息中。
            //1 所选内容或区域位于正在发送的电子邮件中。
            //2 所选内容或区域位于正在阅读的电子邮件中。
            //wdmaximumnumberofcolumns 返回所选内容或区域中任何行的最大表格列数。
            //wdmaximumnumberofrows 返回指定的所选内容或区域中表格的最大行数。
            //wdnumberofpagesindocument 返回与所选内容或区域相关联的文档的页数。
            //wdnumlock 如果 num lock 有效,则本参数返回 true。
            //wdovertype 如果改写模式有效,则本参数返回 true。可用 overtype 属性改变改写模式的状态。
            //wdreferenceoftype 返回一个值,该值表明所选内容相对于脚注、尾注或批注引用的位置,如下表所示。 值 描述
            //— 1 所选内容或区域包含、但不只限定于脚注、尾注或批注引用中。
            //0 所选内容或区域不在脚注、尾注或批注引用之前。
            //1 所选内容或区域位于脚注引用之前。
            //2 所选内容或区域位于尾注引用之前。
            //3 所选内容或区域位于批注引用之前。
            //wdrevisionmarking 如果修订功能处于活动状态,则本参数返回 true。
            //wdselectionmode 返回一个值,该值表明当前的选定模式,如下表所示。 值 选定模式
            //0 常规选定
            //1 扩展选定
            //2 列选定
            //wdstartofrangecolumnnumber 返回所选内容或区域的起点所在的表格的列号。
            //wdstartofrangerownumber 返回所选内容或区域的起点所在的表格的行号。
            //wdverticalpositionrelativetopage 返回所选内容或区域的垂直位置,即所选内容的上边与页面的上边之间的距离,以磅为单位。如果所选内容或区域没有显示在屏幕上,则本参数返回 - 1。
            //wdverticalpositionrelativetotextboundary 返回所选内容或区域相对于周围最近的正文边界的上边的垂直位置,以磅为单位。如果所选内容或区域没有显示在屏幕上,则本参数返回 - 1。
            //wdwithintable 如果所选内容位于一个表格中,则本参数返回 true。
            //wdzoompercentage 返回由 percentage 属性设置的当前的放大百分比。


扩展阅读
相关阅读
© CopyRight 2010-2021, PREDREAM.ORG, Inc.All Rights Reserved. 京ICP备13045924号-1