[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[9615] | 3 | * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 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 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.IO;
|
---|
| 27 | using System.Globalization;
|
---|
| 28 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 29 | using HeuristicLab.DataImporter.DataProcessor;
|
---|
| 30 |
|
---|
| 31 | namespace 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 | }
|
---|