private void btnExportToExcelMain_Click(object sender, RoutedEventArgs e)
        {
            if (dg.Items.Count == 0)
            {
                return;
            }
            if (!BaseDataBase.CurrentUser.CanExport)
            {
                MyMessageBox.Show("لا يوجد لديك صلاحيات للتصدير");
                return;
            }
            var dt = (dg.ItemsSource as DataView).Table.Copy();

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                for (int j = 0; j < dg.Columns.Count; j++)
                {
                    if (dg.Columns[j].SortMemberPath == dt.Columns[i].ColumnName)
                    {
                        dt.Columns[i].ColumnName = dg.Columns[j].Header.ToString();
                        break;
                    }
                }
            }

            ExportToExcel.ExportDataTableToExcel x = new MainWPF.ExportToExcel.ExportDataTableToExcel(dt);
            x.GenerateReport();
        }
Esempio n. 2
0
        private void btnExportToExcel_Click(object sender, RoutedEventArgs e)
        {
            if (!BaseDataBase.CurrentUser.CanExport)
            {
                MyMessageBox.Show("لا يوجد لديك صلاحيات للتصدير");
                return;
            }
            var dg = ((sender as Button).Parent as Grid).FindName("dg2") as DataGrid;

            if (dg.Items.Count > 0)
            {
                var        dt = (dg.ItemsSource as DataView).Table.Copy();
                List <int> indexesToRemove = new List <int>();
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    int j = 0;
                    for (; j < dg.Columns.Count; j++)
                    {
                        if (dg.Columns[j].SortMemberPath == dt.Columns[i].ColumnName)
                        {
                            dt.Columns[i].ColumnName = dg.Columns[j].Header.ToString();
                            break;
                        }
                    }
                    if (j == dg.Columns.Count)
                    {
                        indexesToRemove.Add(i);
                    }
                }
                for (int i = indexesToRemove.Count - 1; i >= 0; i--)
                {
                    dt.Columns.RemoveAt(i);
                }

                ExportToExcel.ExportDataTableToExcel x = new MainWPF.ExportToExcel.ExportDataTableToExcel(dt);
                x.GenerateReport();
            }
        }