• .Net Core NOPI操作word(二) 表格操作


    一、创建表格操作

    private void btnExport_Click(object sender, EventArgs e)
            {
                var dbcontext = new BlogModel();
                var list = dbcontext.ArticleInfos.ToList();
                //创建document对象
                XWPFDocument doc = new XWPFDocument();
                //创建段落对象
                XWPFParagraph p1 = doc.CreateParagraph();
                //创建run对象
                //本节提到的所有样式都是基于XWPFRun的,
                //你可以把XWPFRun理解成一小段文字的描述对象,
                //这也是Word文档的特征,即文本描述性文档。
                //来自Tony Qu http://tonyqus.sinaapp.com/archives/609
                XWPFRun r1 = p1.CreateRun();
                r1.SetBold(true);
                r1.SetText("数据导出demo");
                r1.SetBold(true);
                r1.SetFontFamily("Arial");//设置雅黑字体
                //创建表格对象列数写死了,可根据自己需要改进或者自己想想解决方案
                XWPFTable table = doc.CreateTable(list.Count(), 4);
                for (int i = 0; i < list.Count(); i++)
                {
                    table.GetRow(i).GetCell(0).SetText(list[i].Id.ToString());
                    table.GetRow(i).GetCell(1).SetText(list[i].Title);
                    table.GetRow(i).GetCell(2).SetText(list[i].Content);
                    table.GetRow(i).GetCell(3).SetText(list[i].AddTime);
                }
                //保存文件到磁盘
                FileStream out1 = new FileStream("simpleTable.docx", FileMode.Create);
                doc.Write(out1);
                out1.Close();
            }

    二、设定单元格宽度

       table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.w = "8450";//单元格宽
                table.GetRow(0).GetCell(1).GetCTTc().AddNewTcPr().tcW.type = ST_TblWidth.dxa;
    
    CT_TcPr ctPr = cttc.AddNewTcPr();                       //添加TcPr
                        ctPr.tcW = new CT_TblWidth();
                        ctPr.tcW.w = "100";//单元格宽
                        ctPr.tcW.type = ST_TblWidth.dxa;

    三、复制单元格样式,复制行样式,创建新行

    注:表格的行没有设置样式的地方,样式设置具体到了单元格

    特别说明: 在表格SetText() 的时候不能传入null 值,尤其是字符串需要处理成“”

    XWPFTableRow row4 = table.GetRow(4);
    row4.GetCell(0).SetText(stu.JapanHistory??"");
    static void TestWord2()
    {
        //1.读取word 文档
        string tempPath = @"D:QLWorkQL.OhterLiuXue.Web.CRM数据库pinggu.docx";
        string targetPath = "D:\test_pg.docx";
    
        using (FileStream fs = new FileStream(tempPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            XWPFDocument doc = new XWPFDocument(fs);
    
            //2.修改内容
            //获取表格
            CT_Tbl tbl = doc.Document.body.getTblArray().FirstOrDefault();
            XWPFTable table = doc.GetTable(tbl);
            //第一行
            XWPFTableRow row0 = table.GetRow(0);
            string str = row0.GetCell(0).GetText();
            Console.WriteLine(str);
            row0.GetCell(0).SetText("京国际文化学院");
            row0.GetCell(1).SetText("2019年10月");
            //第三行
            XWPFTableRow row2 = table.GetRow(2);
            row2.GetCell(0).SetText("张三丰  (拼音): zhagnsanfeng");
    
            //创建一行
            //CT_Row ctrow12 = new CT_Row();
            //XWPFTableRow row12 = new XWPFTableRow(ctrow12, table);
            //table.AddRow(row12, 12);
    
            //row12.CreateCell().SetText("小学");
            //row12.CreateCell().SetText("聊城三中");
            //row12.CreateCell().SetText("2019年10月~2019年3月");
            //row12.CreateCell().SetText("5年制");
            //row12.CreateCell().SetText("计算机应用");
    
            //根据上一行创建一行,使用上一行的样式
            XWPFTableRow row11 = table.GetRow(12);
            XWPFTableCell oldCell = row11.GetCell(0);
    
            CT_Row newctRow = new CT_Row();
            XWPFTableRow rowNew = new XWPFTableRow(newctRow, table);
            XWPFTableCell cell1 = rowNew.CreateCell();
    
            cell1.SetText("小学");
            CopyCellStyle(cell1, oldCell);
    
            cell1 = rowNew.CreateCell();
            cell1.SetText("张营小学");
            oldCell = row11.GetCell(1);
            CopyCellStyle(cell1, oldCell);
    
    
            cell1 = rowNew.CreateCell();
            cell1.SetText("2019年-5月~2019年10月");
            oldCell = row11.GetCell(2);
            CopyCellStyle(cell1, oldCell);
    
            cell1 = rowNew.CreateCell();
            oldCell = row11.GetCell(3);
            CopyCellStyle(cell1, oldCell);
    
            cell1 = rowNew.CreateCell();
            oldCell = row11.GetCell(4);
            CopyCellStyle(cell1, oldCell);
    
            table.AddRow(rowNew, 12);
    
            //3.保存文件
            using (FileStream fsw = new FileStream(targetPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                doc.Write(fsw);
                doc.Close();
                Console.WriteLine("word生成成功");
            }
        }
    }
    //复制单元格样式
    static void CopyCellStyle(XWPFTableCell newCell, XWPFTableCell oldCell)
    {
        CT_Tc cttc1 = newCell.GetCTTc();
        CT_TcPr tcpr1 = cttc1.AddNewTcPr();
    
        tcpr1.tcW = oldCell.GetCTTc().tcPr.tcW;
        tcpr1.tcBorders = oldCell.GetCTTc().tcPr.tcBorders;
        tcpr1.tcMar = oldCell.GetCTTc().tcPr.tcMar;
        tcpr1.gridSpan = oldCell.GetCTTc().tcPr.gridSpan;
    }

    更多:

    .Net Core NOPI操作word(一)

    .Net Excel操作之NPOI(二)常用操作封装

    .Net Excel操作之NPOI(一)简介

  • 相关阅读:
    union 和 union all的区别
    JDBC中PreparedStatement相比Statement的好处
    25个经典的Spring面试问答
    MySQL 事务
    漫谈Linux下的音频问题(转)
    监控 Linux 性能的 18 个命令行工具(转)
    在终端中用默认程序打开文件(转)
    【转】使程序在Linux下后台运行 (关掉终端继续让程序运行的方法)
    Getting Started with Amazon EC2 (1 year free AWS VPS web hosting)
    压缩解压命令小结
  • 原文地址:https://www.cnblogs.com/tianma3798/p/10953315.html
Copyright © 2020-2023  润新知