使用EPPLUS对excel行进行分组

时间:2017-01-13 14:02:44

标签: c# excel vba epplus

当行的值与前一行相同时,我需要的是组行,“B3”的值与“B2”相同,如下所示:

enter image description here

我在使用epplus的c#中,我看到我如何使用大纲选项做类似的事情,并且类似于我想要的,但是这个选项有一些缺点,例如那些不自动的基于价值观的群体,不能做各种群体......

是否可以使用EPPLUS执行此操作?如果它不可行,我如何将vba代码添加到c#?,我试试这个:

  StringBuilder vbaCode = new StringBuilder();

  vbaCode.AppendLine("Sheets('Sheet1').Activate");
  vbaCode.AppendLine("Range('A1: D11').Select");
  vbaCode.AppendLine("Selection.Subtotal GroupBy:= 1, Function:= xlSum, TotalList:= Array(2, 3),Replace:= True, PageBreaks:= False, SummaryBelowData:= True");
pck.Save();

但无法正常工作,我无法打开Excel文件。

修改

现在使用sugested我尝试使用组函数进行Interop,但是为了一个额外的原因他将列分组而不是行,这是代码:

 var ExApp = new Microsoft.Office.Interop.Excel.Application();
 Microsoft.Office.Interop.Excel.Workbooks Wbs = ExApp.Workbooks;
 Microsoft.Office.Interop.Excel.Workbook Wb = Wbs.Open(fi.FullName.ToString());
 Microsoft.Office.Interop.Excel.Sheets wss = Wb.Worksheets;
 Microsoft.Office.Interop.Excel.Worksheet Ws = (Microsoft.Office.Interop.Excel.Worksheet)wss.get_Item("Sheet1");
 Ws.Range["A6:A10"].Group();
 Ws.Outline.SummaryRow =Microsoft.Office.Interop.Excel.XlSummaryRow.xlSummaryAbove;
 ExApp.Visible = true;

2 个答案:

答案 0 :(得分:5)

我看到这已经得到了回答,但我想我会提供一种EPPlus方式,你当然可以这样做,但你需要手动创建总和单元格:

[TestMethod]
public void Row_Grouping_Test()
{
    //http://stackoverflow.com/questions/41636336/grouping-excel-rows-with-epplus

    //Throw in some data
    var datatable = new DataTable("tblData");
    datatable.Columns.AddRange(new[]
    {
        new DataColumn("Header", typeof (string)), new DataColumn("Col1", typeof (int)), new DataColumn("Col2", typeof (int)), new DataColumn("Col3", typeof (object))
    });

    for (var i = 0; i < 10; i++)
    {
        var row = datatable.NewRow();
        row[0] = $"Header {i}"; row[1] = i; row[2] = i * 10; row[3] = Path.GetRandomFileName(); datatable.Rows.Add(row);
    }

    //Create a test file
    var fi = new FileInfo(@"c:\temp\Row_Grouping_Test.xlsx");
    if (fi.Exists)
        fi.Delete();

    using (var pck = new ExcelPackage(fi))
    {
        var worksheet = pck.Workbook.Worksheets.Add("Sheet1");
        worksheet.Cells.LoadFromDataTable(datatable, false);

        worksheet.Cells["A11"].Value = "TOTAL";
        worksheet.Cells["B11"].Formula = "SUM(B2:B10)";
        worksheet.Cells["C11"].Formula = "SUM(C2:C10)";
        worksheet.Row(11).Style.Font.Bold = true;

        //Row Group 1 (start with 1 since row index is 1-based)
        for (var i = 1; i <= datatable.Rows.Count; i++)
            worksheet.Row(i).OutlineLevel = 1;

        pck.Save();
    }
}

看起来像这样:

enter image description here

答案 1 :(得分:0)

我不熟悉EPPLUS图书馆。
但您可以使用Microsoft.Office.Interop.Excel命名空间轻松完成该任务 而且你可以像使用Vba一样(使用Merge()方法) 请注意,您需要将该命名空间的引用添加到项目中。
这是一个例子:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication32
{
    public partial class Form1 : Form
    {
        public Microsoft.Office.Interop.Excel.Application ExApp;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ExApp = new Microsoft.Office.Interop.Excel.Application();
            // CREATE A NEW WORKBOOK (you can also open existing workbook using the Open() method)
            Microsoft.Office.Interop.Excel.Workbook Wb = ExApp.Workbooks.Add();
            // SET WORKSHEET
            Microsoft.Office.Interop.Excel.Worksheet Ws = Wb.Worksheets.Add();
            // MERGE CELLS (the answer to your question)
            Ws.Range["A1:G5"].Merge();
            // GROUP ROWS (the final answer to your question)
            Ws.Rows["4:7"].Group();
            ExApp.Visible = true;
        }
    }
}