DocX Xceed.Words.NET操作Word,插入特殊符号

x

传送门,我们走...

DocX的Github传送门

介绍一

介绍二

写入特殊符号

开始...

自己做一个工具,要导出Word的,当时刚开始想使用Xceed.Words.NET.dll第三方插件进行开发。

但是网络上真的找不到太多相关资料来进行参考的好不好(不知道当初为毛要选择这个,听说很强大?)

然后搜到了中文资料比较多的一个第三方的插件:DocX.dll,听说也挺好用的。好吧就用它了,

并且找到了Github地址(本文中第一个门),下载一个Demo下来(感觉根据Demo调试生成下,一般操作完全Ok了),

生成Word,修改一下,感觉还不错。接着问题来了...Word中要插入特殊符号,

生成Word + 特殊符号...

/// <summary>
/// 生成Word
/// </summary>
public byte[] GetWordByte()
{
    byte[] bytes = null;
    try
    {
        List<string> list = new List<string>()
        {
            "我想要","生成","的数据。"
        };
        if (list.Count > 0)
        {
            string rootFolderPath = System.Configuration.ConfigurationManager.AppSettings["UploadFileAddress"]; ;//跟文件夹路径
            int intUserID = XY.Global.XYRequestCache.RequestState.UserID;
            string folderPath = "C:\TestPath\Word\";
            string fileName = "\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "测试.docx";
            string filePath = folderPath + fileName;
            XYFileSystem FileSystem = new XYFileSystem();
            if (File.Exists(folderPath))
            {
                System.IO.Directory.CreateDirectory(folderPath);
            }
            // Create a new document.
            using (DocX document = DocX.Create(filePath))
            {
                // Add a title
                //document.InsertParagraph("Adding Custom Properties to a document").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center;
                foreach (var item in list)
                {
                    double fontSize = 14d;
                    Paragraph retPar = document.InsertParagraph().Font("宋体").FontSize(fontSize);
                    retPar.Append(item).Font("宋体").FontSize(fontSize).AppendLine();
                    //特殊符号

                    //空心圆形
                    retPar.Append(string.Concat("u25CB", "空心圆形A")).Font("宋体").FontSize(fontSize);
                    retPar.Append(string.Concat("u25CB", "空心圆形B")).Font("宋体").FontSize(fontSize);

                    //空心方形
                    retPar.Append(string.Concat("u25A1", "空心方形A")).Font("宋体").FontSize(fontSize);
                    //下面示例中的特殊符号
                    if ("男" == "男")//纯属YuLe...
                    {
                      retPar.Append("u0052男u00A3女").Font("Wingdings 2").FontSize(fontSize);//这里设置字体类型
                    }
                    else
                    {
                      retPar.Append("u0052女u00A3男").Font("Wingdings 2").FontSize(fontSize);//这里设置字体类型
                    } } document.AddFooters();
//要添加的页脚,标识页数 //偶数[2,4,6,8...]页脚的样式... // Add the page number in the even Footers. //document.Footers.Even.InsertParagraph("").Font("Times New Roman").FontSize(9).AppendPageNumber(PageNumberFormat.normal); var evenFooter = document.Footers.Even.InsertParagraph(); evenFooter.Font("Times New Roman").FontSize(9); evenFooter.Alignment = Alignment.center; document.Footers.Even.InsertParagraph(evenFooter).AppendPageNumber(PageNumberFormat.normal); //奇数[1,3,5,7...]页脚的样式... // Add the page number in the odd Footers. //document.Footers.Odd.InsertParagraph("").Font("Times New Roman").FontSize(9).AppendPageNumber(PageNumberFormat.normal); var oddFooter = document.Footers.Even.InsertParagraph(); oddFooter.Font("Times New Roman").FontSize(9); oddFooter.Alignment = Alignment.center; document.Footers.Odd.InsertParagraph(oddFooter).AppendPageNumber(PageNumberFormat.normal); // Save this document to disk. document.Save(); } #region 将文件转换为byte[] //if (!File.Exists(filePath)) return null; using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { try { byte[] buffur = new byte[fs.Length]; fs.Read(buffur, 0, (int)fs.Length); bytes = buffur; } catch (Exception) { return null; } finally { if (fs != null) { fs.Close(); } } } #endregion //清理痕迹,打扫磁盘战场... File.Delete(filePath); } return bytes; } catch (Exception) { //DO sth ... } return bytes; }

特殊符号

这个绝对是,找了好长时间才找到!!!

直接复制原文了...

当我们要在word中输入选择框和打钩的选择框,我们可以这样:插入->符号->字体选择“Wingdings 2”。里面就有我们需要的选择框。

 

这里我们看到我们需要的打钩选择框的,字体是Wingdings 2,然后16进制的字符代码是0052,这样我们就可以利用这个来实现我们需要的字符

用之前同样的模板但是我们希望在性别这一栏采用选中框的方式展现效果,类似这样的//请看上面的代码...

 

 写在最后...

一切都搞完了,回头看了下DocX的GitHub主页...

发现原来是一回事,但是免费的中文资料就还行,付费的真的不太好找...

"

DocX is the free, open source version of Xceed Words for .NET. Originally written by Cathal Coffey, and maintained by Przemyslaw Klys, it is now maintained by Xceed.

Currently, the only difference between DocX and Xceed Words for .NET, is that Xceed Words for .NET can convert a Word document to PDF, and has professional technical support included in the subscription.

"

Edition DocX Xceed Words for .NET
Price Free $499.95
License Ms-PL Proprietary
我只是 添加错了我...
并且不 知道怎么删这个表格..

x

原文地址:https://www.cnblogs.com/love-zf/p/9508734.html