Exemple #1
0
        public static void ToCSV(string path, string[,] data)
        {
            System.Data.DataTable dt = new System.Data.DataTable();

            int row = data.GetLength(0);
            int column = data.GetLength(1);
            for (int j = 0; j < column; j++)
            {
                dt.Columns.Add(data[0, j], typeof(String));
            }

            for (int i = 0; i < row; i++)   //含表头
            {
                dt.Rows.Add(dt.NewRow());
                for (int j = 0; j < column; j++)
                {
                    if (!String.IsNullOrEmpty(data[i, j]))
                    {
                        dt.Rows[i][j] = "\"" + data[i, j].Replace("\"", "\"\"") + "\"";
                    }
                }
            }
            dt.AcceptChanges();

            CsvOptions options = new CsvOptions("String[,]", ',', data.GetLength(1));
            CsvEngine.DataTableToCsv(dt, path, options);
        }
Exemple #2
0
        private void GenerateCsv(SqlTableSelect table)
        {
            _logger.Log("\t" + table.TableName);
            var select = _selectionFilteringStrategy.GetFilteredSelectStatement(table);
            var results =
                _queryExecutor.ExecuteSelectStatement(select);
            foreach (var column in table.ExcludedColumns)
            {
                results.Columns.Remove(column);
            }

            var csvOptions = new CsvOptions(DontCare, _delimiter, results.Columns.Count) {DateFormat = "g", Encoding = Encoding.UTF8};

            var filename = _destinationDirectory + table.TableName.ToLower() + ".csv";
            try
            {
                CsvEngine.DataTableToCsv(results, filename, csvOptions);
            }
            catch (UnauthorizedAccessException ex)
            {
                throw new nDumpApplicationException(
                    "nDump cannot access the CSV file at " + filename +
                    ". Is it checked out (TFS) or modifiable? This error may also occur if the file has been opened by another program.",
                    ex);
            }
        }
Exemple #3
0
        private void GenerateCsv(SqlTableSelect table)
        {
            _logger.Log("     " + table.TableName);
            var select = _selectionFilteringStrategy.GetFilteredSelectStatement(table);
            DataTable results =
                _queryExecutor.ExecuteSelectStatement(select);
            foreach (var column in table.ExcludedColumns)
            {
                results.Columns.Remove(column);
            }

            var csvOptions = new CsvOptions(DontCare, ',', results.Columns.Count) {DateFormat = "g"};
            CsvEngine.DataTableToCsv(results, _destinationDirectory + table.TableName.ToLower() + ".csv", csvOptions);
        }
Exemple #4
0
 /// <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)
 {
     return(CsvEngine.CsvToDataTable(filename, options));
 }
Exemple #5
0
 /// <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)
 {
     CsvEngine.DataTableToCsv(dt, filename, options);
 }
Exemple #6
0
		/// <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))
			{
			    for(int i = 0; i < dt.Columns.Count; i++)
					{
						if (i > 0)
							fs.Write(options.Delimiter);
						
						fs.Write(dt.Columns[i].ColumnName);
					}
                fs.Write(StringHelper.NewLine);
				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);

					    string convertedValue = options.ValueToString(fields[i]);

                        fs.Write(convertedValue.TrimEnd(new char[] { '\n', ' ', '\r' }));
					}
					fs.Write(StringHelper.NewLine);
				}
				fs.Close();
			}
		}
Exemple #7
0
		/// <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);
		}
Exemple #8
0
		/// <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);
		}
Exemple #9
0
		private static Type GetMappingClass(CsvOptions options)
		{
			CsvClassBuilder cb = new CsvClassBuilder(options);
			return cb.CreateRecordClass();
		}
Exemple #10
0
		/// <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))
		{
		}
        /// <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));
        }
        private static Type GetMappingClass(CsvOptions options)
        {
            CsvClassBuilder cb = new CsvClassBuilder(options);

            return(cb.CreateRecordClass());
        }
 /// <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))
 {
 }
Exemple #14
0
		/// <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();
			}
		}