// Export data for this image set as a .csv file // Export data for this image set as a .csv file and preview in Excel private void MenuItemExportCsv_Click(object sender, RoutedEventArgs e) { if (this.State.SuppressSelectedCsvExportPrompt == false && this.DataHandler.FileDatabase.ImageSet.FileSelection != FileSelectionEnum.All) { // Export data for this image set as a.csv file, but confirm, as only a subset will be exported since a selection is active if (Dialogs.MenuFileExportCSVOnSelectionDialog(this) == false) { return; } } // Generate the file names/path string csvFileName = Path.GetFileNameWithoutExtension(this.DataHandler.FileDatabase.FileName) + ".csv"; string csvFilePath = Path.Combine(this.FolderPath, csvFileName); // Backup the csv file if it exists, as the export will overwrite it. if (FileBackup.TryCreateBackup(this.FolderPath, csvFileName)) { this.StatusBar.SetMessage("Backup of csv file made."); } else { this.StatusBar.SetMessage("No csv file backup was made."); } try { CsvReaderWriter.ExportToCsv(this.DataHandler.FileDatabase, csvFilePath, this.excludeDateTimeAndUTCOffsetWhenExporting); } catch (IOException exception) { // Can't write the spreadsheet file Dialogs.MenuFileCantWriteSpreadsheetFileDialog(this, csvFilePath, exception.GetType().FullName, exception.Message); return; } MenuItem mi = (MenuItem)sender; if (mi == this.MenuItemExportAsCsvAndPreview) { // Show the file in excel // Create a process that will try to show the file ProcessStartInfo processStartInfo = new ProcessStartInfo { UseShellExecute = true, RedirectStandardOutput = false, FileName = csvFilePath }; if (ProcessExecution.TryProcessStart(processStartInfo) == false) { // Can't open excel Dialogs.MenuFileCantOpenExcelDialog(this, csvFilePath); return; } } else if (this.State.SuppressCsvExportDialog == false) { Dialogs.MenuFileCSVDataExportedDialog(this, csvFileName); } this.StatusBar.SetMessage("Data exported to " + csvFileName); }
// Export data for this image set as a .csv file // Export data for this image set as a .csv file and preview in Excel private async void MenuItemExportCsv_Click(object sender, RoutedEventArgs e) { if (this.State.SuppressSelectedCsvExportPrompt == false && this.DataHandler.FileDatabase.ImageSet.FileSelection != FileSelectionEnum.All) { // Export data for this image set as a.csv file, but confirm, as only a subset will be exported since a selection is active if (Dialogs.MenuFileExportCSVOnSelectionDialog(this) == false) { return; } } // Generate the candidate file name/path string csvFileName = Path.GetFileNameWithoutExtension(this.DataHandler.FileDatabase.FileName) + ".csv"; // Get the selected filepath from the user if (false == Dialogs.TryGetFileFromUserUsingSaveFileDialog( "Export and save your data as a CSV file", csvFileName, String.Format("CSV files (*{0})|*{0}", Constant.File.CsvFileExtension), Constant.File.CsvFileExtension, out string selectedCSVFilePath)) { // Abort, as file selection is cancelled this.StatusBar.SetMessage("Csv file export cancelled."); return; } if (File.Exists(selectedCSVFilePath) && new System.IO.FileInfo(selectedCSVFilePath).Attributes.HasFlag(System.IO.FileAttributes.ReadOnly)) { // The file exists but its read only... Dialogs.FileCantOpen(GlobalReferences.MainWindow, selectedCSVFilePath, true); this.StatusBar.SetMessage("Csv file export cancelled."); return; } // Backup the csv file if it exists, as the export will overwrite it. if (FileBackup.TryCreateBackup(this.FolderPath, selectedCSVFilePath)) { this.StatusBar.SetMessage("Backup of csv file made."); } else { this.StatusBar.SetMessage("No csv file backup was made."); } try { // Show the Busy indicator this.BusyCancelIndicator.IsBusy = true; if (false == await CsvReaderWriter.ExportToCsv(this.DataHandler.FileDatabase, selectedCSVFilePath, this.State.CSVDateTimeOptions, this.State.CSVInsertSpaceBeforeDates)) { Dialogs.FileCantOpen(GlobalReferences.MainWindow, selectedCSVFilePath, true); return; } // Hide the Busy indicator this.BusyCancelIndicator.IsBusy = false; } catch (Exception exception) { // Can't write the spreadsheet file Dialogs.MenuFileCantWriteSpreadsheetFileDialog(this, selectedCSVFilePath, exception.GetType().FullName, exception.Message); return; } MenuItem mi = (MenuItem)sender; if (mi == this.MenuItemExportAsCsvAndPreview) { // Show the file in excel // Create a process that will try to show the file ProcessStartInfo processStartInfo = new ProcessStartInfo { UseShellExecute = true, RedirectStandardOutput = false, FileName = selectedCSVFilePath }; if (ProcessExecution.TryProcessStart(processStartInfo) == false) { // Can't open excel Dialogs.MenuFileCantOpenExcelDialog(this, selectedCSVFilePath); return; } } else if (this.State.SuppressCsvExportDialog == false) { Dialogs.MenuFileCSVDataExportedDialog(this, selectedCSVFilePath); } this.StatusBar.SetMessage("Data exported to " + selectedCSVFilePath); }