相关疑难解决方法(0)

为什么在此示例中附加AutoFilter会破坏我的excel文件?

您好我使用以下方法来应用自动筛选:

public static void ApplyAutofilter(string fileName, string sheetName, string reference)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Open(fileName, true))
            {
                IEnumerable<Sheet> sheets = document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
                var arrSheets = sheets as Sheet[] ?? sheets.ToArray();

                string relationshipId = arrSheets.First().Id.Value;
                var worksheetPart = (WorksheetPart)document.WorkbookPart.GetPartById(relationshipId);

                var autoFilter = new AutoFilter() { Reference = reference };
                worksheetPart.Worksheet.Append(autoFilter);

                worksheetPart.Worksheet.Save();
            }
        }
Run Code Online (Sandbox Code Playgroud)

我有这个简单的Excel表格:

在此输入图像描述

问题在于我将此方法称为:

ApplyAutofilter(@".\Test.xlsx", "Foo", "A0:A200");
Run Code Online (Sandbox Code Playgroud)

要么

ApplyAutofilter(@".\Test.xlsx", "Foo", "A1:A200");
Run Code Online (Sandbox Code Playgroud)

在我尝试打开之后,未应用过滤器并且文件已损坏:

在此输入图像描述

问题:有没有想过如何将这个自动加载器应用到excel表而不会破坏它?

.net c# excel openxml openxml-sdk

5
推荐指数
1
解决办法
2961
查看次数

标签 统计

.net ×1

c# ×1

excel ×1

openxml ×1

openxml-sdk ×1