使用Excel Interop C#sortingExcel行

我有一个C#程序,将结果放在Excel电子表格中。 每行包含竞争对手的信息,包括姓名,身份证号码,地址,分数等几个字段,每个字段在不同的列中。 我想根据分数sorting所有这些竞争对手(所以我想对这些行进行sorting),logging从最高到最低sorting。 什么是最好的方式去做这件事? 这是我正在尝试哪些不工作的代码。

Excel.Range sortRange; sortRange = worksheet.get_Range("A14", "K32"); Excel.Range scoreColumn; scoreColumn = worksheet.get_Range("C14", "C32"); sortRange.Sort(scoreColumn, Excel.XlSortOrder.xlDescending, 

我会放弃使用Excel Interop类。
已经有多个API可以完成很好的工作。

无需Interop创buildExcel

另一个伟大的SO职位是阅读文件。 这可能会指向使用非interop C#类编写文件的正确方向。

.net – 读取Excel文件

啊哈,我刚刚做了这个,不幸的是代码是在VB.NET中,让我知道如果你有任何困难转化为C#

  Dim lastrow As Integer = 0 lastrow = xlsSheet.UsedRange.Row + xlsSheet.UsedRange.Rows.Count - 1 Dim myRange = xlsSheet.range("A2", "Q" & lastrow) myRange.Select() xlsApp.ActiveWorkbook.Worksheets("your_work_sheet_name").Sort.SortFields.Add(Key:= _ xlsSheet.Range("A2:A" & lastrow), Order:=Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending) xlsApp.ActiveWorkbook.Worksheets("your_work_sheet_name").Sort.SortFields.Add(Key:= _ xlsSheet.Range("B2:B" & lastrow), Order:=Microsoft.Office.Interop.Excel.XlSortOrder.xlAscending) With xlsApp.ActiveWorkbook.Worksheets("your_work_sheet_name").Sort .SetRange(myRange) .Header = Microsoft.Office.Interop.Excel.XlYesNoGuess.xlYes .MatchCase = False .Orientation = Microsoft.Office.Interop.Excel.XlSortOrientation.xlSortColumns .SortMethod = Microsoft.Office.Interop.Excel.XlSortMethod.xlPinYin .Apply() End With 

这工作对我来说,但我不得不添加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; 

我不再添加一个表,只是设置autofilter与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答案后更新

 using System.Runtime.InteropServices; ... /// <summary> /// <para>Customized function for Range property of Microsoft.Office.Interop.Excel.Worksheet</para> /// </summary> /// <param name="WS"></param> /// <param name="Cell1"></param> /// <param name="Cell2"></param> /// <returns>null if Range property of <paramref name="WS"/> throws exception, otherwise corresponding range</returns> public static Range GetRange(this Worksheet WS, object Cell1, [Optional] object Cell2) { try { return WS.Range[Cell1: Cell1, Cell2: Cell2]; } catch (Exception ex) { return null; } } ... Excel.Range sortRange = worksheet.GetRange("A14", "K32"); Excel.Range scoreColumn = worksheet.GetRange("C14", "C32"); // for this particular case, this check is unuseful // but if you set you scoreColumn as: // Excel.Range scoreColumn = worksheet.GetRange("COMPETITORS[[#Data], [SCORE]]"); // you will have scoreColumn as null if COMPETITORS table has no column named "SCORE" if (sortRange != null && scoreColumn != null) { sortRange.Sort.SortFields.Clear(); sortRange.Sort.SortFields.Add(scoreColumn, Excel.XlSortOn.xlSortOnValues, Excel.XlSortOrder.xlDescending, Type.Missing, Excel.XlSortDataOption.xlSortNormal); sortRange.Sort.Header = XlYesNoGuess.xlYes; sortRange.Sort.MatchCase = false; // avoid setting this when you're dealing with a table // the only available option is xlSortColumns // if you apply a sort to a table and you set XlSortOrientation.xlRows, // you will get an exception // see https://stackoverflow.com/questions/13057631/f-excel-range-sort-fails-or-rearranges-columns //sortRange.Sort.Orientation = XlSortOrientation.xlSortColumns; sortRange.Sort.SortMethod = XlSortMethod.xlPinYin; sortRange.Sort.Apply(); } 

进一步阅读:
1) F#Excel Range.Sort失败或重新排列列