NPOI的WORD填表

此篇博客介绍了如何使用NPOI库操作Word文档,通过给定的内容字典,动态地在模板中插入和替换价格、商品名、数量等信息,并展示了如何创建和修改表格。
public static void Word_FillModelForReceipt(string modelPath, string savePath, Dictionary<string, string> content, Dictionary<string, string> price = default, Dictionary<int, Models.PosItem> posItem = default)
        {
            System.IO.FileStream fs = new System.IO.FileStream(modelPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);

            NPOI.XWPF.UserModel.XWPFDocument docx = new NPOI.XWPF.UserModel.XWPFDocument(fs);//打开07(.docx)以上的版本的文档
            //遍历word中的段落
            foreach (var para in docx.Paragraphs)
            {
                foreach (var be in para.Body.BodyElements)
                {
                    if (be.GetType() == typeof(NPOI.XWPF.UserModel.XWPFParagraph))
                    {
                        var be1 = (NPOI.XWPF.UserModel.XWPFParagraph)be;
                        foreach (var kv in content)
                        {
                            if (be1.ParagraphText.Contains(kv.Key))
                            {
                                be1.ReplaceText(kv.Key, kv.Value);
                            }
                        }
                    }
                }
            }
            //插入表格
            if (price?.Count > 0)
            {
                if (docx.Tables?.Count > 0)
                {
                    NPOI.XWPF.UserModel.XWPFTable table = docx.Tables[0];
                    if (table.Rows?.Count > 0)
                    {
                        int i = 0;
                        foreach (var item in price)
                        {

                            var row = table.InsertNewTableRow(table.Rows.Count - 1);
                            var cell = row.GetCell(0) ?? row.CreateCell();
                            SetCellText(cell, table, item.Key);
                            cell = row.GetCell(1) ?? row.CreateCell();
                            cell = row.GetCell(2) ?? row.CreateCell();
                            //SetCellText(cell, table, item.Value.CountTitle);
                            cell = row.GetCell(3) ?? row.CreateCell();
                            //SetCellText(cell, table, item.Value.Count);
                            cell = row.GetCell(4) ?? row.CreateCell();
                            cell = row.GetCell(5) ?? row.CreateCell();
                            SetCellText(cell, table, "HK$", NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
                            cell = row.GetCell(6) ?? row.CreateCell();
                            SetCellText(cell, table, item.Value, NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
                            i++;
                        }
                    }
                }
            }

            //插入表格
            if (posItem?.Count > 0)
            {
                if (docx.Tables?.Count > 0)
                {
                    NPOI.XWPF.UserModel.XWPFTable table = docx.Tables[0];
                    if (table.Rows?.Count > 0)
                    {
                        int i = 0;
                        foreach (var item in posItem)
                        {

                            var row = table.InsertNewTableRow(table.Rows.Count - 1);
                            var cell = row.GetCell(0) ?? row.CreateCell();
                            SetCellText(cell, table, item.Value.Name);
                            cell = row.GetCell(1) ?? row.CreateCell();
                            cell = row.GetCell(2) ?? row.CreateCell();
                            SetCellText(cell, table, item.Value.CountTitle);
                            cell = row.GetCell(3) ?? row.CreateCell();
                            SetCellText(cell, table, item.Value.Count);
                            cell = row.GetCell(4) ?? row.CreateCell();
                            cell = row.GetCell(5) ?? row.CreateCell();
                            SetCellText(cell, table, "HK$", NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
                            cell = row.GetCell(6) ?? row.CreateCell();
                            SetCellText(cell, table, item.Value.Price, NPOI.XWPF.UserModel.ParagraphAlignment.RIGHT);
                            i++;
                        }
                    }
                }
            }

            var tr = docx.Tables[0].Rows.Last();
            var tc = tr.GetCell(5);
            //SetCellStyle(tc, docx.Tables[0]);
            tc.SetBorderBottom(NPOI.XWPF.UserModel.XWPFTable.XWPFBorderType.DOUBLE, 1, 0, NPOI.HSSF.Util.HSSFColor.Black.HexString);

            tc.Paragraphs.First().Runs.First().IsBold = true;
            tc.Paragraphs.First().Runs.First().FontSize = 9;
            tc = tr.GetCell(6);
            //SetCellStyle(tc, docx.Tables[0]);
            tc.SetBorderBottom(NPOI.XWPF.UserModel.XWPFTable.XWPFBorderType.DOUBLE, 1, 0, NPOI.HSSF.Util.HSSFColor.Black.HexString);
            tc.Paragraphs.First().Runs.First().IsBold = true;
            tc.Paragraphs.First().Runs.First().FontSize = 9;

            //遍历表格      
            foreach (var table in docx.Tables)
            {
                foreach (var row in table.Rows)
                {
                    foreach (var cell in row.GetTableCells())
                    {
                        foreach (var para in cell.Paragraphs)
                        {
                            foreach (var be in para.Body.BodyElements)
                            {
                                if (be.GetType() == typeof(NPOI.XWPF.UserModel.XWPFParagraph))
                                {
                                    var be1 = (NPOI.XWPF.UserModel.XWPFParagraph)be;
                                    foreach (var kv in content)
                                    {
                                        if (be1.ParagraphText.Contains(kv.Key))
                                        {
                                            be1.ReplaceText(kv.Key, kv.Value);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            System.IO.FileStream output = new System.IO.FileStream(savePath, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
            docx.Write(output);
            fs.Close();
            fs.Dispose();
            output.Close();
            output.Dispose();
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值