Esempio n. 1
0
        public void SaveDataGridToEMethod(System.Windows.Controls.DataGrid _dataGrid)
        {
            SaveFileDialog _saveFileDialog = new SaveFileDialog();

            _saveFileDialog.Filter = "Comma separated values files (*.csv) | *.csv";
            if (_saveFileDialog.ShowDialog() == true)
            {
                _dataGrid.SelectionMode = System.Windows.Controls.DataGridSelectionMode.Extended;
                _dataGrid.SelectAllCells();
                _dataGrid.ClipboardCopyMode = System.Windows.Controls.DataGridClipboardCopyMode.IncludeHeader;
                System.Windows.Input.ApplicationCommands.Copy.Execute(null, _dataGrid);
                String result = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
                _dataGrid.UnselectAllCells();
                System.IO.StreamWriter file1 = new System.IO.StreamWriter(_saveFileDialog.FileName, false, Encoding.UTF8);
                file1.WriteLine(result);
                file1.Close();
                _dataGrid.SelectionMode = System.Windows.Controls.DataGridSelectionMode.Single;
                MessageBox.Show("Файл сохранен!");
            }
        }
Esempio n. 2
0
        public static void Export(System.Windows.Controls.DataGrid dataGrid)
        {
            SaveFileDialog dlg = new SaveFileDialog();

            dlg.DefaultExt = ".xls";
            dlg.Filter     = "Text documents (.xls)|*.xls";

            Nullable <bool> r = dlg.ShowDialog();

            if (r == true)
            {
                dataGrid.SelectAllCells();
                dataGrid.ClipboardCopyMode = DataGridClipboardCopyMode.IncludeHeader;
                ApplicationCommands.Copy.Execute(null, dataGrid);
                String resultat = (string)Clipboard.GetData(DataFormats.CommaSeparatedValue);
                String result   = (string)Clipboard.GetData(DataFormats.Text);
                dataGrid.UnselectAllCells();
                System.IO.StreamWriter file = new System.IO.StreamWriter(dlg.FileName);
                file.WriteLine(result.Replace(',', ' '));
                file.Close();
            }
        }