• C# 操作docx文档


     一、需要引用DocX类库文件,可以直接在NuGet中找到。

     二、创建文件,并添加一张表格

            public static string fileName = AppDomain.CurrentDomain.BaseDirectory + "youziku-5.doc";
    
            public static bool ExistsFile()
            {
                try
                {
                    if (File.Exists(fileName)) return true;
                    //创建文件
                    File.Create(fileName).Close();
    using (var document = DocX.Create(fileName)) {
    //创建一个1行3列的表格 var table = document.AddTable(1, 3); table.Design = TableDesign.LightList;//边框 table.Alignment = Alignment.center; table.Rows[0].Cells[0].Paragraphs[0].Append("字体名称"); table.Rows[0].Cells[1].Paragraphs[0].Append("字体效果"); table.Rows[0].Cells[2].Paragraphs[0].Append("字体厂商"); var p1 = document.InsertParagraph(); p1.InsertTableAfterSelf(table); // 保存当前文档 document.Save(); } return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } }

    三、向表格中追加数据 

      public static string Set(string fontName, string fontCompany)
            {
                try
                {
                    //如果不存在文件则创建
                    var result = ExistsFile();
                    if (!result) return TAjaxCallBack.ERROR;
                    using (var document = DocX.Load(fileName))
                    {
                        var imgpath = AppDomain.CurrentDomain.BaseDirectory +  "123.png";
                        // 将图像添加到文档中。    
                        var image = document.AddImage(imgpath);
                        var picture = image.CreatePicture();
    
                        var table = document.Tables[0];
                        var count = table.RowCount;
                        table.InsertRow();
                        table.Rows[count].Cells[0].Paragraphs[0].Append(fontName);
                        table.Rows[count].Cells[1].Paragraphs[0].AppendPicture(picture);
                        table.Rows[count].Cells[2].Paragraphs[0].Append(fontCompany);
                        document.Save();
                    }
    
                    return TAjaxCallBack.OK;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return TAjaxCallBack.ERROR;
                }
            }
    

    四、查询及删除  

      

      public void Set()
            {
                try
                {
                    using (var document = DocX.Load(path))
                    {
                        //获取表格
                        var table = document.Tables[0] as Table;
                        //总条数
                        var count = table.RowCount;
    
                        //查询列表中的数据
                        foreach (var item in table.Rows)
                        {
                            var str1 = item.Cells[0].Paragraphs[0].Text;
                            var str2 = item.Cells[1].Paragraphs[0].Text;
                            var str3 = item.Cells[2].Paragraphs[0].Text;
                        }
    
                        //获取其中的一条数据
                        var tableItem = table.Rows.FirstOrDefault(a => a.Cells[0].Paragraphs[0].Text == "书体坊向佳红毛笔行书");
                        var p = tableItem?.Cells[0].Paragraphs[0].Text;
    
                        //删除数据
                      table.Rows.RemoveAt(1);
    
    
                        document.Save();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
    
                }
    
            }

     更多关于操作docx文件:https://blog.csdn.net/Eiceblue/article/details/78409116

  • 相关阅读:
    03-字典
    02-列表
    01-字符串操作
    Django中的跨域问题
    Codeforces Round #617 (Div. 3) A
    Codeforces Round #717 (Div. 2) A
    如何在Vuespa中使用less
    excle导出
    ajaxFileUpload上传文件
    图片插入word
  • 原文地址:https://www.cnblogs.com/xiaoyaodijun/p/9268982.html
Copyright © 2020-2023  润新知