这对我有用,但我必须添加一个带有 EPPLUS 的表才能使这条线工作:
sheet.AutoFilter.Sort.SortFields.Add(oRange, XlSortOrder.xlAscending);</s>
<s> // add the excel table entity with EPPLUS (available on Nuget)
var table = ws.Tables.Add(range, "table1");
table.TableStyle = OfficeOpenXml.Table.TableStyles.Light2;
我不再添加表格,只是将带有 epplus 的自动过滤器设置为 sheet.AutoFilter 在较大的 Excel 文件中不为 NULL
var colindexM = ws.Cells["M5"].Start.Column;
var endcell = ws.Cells[ws.Dimension.End.Row, ws.Dimension.End.Column];
var range = ws.Cells[4, 2, endcell.Start.Row, endcell.Start.Column];
range.AutoFilter = true;
然后:
//open workbook
workBook = oXL.Workbooks.Open(outputfilepath, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
//oXL.Visible = false;
Worksheet sheet = workBook.Sheets["Rapport1"];
// set Date Format to the sort column
Range rg = sheet.Cells[5, colindexM];
rg.EntireColumn.NumberFormat = "DD/MM/YYYY";
sheet.EnableAutoFilter = true;
sheet.AutoFilter.Sort.SortFields.Add(rg, XlSortOn.xlSortOnValues, XlSortOrder.xlAscending,"", XlSortDataOption.xlSortTextAsNumbers );
sheet.AutoFilter.ApplyFilter();
workBook.Save();
oXL.Quit();
在 Jan 'splite' K 回答之后更新