- Timestamp:
- 06/13/13 19:45:37 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/MenuItems/ExportSymbolicSolutionToExcelMenuItem.cs
r9607 r9626 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.ComponentModel; 24 25 using System.IO; 25 26 using System.Linq; … … 27 28 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views; 28 29 using HeuristicLab.MainForm; 30 using HeuristicLab.MainForm.WindowsForms; 29 31 using HeuristicLab.Optimizer; 30 32 using OfficeOpenXml; … … 52 54 53 55 protected override void OnToolStripItemSet(EventArgs e) { 56 base.OnToolStripItemSet(e); 54 57 ToolStripItem.Enabled = false; 55 } 56 protected override void OnActiveViewChanged(object sender, EventArgs e) { 58 var menuItem = ToolStripItem.OwnerItem as ToolStripMenuItem; 59 if (menuItem != null) 60 menuItem.DropDownOpening += menuItem_DropDownOpening; 61 } 62 63 private void menuItem_DropDownOpening(object sender, EventArgs e) { 57 64 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 58 ToolStripItem.Enabled = activeView != null && activeView.Content is ISymbolicDataAnalysisSolution; 65 Control control = activeView as Control; 66 activeView = control.GetNestedControls((c) => c.Visible) 67 .OfType<IContentView>().FirstOrDefault(v => v.Content is ISymbolicDataAnalysisSolution && v.Content is IRegressionSolution); 68 ToolStripItem.Enabled = activeView != null; 59 69 } 60 70 61 71 public override void Execute() { 62 var activeView = (IContentView)MainFormManager.MainForm.ActiveView; 72 IContentView activeView = MainFormManager.MainForm.ActiveView as IContentView; 73 Control control = activeView as Control; 74 activeView = control.GetNestedControls((c) => c.Visible) 75 .OfType<IContentView>().First(v => v.Content is ISymbolicDataAnalysisSolution && v.Content is IRegressionSolution); 63 76 var solution = (ISymbolicDataAnalysisSolution)activeView.Content; 64 77 var formatter = new SymbolicDataAnalysisExpressionExcelFormatter(); 65 var formula = formatter.Format(solution.Model.SymbolicExpressionTree );66 var formulaParts = formula.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 78 var formula = formatter.Format(solution.Model.SymbolicExpressionTree, solution.ProblemData.Dataset); 79 67 80 68 81 SaveFileDialog saveFileDialog = new SaveFileDialog(); … … 71 84 if (saveFileDialog.ShowDialog() == DialogResult.OK) { 72 85 string fileName = saveFileDialog.FileName; 73 FileInfo newFile = new FileInfo(fileName); 74 if (newFile.Exists) { 75 newFile.Delete(); 76 newFile = new FileInfo(fileName); 86 using (BackgroundWorker bg = new BackgroundWorker()) { 87 bg.DoWork += (b, e) => ExportChart(fileName, solution, formula); 88 bg.RunWorkerAsync(); 77 89 } 78 using (ExcelPackage package = new ExcelPackage(newFile)) { 79 ExcelWorksheet modelWorksheet = package.Workbook.Worksheets.Add("Model"); 80 FormatModelSheet(modelWorksheet, solution, formulaParts); 81 82 ExcelWorksheet datasetWorksheet = package.Workbook.Worksheets.Add("Dataset"); 83 WriteDatasetToExcel(datasetWorksheet, solution.ProblemData); 84 85 ExcelWorksheet inputsWorksheet = package.Workbook.Worksheets.Add("Inputs"); 86 WriteInputSheet(inputsWorksheet, datasetWorksheet, formulaParts.Skip(2), solution.ProblemData.Dataset); 87 88 if (solution is IRegressionSolution) { 89 ExcelWorksheet estimatedWorksheet = package.Workbook.Worksheets.Add("Estimated Values"); 90 WriteEstimatedWorksheet(estimatedWorksheet, datasetWorksheet, formulaParts, solution as IRegressionSolution); 91 92 ExcelWorksheet chartsWorksheet = package.Workbook.Worksheets.Add("Charts"); 93 AddCharts(chartsWorksheet); 94 } 95 package.Workbook.Properties.Title = "Excel Export"; 96 package.Workbook.Properties.Author = "HEAL"; 97 package.Workbook.Properties.Comments = "Excel export of a symbolic data analysis solution from HeuristicLab"; 98 99 package.Save(); 90 } 91 } 92 93 private void ExportChart(string fileName, ISymbolicDataAnalysisSolution solution, string formula) { 94 FileInfo newFile = new FileInfo(fileName); 95 if (newFile.Exists) { 96 newFile.Delete(); 97 newFile = new FileInfo(fileName); 98 } 99 var formulaParts = formula.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); 100 101 using (ExcelPackage package = new ExcelPackage(newFile)) { 102 ExcelWorksheet modelWorksheet = package.Workbook.Worksheets.Add("Model"); 103 FormatModelSheet(modelWorksheet, solution, formulaParts); 104 105 ExcelWorksheet datasetWorksheet = package.Workbook.Worksheets.Add("Dataset"); 106 WriteDatasetToExcel(datasetWorksheet, solution.ProblemData); 107 108 ExcelWorksheet inputsWorksheet = package.Workbook.Worksheets.Add("Inputs"); 109 WriteInputSheet(inputsWorksheet, datasetWorksheet, formulaParts.Skip(2), solution.ProblemData.Dataset); 110 111 if (solution is IRegressionSolution) { 112 ExcelWorksheet estimatedWorksheet = package.Workbook.Worksheets.Add("Estimated Values"); 113 WriteEstimatedWorksheet(estimatedWorksheet, datasetWorksheet, formulaParts, solution as IRegressionSolution); 114 115 ExcelWorksheet chartsWorksheet = package.Workbook.Worksheets.Add("Charts"); 116 AddCharts(chartsWorksheet); 100 117 } 118 package.Workbook.Properties.Title = "Excel Export"; 119 package.Workbook.Properties.Author = "HEAL"; 120 package.Workbook.Properties.Comments = "Excel export of a symbolic data analysis solution from HeuristicLab"; 121 122 package.Save(); 101 123 } 102 124 }
Note: See TracChangeset
for help on using the changeset viewer.