使用EPPlus生成的Excel文件。过滤器无法正常工作

时间:2018-05-08 05:50:13

标签: c# excel epplus

首先,如果这是一个提出这个问题的错误地方,请告诉我。

现在我会尽可能清楚地了解它。

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

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

我可以提供代码段或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

我删除了代码的一些重复部分,以免弄乱你。

2 个答案:

答案 0 :(得分:1)

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

无论如何,你正在为Style编写这么多额外的代码,那么你也在过滤(这对你不起作用)。

答案 1 :(得分:0)

我找到了问题的根源,并且我已经决定发布一个答案,也许它将来会帮助某人。

我给出了一个错误的表范围,使得我的表比我写的所有数据都小,其余的数据写在表外。这就是我无法过滤的原因。过滤器正在运行,但仅限于表的一部分。

相关问题