Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Backend.Csv/CsvExporter.cs @ 6774

Last change on this file since 6774 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.IO;
27using System.Globalization;
28using HeuristicLab.DataImporter.Data.Model;
29using HeuristicLab.DataImporter.DataProcessor;
30
31namespace HeuristicLab.DataImporter.Backend.Csv {
32  public class CsvExporter : IExporter{
33    public string Description {
34      get { return "Export as CSV file"; }
35    }
36
37    private CsvExporterView view;
38    public System.Windows.Forms.UserControl SettingsControl {
39      get {
40        if (view == null)
41          view = new CsvExporterView();
42        return view;
43      }
44    }
45
46    public void Export(DataSet dataSet) {
47      StreamWriter stream = new StreamWriter(view.FileName, false);     
48      if (dataSet.ColumnGroups.Count() != 0) {
49        StringBuilder sb = new StringBuilder();
50        if (view.IncludeColumnNames) {
51          foreach (ColumnGroup grp in dataSet.ColumnGroups)
52            foreach (ColumnBase col in grp.Columns)
53              sb.Append(col.Name + ';');
54          sb.Remove(sb.Length - 1, 1);
55          sb.Append(Environment.NewLine);
56          stream.Write(sb);
57          sb.Remove(0, sb.Length);
58        }
59
60        IComparable value;
61        for (int i = 0; i < dataSet.ColumnGroups.Max(x => x.RowCount); i++) {
62          foreach (ColumnGroup grp in dataSet.ColumnGroups) {
63              foreach (ColumnBase col in grp.Columns) {
64                value = col.GetValue(i);
65                if (value == null)
66                  sb.Append(';');
67                else if (value is double)
68                  sb.Append(((double)value).ToString(CultureInfo.InvariantCulture) + ';');
69                else
70                  sb.Append(value.ToString() + ';');
71              }
72          } //foreach columngroup
73          sb.Remove(sb.Length - 1, 1);
74          sb.Append(Environment.NewLine);
75          if (i % 100 == 0) {
76            stream.Write(sb.ToString());
77            sb.Remove(0, sb.Length);
78          }
79        } //foreach row
80        stream.Write(sb);
81      }
82      stream.Close();
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.