龙空技术网

Word动态生成可勾选复选框

陈晖 22

前言:

眼前大家对“word复选框不能用”大体比较看重,姐妹们都想要知道一些“word复选框不能用”的相关资讯。那么小编在网摘上网罗了一些对于“word复选框不能用””的相关知识,希望姐妹们能喜欢,大家快快来了解一下吧!

此动态生成可勾选复选框是本人经过查阅DocumentFormat.OpenXml接口文档实践整理出来的方法,并结合网络上常用生成方法进行对比,以便开发者更好的根据场景进行选择使用。如有疑问,可下方留言交流学习。

创建控制台应用程序并引用Nuget包DocumentFormat.OpenXml

在项目根目录下创建测试word文档 test.docx

创建WordHelper助手类,并封装可勾选复选框和无法勾选复选框两种方法:1、生成可在word勾选的复选框

public static Paragraph GenerateCheckboxParagraphNew(List<CheckItem> items)        {                        List<Run> runs=new List<Run>();            foreach (var item in items)            {                var checkedSym = item.IsChecked ? "0052" : "00A3";                var run1 = new Run(new SymbolChar()                {                    Char = checkedSym,                    Font = "Wingdings 2",                });                var run2 = new Run(new Text(item.Text.Insert(0, " "))                {                    Space = SpaceProcessingModeValues.Preserve                });                runs.Add(run1);                runs.Add(run2);            }            var element =                new Paragraph(runs);            return element;        }
2、无法在word勾选的复选框
public static Paragraph GenerateCheckboxParagraph(string internalName, int internalId, string textAfterTextbox,bool checkedVal=false)        {            var run1 = new Run(                    new FieldChar(                        new FormFieldData(                            new FormFieldName() { Val = internalName },                            new Enabled(),                            new CalculateOnExit() { Val = OnOffValue.FromBoolean(checkedVal) },                            new CheckBox(                                new AutomaticallySizeFormField(),                                new DefaultCheckBoxFormFieldState() { Val = OnOffValue.FromBoolean(checkedVal) }))                    )                    {                        FieldCharType = FieldCharValues.Begin                    }                );            var run2 = new Run(new FieldCode("FORMCHECKBOX") { Space = SpaceProcessingModeValues.Preserve });            var run3 = new Run(new FieldChar() { FieldCharType = FieldCharValues.End });            var run4 = new Run(new Text(textAfterTextbox));            var element =                new Paragraph(                    run1,                    new BookmarkStart() { Name = internalName, Id = new StringValue(internalId.ToString()) },                    run2,                    run3,                    new BookmarkEnd() { Id = new StringValue(internalId.ToString()) },                    run4                );            return element;        }    }
完整代码如下:

WordHelper代码

using DocumentFormat.OpenXml.Wordprocessing;using DocumentFormat.OpenXml;namespace OpenxmlDemo{    public class WordHelper    {        /// <summary>        /// 复选框项实体        /// </summary>        public class CheckItem        {            public string Text { get; set; }            public bool IsChecked { get; set; }        }        /// <summary>        /// 可在word中勾选复选框        /// </summary>        /// <param name="items"></param>        /// <returns></returns>        public static Paragraph GenerateCheckboxParagraphNew(List<CheckItem> items)        {                        List<Run> runs=new List<Run>();            foreach (var item in items)            {                var checkedSym = item.IsChecked ? "0052" : "00A3";                var run1 = new Run(new SymbolChar()                {                    Char = checkedSym,                    Font = "Wingdings 2",                });                var run2 = new Run(new Text(item.Text.Insert(0, " "))                {                    Space = SpaceProcessingModeValues.Preserve                });                runs.Add(run1);                runs.Add(run2);            }            var element =                new Paragraph(runs);            return element;        }        /// <summary>        /// 无法在word中勾选复选框        /// </summary>        /// <param name="internalName"></param>        /// <param name="internalId"></param>        /// <param name="textAfterTextbox"></param>        /// <param name="checkedVal"></param>        /// <returns></returns>        public static Paragraph GenerateCheckboxParagraph(string internalName, int internalId, string textAfterTextbox,bool checkedVal=false)        {            var run1 = new Run(                    new FieldChar(                        new FormFieldData(                            new FormFieldName() { Val = internalName },                            new Enabled(),                            new CalculateOnExit() { Val = OnOffValue.FromBoolean(checkedVal) },                            new CheckBox(                                new AutomaticallySizeFormField(),                                new DefaultCheckBoxFormFieldState() { Val = OnOffValue.FromBoolean(checkedVal) }))                    )                    {                        FieldCharType = FieldCharValues.Begin                    }                );            var run2 = new Run(new FieldCode("FORMCHECKBOX") { Space = SpaceProcessingModeValues.Preserve });            var run3 = new Run(new FieldChar() { FieldCharType = FieldCharValues.End });            var run4 = new Run(new Text(textAfterTextbox));            var element =                new Paragraph(                    run1,                    new BookmarkStart() { Name = internalName, Id = new StringValue(internalId.ToString()) },                    run2,                    run3,                    new BookmarkEnd() { Id = new StringValue(internalId.ToString()) },                    run4                );            return element;        }    }}

Program代码

using DocumentFormat.OpenXml.Packaging;using OpenxmlDemo;using static OpenxmlDemo.WordHelper;//test.docx文件路径var path=Path.Combine(AppContext.BaseDirectory,"test.docx");using (WordprocessingDocument document= WordprocessingDocument.Open(path,true)){    var body = document.MainDocumentPart!.Document.Body;    var checkBoxs = new List<CheckItem>()    {        new CheckItem()        {            Text="复选框1",            IsChecked=false        },        new CheckItem()        {            Text="复选框2",            IsChecked=true        },        new CheckItem()        {            Text="复选框3",            IsChecked=true        },    };    var paragraph = WordHelper.GenerateCheckboxParagraphNew(checkBoxs);    body.AppendChild(paragraph);    var noCheckParagraph = WordHelper.GenerateCheckboxParagraph("标签", 66, "无法勾选复选框", true);    body.AppendChild(noCheckParagraph);    document.Save();    document.Dispose();}

运行Program程序后效果如下:

标签: #word复选框不能用 #word中复选框属性无法编辑 #word复选框不能勾选