private void OnExportEXCEL(object _type) { if (_type == null) { return; } NodesToExcelExportType type = NodeAssembler.StringToType(_type.ToString()); try { // Configure save file dialog box var dlg = new Microsoft.Win32.SaveFileDialog() { FileName = "Export", // Default file name DefaultExt = ".xlsx", // Default file extension Filter = "excel files|*.xls;*.xlsx" // Filter files by extension }; // Show save file dialog box Nullable <bool> result = dlg.ShowDialog(); // Process save file dialog box results if (result.HasValue && result == true) { // transfer the nodes into strings List <string> data_header; List <List <string> > data_as_strings; switch (type) { case NodesToExcelExportType.COMPLETE: data_header = NodeAssembler.GetNodeListHeaderComplete(); data_as_strings = NodeAssembler.NodeListToStringsComplete(this.NManager.Nodes, false); break; case NodesToExcelExportType.EXPLICIT: data_header = NodeAssembler.GetNodeListHeaderComplete(); data_as_strings = NodeAssembler.NodeListToStringsComplete(this.NManager.Nodes, true); break; default: data_header = NodeAssembler.GetNodeListHeader(); data_as_strings = NodeAssembler.NodeListToStrings(this.NManager.Nodes); break; } // prepare an empty document string filename = dlg.FileName; File.Delete(filename); File.Copy(".\\Data\\blank.xlsx", filename); // write data ExcelExporter excelExp = new ExcelExporter(); excelExp.ExportToFile(filename, "Export", data_header, data_as_strings); } } catch (Exception ex) { MessageBox.Show(ex.Message, "EXCEL File Export Error", MessageBoxButton.OK, MessageBoxImage.Error); // MessageBox.Show(ex.StackTrace, "EXCEL File Export Error", MessageBoxButton.OK, MessageBoxImage.Error); } }
private void OnImportEXCEL(object _type) { if (_type == null) { return; } NodesToExcelExportType type = NodeAssembler.StringToType(_type.ToString()); try { var dlg = new Microsoft.Win32.OpenFileDialog() { Filter = "excel files|*.xls;*.xlsx" }; // this causes a Securiy Exception (SQLite) if the propgram is not run as Administrator, // but it has no effect on the rest if (dlg.ShowDialog().Value) { if (File.Exists(dlg.FileName)) { // get the name of the table string tablename = "Tabelle1"; Window window = Application.Current.MainWindow; if (window != null) { MainWindow mw = window as MainWindow; if (mw != null) { tablename = mw.OpenTableNameWindow(); } } //imports the EXCEL file ExcelImporter excelImp = new ExcelImporter(); List <List <string> > raw_data = excelImp.ImportFromFile(dlg.FileName, tablename, 1000); // extract the data switch (type) { case NodesToExcelExportType.COMPLETE: this.imported_node_list = NodeAssembler.GetNodeListComplete(raw_data); break; case NodesToExcelExportType.EXPLICIT: this.imported_node_list = NodeAssembler.GetNodeListExplicit(raw_data); break; default: this.imported_node_list = NodeAssembler.GetNodeList(raw_data); break; } // attach to the nodemanager NodeAssembler.AttachNodesToNodeManager(this.imported_node_list, ref this.NManager); // publish to GUI this.OnRefreshTree(); } } } catch (Exception ex) { MessageBox.Show(ex.Message, "EXCEL File Import Error", MessageBoxButton.OK, MessageBoxImage.Error); // MessageBox.Show(ex.StackTrace, "EXCEL File Import Error", MessageBoxButton.OK, MessageBoxImage.Error); } }