Beispiel #1
0
        /// <summary>
        /// Writes the datatable to a *csv formatted String using the provided separator.
        /// </summary>
        /// <param name="datatable">the datatable that ahs to be saved</param>
        /// <param name="seperator">the seperator that delimits every column, in this case its always a ';'</param>
        /// <param name="path">The path to where the DataTable shall be written to.</param>
        public static Exception WriteDataTableToString(DataTable datatable, char seperator, out string csv)
        {
            csv = null;
            try
            {
                using (StringWriter sw = new StringWriter())
                {
                    int numberOfColumns = datatable.Columns.Count;

                    for (int i = 0; i < numberOfColumns; i++) //write Header names
                    {
                        sw.Write(datatable.Columns[i]);
                        if (i < numberOfColumns - 1)
                        {
                            sw.Write(seperator);
                        }
                    }

                    sw.Write(sw.NewLine);

                    foreach (DataRow dr in datatable.Rows) //write data
                    {
                        for (int i = 0; i < numberOfColumns; i++)
                        {
                            sw.Write(dr[i].ToString());

                            if (i < numberOfColumns - 1)
                            {
                                sw.Write(seperator);
                            }
                        }
                        sw.Write(sw.NewLine);
                    }
                    csv = sw.GetStringBuilder().ToString();
                    sw.Close();
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.AddLogEntry(Logging.Logger.LogEntryCategories.Fatal, "Error in CsvHandler.WriteDataTableToString() : " + ex);
                return(ex);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Writes the datatable to a *csv file using the provided separator.
        /// </summary>
        /// <param name="datatable">the datatable that ahs to be saved</param>
        /// <param name="seperator">the seperator that delimits every column, in this case its always a ';'</param>
        /// <param name="path">The path to where the DataTable shall be written to.</param>
        public static Exception WriteDataTable(DataTable datatable, char seperator, string path, Encoding encoding)
        {
            try
            {
                using (StreamWriter sw = new StreamWriter(path, false, encoding)) //overwrite existing files
                {
                    int numberOfColumns = datatable.Columns.Count;

                    for (int i = 0; i < numberOfColumns; i++) //write Header names
                    {
                        sw.Write(datatable.Columns[i]);
                        if (i < numberOfColumns - 1)
                        {
                            sw.Write(seperator);
                        }
                    }

                    sw.Write(sw.NewLine);

                    foreach (DataRow dr in datatable.Rows) //write data
                    {
                        for (int i = 0; i < numberOfColumns; i++)
                        {
                            sw.Write(dr[i].ToString());

                            if (i < numberOfColumns - 1)
                            {
                                sw.Write(seperator);
                            }
                        }
                        sw.Write(sw.NewLine);
                    }

                    sw.Close();
                }

                return(null);
            }
            catch (Exception ex)
            {
                Logger.AddLogEntry(Logger.LogEntryCategories.Fatal, "Error in CsvHandler.WriteDataTable() for file: " + path, ex);
                return(ex);
            }
        }