Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Csv/CsvExporter.cs @ 16994

Last change on this file since 16994 was 16994, checked in by gkronber, 5 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using System.Text;
25using System.IO;
26using System.Globalization;
27using HeuristicLab.DataImporter.Data.Model;
28using HeuristicLab.DataImporter.DataProcessor;
29
30namespace HeuristicLab.DataImporter.Backend.Csv {
31  public class CsvExporter : IExporter{
32    public string Description {
33      get { return "Export as CSV file"; }
34    }
35
36    private CsvExporterView view;
37    public System.Windows.Forms.UserControl SettingsControl {
38      get {
39        if (view == null)
40          view = new CsvExporterView();
41        return view;
42      }
43    }
44
45    public void Export(DataSet dataSet) {
46      StreamWriter stream = new StreamWriter(view.FileName, false);     
47      if (dataSet.ColumnGroups.Count() != 0) {
48        StringBuilder sb = new StringBuilder();
49        if (view.IncludeColumnNames) {
50          foreach (ColumnGroup grp in dataSet.ColumnGroups)
51            foreach (ColumnBase col in grp.Columns)
52              sb.Append(col.Name + ';');
53          sb.Remove(sb.Length - 1, 1);
54          sb.Append(Environment.NewLine);
55          stream.Write(sb);
56          sb.Remove(0, sb.Length);
57        }
58
59        IComparable value;
60        for (int i = 0; i < dataSet.ColumnGroups.Max(x => x.RowCount); i++) {
61          foreach (ColumnGroup grp in dataSet.ColumnGroups) {
62              foreach (ColumnBase col in grp.Columns) {
63                value = col.GetValue(i);
64                if (value == null)
65                  sb.Append(';');
66                else if (value is double)
67                  sb.Append(((double)value).ToString(CultureInfo.InvariantCulture) + ';');
68                else
69                  sb.Append(value.ToString() + ';');
70              }
71          } //foreach columngroup
72          sb.Remove(sb.Length - 1, 1);
73          sb.Append(Environment.NewLine);
74          if (i % 100 == 0) {
75            stream.Write(sb.ToString());
76            sb.Remove(0, sb.Length);
77          }
78        } //foreach row
79        stream.Write(sb);
80      }
81      stream.Close();
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.