OpenXML使用SAX将AutoFilter添加到文档中

时间:2016-08-25 21:12:02

标签: c# excel openxml sax

出于某种原因,我的代码不会将自动过滤器添加到电子表格中。它会产生精细但是当打开时不存在自动过滤器。以下是我方法的相关摘录。我尝试将自动过滤器附加到工作表,然后使用xmlwriter写入文档。

//create worksheet part, and add it to the sheets collection in workbook
WorksheetPart wsp = wbp.AddNewPart<WorksheetPart>();
OpenXmlWriter writer = OpenXmlWriter.Create(wsp);
var worksheet = new Worksheet();
worksheet.AppendChild<AutoFilter>(new AutoFilter() { Reference = "A:BA" });
writer.WriteStartElement(worksheet);
writer.WriteStartElement(new SheetData());

1 个答案:

答案 0 :(得分:0)

自己找到答案。写完sheetdata结束后,必须写入自动过滤器。

  writer.WriteEndElement(); //end of SheetData
  writer.WriteElement(new AutoFilter() { Reference = "A:BA" });
  writer.WriteEndElement(); //end of worksheet
  writer.Close();