A
Andrew Walaszek
I am having problems sorting Tables in Excel 2007 using C#. The below method
changes the cursor to ascending in the Table's header sort fields but fails
to sort the cells. Under debugger the SortField and Table ranges passed to
Excel look OK and Apply() executes without errors. Any help will be
appreciated.
Thanks,
Andrew
/// <summary>
/// Sort Table in ascending order by the specified keys
/// </summary>
/// <param name="inTable">input Table to sort</param>
/// <param name="inColumns">input key column names to use</param>
public static void SortTableByKeys(Excel.ListObject inTable, string[]
inColumns)
{
inTable.Sort.SortFields.Clear();
foreach (string column in inColumns)
{
inTable.Sort.SortFields.Add(inTable.ListColumns.get_Item(column).Range,
Excel.XlSortType.xlSortValues, Excel.XlSortOrder.xlAscending, Type.Missing,
Excel.XlSortDataOption.xlSortNormal);
}
inTable.Sort.SetRange(inTable.Range);
inTable.Sort.Orientation = Excel.XlSortOrientation.xlSortColumns;
inTable.Sort.MatchCase = false;
inTable.Sort.Header = Excel.XlYesNoGuess.xlYes;
inTable.Sort.SortMethod = Excel.XlSortMethod.xlPinYin;
inTable.Sort.Apply();
}
changes the cursor to ascending in the Table's header sort fields but fails
to sort the cells. Under debugger the SortField and Table ranges passed to
Excel look OK and Apply() executes without errors. Any help will be
appreciated.
Thanks,
Andrew
/// <summary>
/// Sort Table in ascending order by the specified keys
/// </summary>
/// <param name="inTable">input Table to sort</param>
/// <param name="inColumns">input key column names to use</param>
public static void SortTableByKeys(Excel.ListObject inTable, string[]
inColumns)
{
inTable.Sort.SortFields.Clear();
foreach (string column in inColumns)
{
inTable.Sort.SortFields.Add(inTable.ListColumns.get_Item(column).Range,
Excel.XlSortType.xlSortValues, Excel.XlSortOrder.xlAscending, Type.Missing,
Excel.XlSortDataOption.xlSortNormal);
}
inTable.Sort.SetRange(inTable.Range);
inTable.Sort.Orientation = Excel.XlSortOrientation.xlSortColumns;
inTable.Sort.MatchCase = false;
inTable.Sort.Header = Excel.XlYesNoGuess.xlYes;
inTable.Sort.SortMethod = Excel.XlSortMethod.xlPinYin;
inTable.Sort.Apply();
}