PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/ClosedXML/ClosedXML/ClosedXML_Examples/AutoFilters/CustomAutoFilter.cs

#
C# | 139 lines | 102 code | 26 blank | 11 comment | 0 complexity | 0b688a4474037a46302e7bc55166a9bb MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using ClosedXML.Excel;
  4. namespace ClosedXML_Examples
  5. {
  6. public class CustomAutoFilter : IXLExample
  7. {
  8. public void Create(string filePath)
  9. {
  10. var wb = new XLWorkbook();
  11. IXLWorksheet ws;
  12. #region Single Column Numbers
  13. String singleColumnNumbers = "Single Column Numbers";
  14. ws = wb.Worksheets.Add(singleColumnNumbers);
  15. // Add a bunch of numbers to filter
  16. ws.Cell("A1").SetValue("Numbers")
  17. .CellBelow().SetValue(2)
  18. .CellBelow().SetValue(3)
  19. .CellBelow().SetValue(3)
  20. .CellBelow().SetValue(5)
  21. .CellBelow().SetValue(1)
  22. .CellBelow().SetValue(4);
  23. // Add filters
  24. ws.RangeUsed().SetAutoFilter().Column(1).EqualTo(3).Or.GreaterThan(4);
  25. // Sort the filtered list
  26. ws.AutoFilter.Sort(1);
  27. #endregion
  28. #region Single Column Strings
  29. String singleColumnStrings = "Single Column Strings";
  30. ws = wb.Worksheets.Add(singleColumnStrings);
  31. // Add a bunch of strings to filter
  32. ws.Cell("A1").SetValue("Strings")
  33. .CellBelow().SetValue("B")
  34. .CellBelow().SetValue("C")
  35. .CellBelow().SetValue("C")
  36. .CellBelow().SetValue("E")
  37. .CellBelow().SetValue("A")
  38. .CellBelow().SetValue("D");
  39. // Add filters
  40. ws.RangeUsed().SetAutoFilter().Column(1).Between("B", "D");
  41. // Sort the filtered list
  42. ws.AutoFilter.Sort(1);
  43. #endregion
  44. #region Single Column Mixed
  45. String singleColumnMixed = "Single Column Mixed";
  46. ws = wb.Worksheets.Add(singleColumnMixed);
  47. // Add a bunch of items to filter
  48. ws.Cell("A1").SetValue("Mixed")
  49. .CellBelow().SetValue("B")
  50. .CellBelow().SetValue(3)
  51. .CellBelow().SetValue("C")
  52. .CellBelow().SetValue("E")
  53. .CellBelow().SetValue(1)
  54. .CellBelow().SetValue(4);
  55. // Add filters
  56. ws.RangeUsed().SetAutoFilter().Column(1).EqualTo(3).Or.EqualTo("C");
  57. // Sort the filtered list
  58. ws.AutoFilter.Sort(1);
  59. #endregion
  60. #region Multi Column
  61. String multiColumn = "Multi Column";
  62. ws = wb.Worksheets.Add(multiColumn);
  63. ws.Cell("A1").SetValue("First")
  64. .CellBelow().SetValue("B")
  65. .CellBelow().SetValue("C")
  66. .CellBelow().SetValue("C")
  67. .CellBelow().SetValue("E")
  68. .CellBelow().SetValue("A")
  69. .CellBelow().SetValue("D");
  70. ws.Cell("B1").SetValue("Numbers")
  71. .CellBelow().SetValue(2)
  72. .CellBelow().SetValue(3)
  73. .CellBelow().SetValue(3)
  74. .CellBelow().SetValue(5)
  75. .CellBelow().SetValue(1)
  76. .CellBelow().SetValue(4);
  77. ws.Cell("C1").SetValue("Strings")
  78. .CellBelow().SetValue("B")
  79. .CellBelow().SetValue("C")
  80. .CellBelow().SetValue("C")
  81. .CellBelow().SetValue("E")
  82. .CellBelow().SetValue("A")
  83. .CellBelow().SetValue("D");
  84. // Add filters
  85. ws.RangeUsed().SetAutoFilter().Column(2).EqualTo(3).Or.GreaterThan(4);
  86. ws.RangeUsed().SetAutoFilter().Column(3).Between("B", "D");
  87. // Sort the filtered list
  88. ws.AutoFilter.Sort(3);
  89. #endregion
  90. using (var ms = new MemoryStream())
  91. {
  92. wb.SaveAs(ms);
  93. var workbook = new XLWorkbook(ms);
  94. #region Single Column Numbers
  95. workbook.Worksheet(singleColumnNumbers).AutoFilter.Sort(1, XLSortOrder.Descending);
  96. #endregion
  97. #region Single Column Strings
  98. workbook.Worksheet(singleColumnStrings).AutoFilter.Sort(1, XLSortOrder.Descending);
  99. #endregion
  100. #region Single Column Mixed
  101. workbook.Worksheet(singleColumnMixed).AutoFilter.Column(1).EqualOrGreaterThan("D");
  102. workbook.Worksheet(singleColumnMixed).AutoFilter.Sort(1, XLSortOrder.Descending);
  103. #endregion
  104. #region Multi Column
  105. workbook.Worksheet(multiColumn).AutoFilter.Column(3).EqualTo("E");
  106. workbook.Worksheet(multiColumn).AutoFilter.Sort(3, XLSortOrder.Descending);
  107. #endregion
  108. workbook.SaveAs(filePath);
  109. ms.Close();
  110. }
  111. }
  112. }
  113. }