public static void DgridToExcelContext(object sender, RoutedEventArgs e) { try { using (Excelcontrol xl = new Excelcontrol()) { DataGrid dg = ((((sender as MenuItem).Parent) as ContextMenu).PlacementTarget as DataGrid); bool[] visOrder = new bool[dg.Columns.Count]; for (int i = 0; i <= dg.Columns.Count - 1; i++) { if (dg.Columns[i].Visibility.Equals(Visibility.Visible)) { visOrder[i] = true; } else { visOrder[i] = false; } } System.Data.DataView dv = (System.Data.DataView)(((((sender as MenuItem).Parent) as ContextMenu).PlacementTarget as DataGrid).ItemsSource); xl.ExportToExcel(dv.ToTable(), visOrder); } } catch { } }
public static void DGridToExcel(object sender, RoutedEventArgs e) { try { using (Excelcontrol xl = new Excelcontrol()) { DataGrid dg = ((((sender as MenuItem).Parent) as ContextMenu).PlacementTarget as DataGrid); xl.ExportToExcel(((System.Data.DataView)(dg.ItemsSource)).ToTable(), xl.GetVisOrder(dg)); } } catch { } }