【问题标题】:Excel file generated with EPPlus. Filters not working properly使用 EPPlus 生成的 Excel 文件。过滤器无法正常工作
【发布时间】:2018-05-08 05:50:12
【问题描述】:

首先,如果问这个问题的地方不对,请告诉我。

现在我会尽可能清楚地进入它。

我使用 EPPlus 从 C# 应用程序生成 Excel 文件。

文件生成正常,但在对其使用过滤器时(不是来自 EPPlus 的过滤器,而是来自 Excel 本身的过滤器),它不会过滤所有内容。它过滤了几行。

我可以提供代码sn-ps,或者excel生成的文件。来之前在网上搜索了很多,但是没有发现别人遇到这个问题。

提前感谢您的意见。

enter code here
        ExcelPackage ExcelPkg = new ExcelPackage();
        ExcelWorksheet wsSheet1 = ExcelPkg.Workbook.Worksheets.Add("Sheet1");

            #region Create table and headers
            using (ExcelRange Rng = wsSheet1.Cells[TableRange])
            {
                //Create Table
                ExcelTable table = wsSheet1.Tables.Add(Rng, "OSTable");
                for (int i = 0; i < headers.Count; i++)
                {
                    table.Columns[i].Name = headers[i];
                }

                table.ShowHeader = true;
                table.ShowFilter = false; 
            }
            #endregion

#region 向 Excel 表格单元格中插入数据

            foreach (ArticleRow ar in mainArticleList)
            {

                for (int i = 0; i < ar.Qty.Count; i++)
                {
                    currentRow++;
                    string thisRowRange = "A" + currentRow.ToString() + ":" + "Q" + currentRow.ToString();


                    #region BOM_Position Col: A
                    using (ExcelRange Rng = wsSheet1.Cells["A" + currentRow.ToString()])
                    {
                        Rng.Value = Convert.ToInt32(ar.BOM_Position);
                        Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        Rng.Style.Font.Size = FontSize;
                    }
                    #endregion
                    #region Input Col: B
                    using (ExcelRange Rng = wsSheet1.Cells["B" + currentRow.ToString()])
                    {
                        Rng.Value = ar.Input;
                        Rng.Style.Font.Size = FontSize;
                    }
                    #endregion

                    #region QTY Col: J
                    using (ExcelRange Rng = wsSheet1.Cells["J" + currentRow.ToString()])
                    {
                        Rng.Value = ar.Qty[i];
                        Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        Rng.Style.Font.Size = FontSize;

                    }
                    #endregion
                    #endregion
                    #region Price Col: L
                    using (ExcelRange Rng = wsSheet1.Cells["L" + currentRow.ToString()])
                    {
                        Rng.Value = ar.Price[i];
                        Rng.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
                        Rng.Style.Font.Size = FontSize;
                    }
                    #endregion

                    #endregion
                    #region ArticleUrl Col: Q
                    using (ExcelRange Rng = wsSheet1.Cells["Q" + currentRow.ToString()])
                    {
                        Rng.Formula = "=HYPERLINK(\"" + ar.Link + "\", \"" + ar.Store + "\")";
                        Rng.StyleName = StyleName;
                        Rng.Style.Font.Size = FontSize;
                    }
                    #endregion


                    #region  Formatting Row Color by Store 
                    using (ExcelRange Rng = wsSheet1.Cells[thisRowRange])
                    {


                        Rng.Style.Border.Top.Style = ExcelBorderStyle.None;
                        Rng.Style.Border.Bottom.Style = ExcelBorderStyle.None;
                        Rng.Style.Border.Left.Style = ExcelBorderStyle.Thin;
                        Rng.Style.Border.Left.Color.SetColor(Color.LightCyan);
                        Rng.Style.Border.Right.Style = ExcelBorderStyle.Thin;
                        Rng.Style.Border.Right.Color.SetColor(Color.LightCyan);

                        Rng.Style.Fill.PatternType = ExcelFillStyle.Solid;

                        if (ar.Store == "Farnell" | ar.Store == "FarnellBETA")
                        { Rng.Style.Fill.BackgroundColor.SetColor(ColorFarnellRow); }
                        else if (ar.Store == "Mouser")
                        { Rng.Style.Fill.BackgroundColor.SetColor(ColorMouserRow); }

                    }
                    #endregion


                    #region Formatting Stock Cell Color by Content
                    using (ExcelRange Rng = wsSheet1.Cells["I" + currentRow.ToString()])
                    {
                        if (ar.Stock == "0" | ar.Stock == "-" | ar.Stock == "n/a" | ar.Stock == "--")
                        {
                            Rng.Style.Fill.PatternType = ExcelFillStyle.Solid;
                            Rng.Style.Fill.BackgroundColor.SetColor(Color.Red);
                        }
                    }
                    #endregion

                }
            }
            #endregion

我已经删除了一些重复的代码部分,以免给你造成混乱。

【问题讨论】:

    标签: c# excel epplus


    【解决方案1】:

    为什么不试试 TableStyle: OfficeOpenXml.Table.TableStyles.Medium6 ,这是 EpPlus 的一部分。您可以将其与标题一起应用。标签样式提供了良好的 UI 外观(背景颜色)以及自动过滤器。

    不管怎样,你为 Style 编写了这么多额外的代码,那么你也在过滤(这对你不起作用)。

    【讨论】:

    • 感谢您的回答。我会调查你所说的。这是我第一次使用 EPPlus,我并不完全知道正确的方法。我是从 youtube 教程中了解到的,我认为这就是它的预期方式。
    【解决方案2】:

    我已经找到了我的问题的根源,我决定发布一个答案,也许它会在未来对某人有所帮助。

    我给了一个错误的表格范围,使我的表格比我正在写入的所有数据都小,而其余的数据都被写入了表格之外。这就是我无法过滤的原因。过滤器正在工作,但只在表格的一部分上工作。

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 2015-02-14
      • 2013-08-16
      • 2012-11-16
      • 2011-04-06
      • 1970-01-01
      相关资源
      最近更新 更多