Esempio n. 1
0
 private void ExportToExcel_Click(object sender, RoutedEventArgs e)
 {
     try {
         DataTable      resultDt    = ((DataView)datGrid.ItemsSource).ToTable();
         SaveFileDialog saveFileDlg = new SaveFileDialog();
         saveFileDlg.Filter           = "xls files (*.xls)|*.xls";
         saveFileDlg.DefaultExt       = "xls";
         saveFileDlg.FilterIndex      = 2;
         saveFileDlg.RestoreDirectory = true;
         bool?result = saveFileDlg.ShowDialog();
         if (result == true)
         {
             var exporter = new ExcelHtmlDataExporter();
             using (var streamWriter = File.OpenWrite(saveFileDlg.FileName))
                 exporter.Export(resultDt.CreateDataReader(), streamWriter);
         }
     }
     catch (Exception error) {
         //if some error occurs just show the error message
         MessageBox.Show(error.Message);
     }
 }
Esempio n. 2
0
        private void btnExportXls_Click(object sender, EventArgs e)
        {
            try {
                //Save xls  file
                using (SaveFileDialog saveFileDialog = new SaveFileDialog()) {
                    saveFileDialog.Filter           = "xls files (*.xls)|*.xls";
                    saveFileDialog.FilterIndex      = 2;
                    saveFileDialog.RestoreDirectory = true;

                    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        var exporter = new ExcelHtmlDataExporter();
                        using (var streamWriter = File.OpenWrite(saveFileDialog.FileName))
                            exporter.Export(ResultDS.CreateDataReader(ResultDS.Tables.Cast <DataTable>().ToArray()), streamWriter);
                    }
                }
            }
            catch (Exception error) {
                //if some error occurs just show the error message
                MessageBox.Show(error.Message);
            }
        }