CsvEngine.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpers » CsvEngine.cs
#undef GENERICS
//#define GENERICS
//#if NET_2_0

#region "   Copyright 2005-07 to Marcos Meli - http://www.marcosmeli.com.ar" 

// Errors, suggestions, contributions, send a mail to: marcos@filehelpers.com.

#endregion

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Text;

using FileHelpers.RunTime;

#if ! MINI
using System.Data;
#endif


namespace FileHelpers{


  /// <summary>A class to read generic CSV files delimited for any char.</summary>
#if NET_2_0
    [DebuggerDisplay("CsvEngine. ErrorMode: {ErrorManager.ErrorMode.ToString()}. Encoding: {Encoding.EncodingName}")]
#endif
    public sealed class CsvEngine : FileHelperEngine
  {

    #region "  Static Methods  "

    /// <summary>Reads a Csv File and return their contents as DataTable (The file must have the field names in the first row)</summary>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="filename">The file to read.</param>
    /// <returns>The contents of the file as a DataTable</returns>
    public static DataTable CsvToDataTable(string filename, char delimiter)
    {
      return CsvToDataTable(filename, "RecorMappingClass", delimiter, true);
    }

    /// <summary>Reads a Csv File and return their contents as DataTable (The file must have the field names in the first row)</summary>
    /// <param name="classname">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="filename">The file to read.</param>
    /// <returns>The contents of the file as a DataTable</returns>
    public static DataTable CsvToDataTable(string filename, string classname, char delimiter)
    {
      return CsvToDataTable(filename, classname, delimiter, true);
    }


    /// <summary>Reads a Csv File and return their contents as DataTable</summary>
    /// <param name="classname">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="filename">The file to read.</param>
    /// <param name="hasHeader">Indicates if the file contains a header with the field names.</param>
    /// <returns>The contents of the file as a DataTable</returns>
    public static DataTable CsvToDataTable(string filename, string classname, char delimiter, bool hasHeader)
    {
      CsvOptions options = new CsvOptions(classname, delimiter, filename);
      if (hasHeader == false) options.HeaderLines = 0;
      return CsvToDataTable(filename, options);
    }

    /// <summary>Reads a Csv File and return their contents as DataTable</summary>
    /// <param name="filename">The file to read.</param>
    /// <param name="options">The options used to create the record mapping class.</param>
    /// <returns>The contents of the file as a DataTable</returns>
    public static DataTable CsvToDataTable(string filename, CsvOptions options)
    {
      CsvEngine engine = new CsvEngine(options);
      return engine.ReadFileAsDT(filename);
    }


        
    /// <summary>Simply dumps the DataTable contents to a delimited file using a ',' as delimiter.</summary>
    /// <param name="dt">The source Data Table</param>
    /// <param name="filename">The destination file.</param>
    public static void DataTableToCsv(DataTable dt, string filename)
    {
      DataTableToCsv(dt, filename, new CsvOptions("Tempo1", ',', dt.Columns.Count));
    }


    /// <summary>Simply dumps the DataTable contents to a delimited file. Only allows to set the delimiter.</summary>
    /// <param name="dt">The source Data Table</param>
    /// <param name="filename">The destination file.</param>
    /// <param name="options">The options used to write the file</param>
    public static void DataTableToCsv(DataTable dt, string filename, CsvOptions options)
    {
      using (StreamWriter fs = new StreamWriter(filename, false, options.Encoding))
      {
        foreach (DataRow dr in dt.Rows)
        {
          object[] fields = dr.ItemArray;
          
          for(int i = 0; i < fields.Length; i++)
          {
            if (i > 0)
              fs.Write(options.Delimiter);
            
            fs.Write(options.ValueToString(fields[i]));
          }
          fs.Write(StringHelper.NewLine);
        }
        fs.Close();
      }
    }

//    private static string ObjectString(CsvOptions options, object o)
//    {
//    }

    #endregion

    #region "  Constructor  "

    /// <summary>Create a CsvEngine using the specified sample file with their headers.</summary>
    /// <param name="className">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="sampleFile">A sample file with a header that contains the names of the fields.</param>
    public CsvEngine(string className, char delimiter, string sampleFile): this(new CsvOptions(className, delimiter, sampleFile))
    {}

    /// <summary>Create a CsvEngine using the specified number of fields.</summary>
    /// <param name="className">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="numberOfFields">The number of fields of each record</param>
    public CsvEngine(string className, char delimiter, int numberOfFields): this(new CsvOptions(className, delimiter, numberOfFields))
    {}

    /// <summary>Create a CsvEngine using the specified sample file with their headers.</summary>
    /// <param name="options">The options used to create the record mapping class.</param>
    public CsvEngine(CsvOptions options): base(GetMappingClass(options))
    {
    }

    #endregion

    private static Type GetMappingClass(CsvOptions options)
    {
      CsvClassBuilder cb = new CsvClassBuilder(options);
      return cb.CreateRecordClass();
    }
  }


}

//#endif
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.