我需要的是当行的值与上一行相同时对行进行分组,“ B3”的值与“ B2”相同,如下所示:
我在epplus中使用c#,我看到如何使用outline选项来执行与此类似的操作,并且与我想要的功能相似,但是此选项有一些缺点,例如不能自动基于分组的缺点价值观,不能做各种团体。
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文件。
编辑
现在有了摘要,我尝试使用group函数进行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;
Ernie S.. 7
我看到已经回答了,但是想出了我会提供一种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 = "SUBTOTAL(9,B2:B10)"; worksheet.Cells["C11"].Formula = "SUBTOTAL(9,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(); } }
看起来像这样:
我看到已经回答了,但是想出了我会提供一种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 = "SUBTOTAL(9,B2:B10)"; worksheet.Cells["C11"].Formula = "SUBTOTAL(9,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(); } }
看起来像这样: