#region License Information /* HeuristicLab * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Globalization; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.DataImporter.DataProcessor; namespace HeuristicLab.DataImporter.Backend.Csv { public class CsvExporter : IExporter{ public string Description { get { return "Export as CSV file"; } } private CsvExporterView view; public System.Windows.Forms.UserControl SettingsControl { get { if (view == null) view = new CsvExporterView(); return view; } } public void Export(DataSet dataSet) { StreamWriter stream = new StreamWriter(view.FileName, false); if (dataSet.ColumnGroups.Count() != 0) { StringBuilder sb = new StringBuilder(); if (view.IncludeColumnNames) { foreach (ColumnGroup grp in dataSet.ColumnGroups) foreach (ColumnBase col in grp.Columns) sb.Append(col.Name + ';'); sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); stream.Write(sb); sb.Remove(0, sb.Length); } IComparable value; for (int i = 0; i < dataSet.ColumnGroups.Max(x => x.RowCount); i++) { foreach (ColumnGroup grp in dataSet.ColumnGroups) { foreach (ColumnBase col in grp.Columns) { value = col.GetValue(i); if (value == null) sb.Append(';'); else if (value is double) sb.Append(((double)value).ToString(CultureInfo.InvariantCulture) + ';'); else sb.Append(value.ToString() + ';'); } } //foreach columngroup sb.Remove(sb.Length - 1, 1); sb.Append(Environment.NewLine); if (i % 100 == 0) { stream.Write(sb.ToString()); sb.Remove(0, sb.Length); } } //foreach row stream.Write(sb); } stream.Close(); } } }