Ep Plus:在表格范围内应用单个过滤器

时间:2019-03-28 09:24:59

标签: c# epplus epplus-4

我已经使用EP plus在c#中创建了对Excel的导出。我已经在excel中成功生成了多个表。 我当前面临的问题是过滤器。每个表都具有列标题,这是我期望的格式,但是我也期望过滤器仅应用于第一个表标题,并且用户应该能够使用相同的过滤器跨工作表中的表进行过滤。目前,每个表都有自己的过滤器,该过滤器仅限于过滤属于其范围的行。

我尝试将过滤器手动应用于导出工作表上的第一列标题,并且它可以工作。如何使用EP plus以编程方式做到这一点。

enter image description here

代码

public static void Print(ExcelPackage package, ILookup<string, FIRMWIDE_MANAGER_ALLOCATION> allocation)
        {

            ExcelWorksheet wsSheet1 = package.Workbook.Worksheets.Add("Sheet1");
            wsSheet1.Protection.IsProtected = false;

            IEnumerable<FIRMWIDE_MANAGER_ALLOCATION> allocationGroup = null;

            var rowNumber = 1;
            int tableIndex = 0;

           // var showFilter = true;
            var showHeader = true;
            var showTotals = true;
            var rowAdderForHeader = Convert.ToInt32(showHeader);
            var rowAdderForFooter = Convert.ToInt32(showTotals);

            //var range = wsSheet1.Cells["A1:G100"];
            //range.AutoFilter = true;

            foreach (var ag in allocation)
            {
                tableIndex += 1;
                Console.WriteLine(tableIndex);

                allocationGroup = ag.Select(a => a);
                var allocationList = allocationGroup.ToList();
                var count = allocationList.Count();
                wsSheet1.Cells["E" + rowNumber + ":E" + (count + rowNumber)].Style.Numberformat.Format = "#,##0";


                using (ExcelRange Rng = wsSheet1.Cells["A" + rowNumber + ":G" + (count + rowNumber)])
                {
                    ExcelTableCollection tblcollection = wsSheet1.Tables;
                    ExcelTable table = tblcollection.Add(Rng, "tblAllocations" + tableIndex);



                    //Set Columns position & name  
                    table.Columns[0].Name = "Manager Strategy";
                    table.Columns[1].Name = "Fund";
                    table.Columns[2].Name = "Product Name";
                    table.Columns[3].Name = "As Of";
                    table.Columns[4].Name = "EMV (USD)";
                    table.Columns[5].Name = "% of Fund Strategy";
                    table.Columns[6].Name = "% of Product";


                    wsSheet1.Column(1).Width = 45;
                    wsSheet1.Column(2).Width = 45;
                    wsSheet1.Column(3).Width = 55;
                    wsSheet1.Column(4).Width = 15;
                    wsSheet1.Column(5).Width = 25;
                    wsSheet1.Column(6).Width = 20;
                    wsSheet1.Column(7).Width = 20;

                    table.ShowHeader = showHeader;
                    //table.ShowFilter = showFilter;
                    table.ShowTotal = showTotals;
                    //Add TotalsRowFormula into Excel table Columns  
                    table.Columns[0].TotalsRowLabel = "Total Rows";
                    table.Columns[4].TotalsRowFormula = "SUBTOTAL(109,[EMV (USD)])";
                    table.Columns[5].TotalsRowFormula = "SUBTOTAL(109,[% of Fund Strategy])";
                    //table.Columns[6].TotalsRowFormula = "SUBTOTAL(109, [% of Product])";

                    table.TableStyle = TableStyles.Dark10;
                }

                //Account for the table header
                rowNumber += rowAdderForHeader;

                foreach (var ac in allocationGroup)
                {
                    wsSheet1.Cells["A" + rowNumber].Value = ac.MANAGER_STRATEGY_NAME;
                    wsSheet1.Cells["B" + rowNumber].Value = ac.MANAGER_FUND_NAME;
                    wsSheet1.Cells["C" + rowNumber].Value = ac.PRODUCT_NAME;
                    wsSheet1.Cells["D" + rowNumber].Value = ac.EVAL_DATE.HasValue ? ac.EVAL_DATE.Value.ToString("MMM dd, yyyy") : string.Empty;
                    wsSheet1.Cells["E" + rowNumber].Value = Math.Round((double)ac.UsdEmv,2);
                    wsSheet1.Cells["F" + rowNumber].Value = Math.Round((double)ac.GroupPercent,2);
                    wsSheet1.Cells["G" + rowNumber].Value = Math.Round((double)ac.WEIGHT_WITH_EQ, 2); 
                    rowNumber++;
                }
                //Account for the table footer
                rowNumber += rowAdderForFooter;
            }
        }
    }

0 个答案:

没有答案