欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C# 在Word 创建word自定义列表样式

程序员文章站 2022-04-09 16:46:49
...
/*
using Microsoft.Office.Interop.Word;
using static Microsoft.Office.Interop.Word.WdAlertLevel;
*/

object OMissing = Missing.Value;
object docVisible = true;

Application OWord=new Application
            {
                Visible = windowVisible,
                DisplayAlerts = wdAlertsNone
            };


Document ODoc= OWord.Documents.Add(ref OMissing, ref OMissing,
                ref OMissing, ref docVisible);


 var style = wordDoc.Styles.Cast<Style>().FirstOrDefault(s => s.NameLocal == "AutoNumber") ?? wordDoc.Styles.Add("AutoNumber");
            style.set_BaseStyle(WdBuiltinStyle.wdStyleHeading1);
            style.Font.Size = float.Parse("10.5");
            style.Font.Bold = 0;


var listTemp = ODoc.Application.ListGalleries[WdListGalleryType.wdNumberGallery].ListTemplates[1];
            foreach (ListLevel lev in listTemp.ListLevels)
            {
                lev.NumberStyle = WdListNumberStyle.wdListNumberStyleArabic;
                lev.NumberFormat = @"%1、";
                lev.NumberPosition = 0;
                lev.TrailingCharacter = WdTrailingCharacter.wdTrailingNone;
                lev.TabPosition = 0;
                lev.TextPosition = 0;
                lev.Font.Size = float.Parse("10.5");
                lev.Font.Bold = 0;
                lev.LinkedStyle = "AutoNumber";

            }


//oWord.Selection.Range.set_Style(style);
oWord.Selection.Range.ListFormat.ApplyListTemplate(listTemp , OMissing, OMissing);


 

相关标签: word