Exemplo n.º 1
0
        private static void OnExecuteExportToExcel(object sender, ExecutedRoutedEventArgs args)
        {
            var dataGrid = args.Source as SfDataGrid;
            EccelOptionsConverter ExcelOption = new EccelOptionsConverter();

            if (dataGrid == null)
            {
                return;
            }
            try
            {
                //Setting the Exporting Options by craeting a instance for ExcelExportingOptions.
                var exportingOptions = args.Parameter as ExcelExportingOptions;
                exportingOptions.ExportingEventHandler = ExportingHandler;
                if (!ExcelOption.isCustomized)
                {
                    exportingOptions.CellsExportingEventHandler = CellExportingHandler;
                }
                else
                {
                    exportingOptions.CellsExportingEventHandler = CustomizeCellExportingHandler;
                }
                exportingOptions.ExportStackedHeaders = true;
                //Below code exports datagrid to excel and returns Excel Engine.
                var excelEngine = dataGrid.ExportToExcel(dataGrid.View, exportingOptions);

                var workBook = excelEngine.Excel.Workbooks[0];

                //saving the workbook using savefiledialog.
                SaveFileDialog sfd = new SaveFileDialog
                {
                    FilterIndex = 2,
                    Filter      = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx",
                    FileName    = "Book1"
                };

                if (sfd.ShowDialog() == true)
                {
                    using (Stream stream = sfd.OpenFile())
                    {
                        if (sfd.FilterIndex == 1)
                        {
                            workBook.Version = ExcelVersion.Excel97to2003;
                        }
                        else
                        {
                            workBook.Version = ExcelVersion.Excel2010;
                        }
                        workBook.SaveAs(stream);
                    }

                    //Message box confirmation to view the created spreadsheet.
                    if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
                                        MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                    {
                        //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
                        Open(sfd.FileName);
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 2
0
        private static void OnExecuteExportToExcel(object sender, ExecutedRoutedEventArgs args)
        {
            var dataGrid = args.Source as SfDataGrid;
            EccelOptionsConverter ExcelOption = new EccelOptionsConverter();

            if (dataGrid == null)
            {
                return;
            }

            try
            {
                ExcelEngine excelEngine = new ExcelEngine();
                if (!ExcelOption.IsCustomizeRow)
                {
                    excelEngine = dataGrid.ExportToExcel(dataGrid.SelectedItems, new ExcelExportingOptions()
                    {
                        CellsExportingEventHandler = CellExportingHandler, ExportingEventHandler = ExportingHandler
                    });
                }
                else
                {
                    excelEngine = dataGrid.ExportToExcel(dataGrid.SelectedItems, new ExcelExportingOptions()
                    {
                        CellsExportingEventHandler = CellExportingHandler, ExportingEventHandler = CustomizeExportingHandler
                    });
                }
                var workBook = excelEngine.Excel.Workbooks[0];
                //saving the workbook using savefiledialog.
                SaveFileDialog sfd = new SaveFileDialog
                {
                    FilterIndex = 2,
                    Filter      = "Excel 97 to 2003 Files(*.xls)|*.xls|Excel 2007 to 2010 Files(*.xlsx)|*.xlsx",
                    FileName    = "Book1"
                };

                if (sfd.ShowDialog() == true)
                {
                    using (Stream stream = sfd.OpenFile())
                    {
                        if (sfd.FilterIndex == 1)
                        {
                            workBook.Version = ExcelVersion.Excel97to2003;
                        }
                        else
                        {
                            workBook.Version = ExcelVersion.Excel2010;
                        }
                        workBook.SaveAs(stream);
                    }

                    //Message box confirmation to view the created spreadsheet.
                    if (MessageBox.Show("Do you want to view the workbook?", "Workbook has been created",
                                        MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
                    {
                        //Launching the Excel file using the default Application.[MS Excel Or Free ExcelViewer]
                        System.Diagnostics.Process.Start(sfd.FileName);
                    }
                }
            }
            catch (Exception)
            {
            }
        }