C# word目录设置方法

C#可以通过Mircosoft.Office.Interop 来设置word的目录,并且修改目录的格式。

示例代码:

        /// 
        /// 增加目录
        /// 
        /// 
        public static void AddTablesOfContents(Word.Range range)
        {

            object oUseHeadingStyles = true;
            object oUpperHeadingLevel = "1";
            object oLowerHeadingLevel = "3";
            object oUseFields = true;
            object oTableID = "TableOfContents";
            object oRightAlignPageNumbers = true;
            object oIncludePageNumbers = true;
            object oAddedStyles = System.Reflection.Missing.Value;
            object oUseHyperlinks = true;
            object oHidePageNumbersInWeb = true;
            object oUseOutlineLevels = true;

            range.Document.TablesOfContents.Add(range, ref oUseHeadingStyles, ref oUpperHeadingLevel, ref oLowerHeadingLevel,
              ref oUseFields, ref oTableID, ref oRightAlignPageNumbers, ref oIncludePageNumbers, ref oAddedStyles, ref oUseHyperlinks,
              ref oHidePageNumbersInWeb, ref oUseOutlineLevels);
        }
        /// 
        /// 处理word目录部分的格式
        /// 
        /// 
        public static void ModifyListTableFontFormat(Word.Document doc)
        {
            string[] styleNames = new string[] { "目录 1", "目录 2", "目录 3", "图表目录,Table" };
            foreach (string styleName in styleNames)
            {
                try
                {
                    Word.Style style = doc.Styles[styleName];
                    style.Font.Name = "Calibri";
                    style.Font.SmallCaps = 0;
                    style.Font.AllCaps = 0;
                }
                catch (Exception)
                {
                    continue;//异常时继续处理
                }

            }
        }

你可能感兴趣的:(C#,Word,处理,c#,开发语言)