Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Visualization/3.2/DataExport/CSVDataExport.cs @ 1835

Last change on this file since 1835 was 1781, checked in by mstoeger, 16 years ago

Data Export (CSV) #588

File size: 1.5 KB
Line 
1using System;
2using System.IO;
3using System.Windows.Forms;
4
5namespace HeuristicLab.Visualization.DataExport {
6  public class CSVDataExport : IExporter {
7    public string Name {
8      get { return "CSV Export"; }
9    }
10
11    public void Export(IChartDataRowsModel model) {
12      SaveFileDialog sfd = new SaveFileDialog();
13      sfd.Filter = "CSV Files|*.csv|All Files|*.*";
14      if (sfd.ShowDialog() != DialogResult.OK) {
15        return;
16      }
17
18      string filename = sfd.FileName;
19
20      Export(model, filename);
21    }
22
23    private static void Export(IChartDataRowsModel model, string filename) {
24      using (FileStream fs = new FileStream(filename, FileMode.Create))
25      using (StreamWriter sw = new StreamWriter(fs)) {
26        CSVWriter writer = new CSVWriter(sw);
27
28        // write headers
29        writer.AddString(model.XAxisLabel);
30        foreach (IDataRow row in model.Rows)
31          writer.AddString(row.Label);
32        writer.NewLine();
33
34        // figure out max number of rows in all data rows
35        int maxItems = 0;
36        foreach (IDataRow row in model.Rows)
37          maxItems = Math.Max(maxItems, row.Count);
38
39        // write rows
40        for (int i = 0; i < maxItems; i++) {
41          writer.AddNumber(i);
42          foreach (IDataRow row in model.Rows) {
43            if (i < row.Count)
44              writer.AddNumber(row[i]);
45            else
46              writer.AddEmpty();
47          }
48          writer.NewLine();
49        }
50      }
51    }
52  }
53}
Note: See TracBrowser for help on using the repository browser.