使用OpenXml为Excel表格添加合计行

63 篇文章 0 订阅
26 篇文章 0 订阅

添加前表格:


运行后结果:


实现代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml;

namespace ConsoleApplication7
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Filter = "Excel Document|*.xlsx";
            ofd.ShowDialog();
            string opath = ofd.FileName;
            string newpath = AppDomain.CurrentDomain.BaseDirectory 
                + "test.xlsx";
            File.Copy(opath, newpath, true);
            using (SpreadsheetDocument sd = SpreadsheetDocument
                .Open(newpath, true))
            {
                WorkbookPart wp = sd.WorkbookPart;
                Workbook wb = wp.Workbook;
                Sheet s = wb.Descendants<Sheet>().Where(w => w.Name == "Sheet1")
                    .FirstOrDefault();
                WorksheetPart sp = wp.GetPartById(s.Id) as WorksheetPart;
                if (sp != null)
                {
                    SheetData sda = sp.Worksheet.Descendants<SheetData>()
                        .FirstOrDefault();
                    Row cr = new Row() { RowIndex = (UInt32Value)8U };
                    Cell c1 = CreateCell("A8", "SUBTOTAL(109,A2:A7)", "51");
                    Cell c2 = CreateCell("B8", "SUBTOTAL(109,B2:B7)", "57");
                    Cell c3 = CreateCell("C8", "SUBTOTAL(109,C2:C7)", "63");
                    cr.Append(c1);
                    cr.Append(c2);
                    cr.Append(c3);
                    sda.Append(cr);
                    CalculationChainPart ccp = wp
                        .AddNewPart<CalculationChainPart>();
                    CalculationChain cc = new CalculationChain();
                    CalculationCell cc1 = new CalculationCell()
                    {
                        CellReference = "A8",
                        SheetId = 1,
                        NewLevel = true
                    };
                    CalculationCell cc2 = new CalculationCell()
                    {
                        CellReference = "B8",
                        SheetId = 1
                    };
                    CalculationCell cc3 = new CalculationCell()
                    {
                        CellReference = "C8",
                        SheetId = 1
                    };
                    cc.Append(cc1);
                    cc.Append(cc2);
                    cc.Append(cc3);
                    ccp.CalculationChain = cc;
                }
                wb.Save();
            }
        }

        private static Cell CreateCell(string p, string p_2, string p_3)
        {
            Cell result = new Cell() { CellReference = p };
            CellFormula cf = new CellFormula() { Text = p_2 };
            CellValue cv = new CellValue() { Text = p_3 };
            result.Append(cf);
            result.Append(cv);
            return result;
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值