예제 #1
0
        public static bool SaveRowToFile(string filename, IDictionary <string, object> row, bool append = false,
                                         Encoding encoding = null, char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = row.Count;
                using (var writer = new CsvFileWriter(filename, append, encoding))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote     = quote;


                    var columns = new List <string>();
                    foreach (var o in row)
                    {
                        if (o.Value == null)
                        {
                            columns.Add(string.Empty);
                        }
                        else
                        {
                            columns.Add(o.Value.ToString());
                        }
                    }

                    writer.WriteRow(columns);
                }


                return(true);
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return(false);
        }
예제 #2
0
        public static bool SaveRowToFile(string filename, DataRow row, bool append = false, Encoding encoding = null,
                                         char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = row.ItemArray.Length;
                using (var writer = new CsvFileWriter(filename, append, encoding))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote     = quote;


                    var columns = new List <string>();
                    for (var col = 0; col < numColumns; col++)
                    {
                        if (row[col] == null || row[col] == DBNull.Value)
                        {
                            columns.Add(string.Empty);
                        }
                        else
                        {
                            columns.Add(row[col].ToString());
                        }
                    }

                    writer.WriteRow(columns);
                }


                return(true);
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return(false);
        }
예제 #3
0
        public static bool SaveToCsvFile(this DataTable table, string filename, bool append = false,
                                         Encoding encoding = null, char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = table.Columns.Count;
                using (var writer = new CsvFileWriter(filename, append, encoding ?? Encoding.UTF8))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote     = quote;
                    foreach (DataRow row in table.Rows)
                    {
                        var columns = new List <string>();
                        for (var col = 0; col < numColumns; col++)
                        {
                            var s = String.Empty;
                            if (row[col] != DBNull.Value && row[col] != null)
                            {
                                s = row[col].ToString();
                            }
                            columns.Add(s);
                        }


                        writer.WriteRow(columns);
                    }
                }

                return(true);
            }
            catch (Exception e)
            {
            }

            return(false);
        }
예제 #4
0
        /// <summary>
        /// Write csv file from this table
        /// </summary>
        /// <param name="filename">Path to file</param>
        /// <param name="append">Append to end of file</param>
        /// <param name="encoding">Encoding</param>
        /// <param name="delimeter">Delimeter between parameters in files</param>
        /// <param name="quote">Brackets around text</param>
        /// <returns>true if file save seccessfully</returns>
        public bool SaveToFile(string filename, bool append = false, Encoding encoding = null, char delimeter = ',',
                               char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = Columns.Count;
                using (var writer = new CsvFileWriter(filename, append, encoding ?? Encoding.UTF8))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote     = quote;
                    foreach (DataRow row in Rows)
                    {
                        var columns = new List <string>();
                        for (var col = 0; col < numColumns; col++)
                        {
                            var s = string.Empty;
                            if (row[col] != DBNull.Value && row[col] != null)
                            {
                                s = row[col].ToString();
                            }
                            columns.Add(s);
                        }

                        writer.WriteRow(columns);
                    }
                }
                FileName = filename;

                return(true);
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return(false);
        }
예제 #5
0
        public static bool SaveRowToFile(string filename, List<string> row, bool append = false, Encoding encoding = null, char delimeter = ',', char quote = '"')
        {
            try
            {
                using (var writer = new CsvFileWriter(filename, append, encoding))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote = quote;

                    var columns = row.Select(o => o ?? "").ToList();

                    writer.WriteRow(columns);
                    writer.Close();
                }

                return true;
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return false;
        }
예제 #6
0
        public static bool SaveRowToFile(string filename, IDictionary<string, object> row, bool append = false, Encoding encoding = null, char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = row.Count;
                using (var writer = new CsvFileWriter(filename, append, encoding))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote = quote;

                    var columns = new List<string>();
                    foreach (var o in row)
                    {
                        if (o.Value == null)
                        {
                            columns.Add("");
                        }
                        else
                        {
                            columns.Add(o.Value.ToString());
                        }
                    }

                    writer.WriteRow(columns);
                }

                return true;
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return false;
        }
예제 #7
0
        public static bool SaveToCsvFile(this DataTable table, string filename, bool append = false, Encoding encoding=null, char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = table.Columns.Count;
                using (var writer = new CsvFileWriter(filename, append,encoding??Encoding.UTF8))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote = quote;
                    foreach (DataRow row in table.Rows)
                    {

                        var columns = new List<string>();
                        for (var col = 0; col < numColumns; col++)
                        {
                            var s = String.Empty;
                            if (row[col] != DBNull.Value && row[col] != null)
                            {
                                s = row[col].ToString();
                            }
                            columns.Add(s);
                        }

                        writer.WriteRow(columns);
                    }

                }

                return true;
            }
            catch (Exception e)
            {

            }

            return false;
        }
예제 #8
0
        /// <summary>
        /// Write csv file from this table
        /// </summary>
        /// <param name="filename">Path to file</param>
        /// <param name="append">Append to end of file</param>
        /// <param name="encoding">Encoding</param>
        /// <param name="delimeter">Delimeter between parameters in files</param>
        /// <param name="quote">Brackets around text</param>
        /// <returns>true if file save seccessfully</returns>
        public bool SaveToFile(string filename, bool append = false, Encoding encoding=null,char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = this.Columns.Count;
                using (var writer = new CsvFileWriter(filename, append,encoding??Encoding.UTF8))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote = quote;
                    foreach (DataRow row in Rows)
                    {

                        var columns = new List<string>();
                        for (var col = 0; col < numColumns; col++)
                        {

                            var s = string.Empty;
                            if (row[col] != DBNull.Value && row[col] !=null)
                            {
                                s=row[col].ToString();
                            }
                            columns.Add(s);
                        }

                        writer.WriteRow(columns);
                    }

                }
                FileName = filename;

                return true;
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            finally
            {
            }
            return false;
        }
예제 #9
0
        public static bool SaveRowToFile(string filename, DataRow row, bool append = false, Encoding encoding = null, char delimeter = ',', char quote = '"')
        {
            try
            {
                // Like Excel, we'll get the highest column number used,
                // and then write out that many columns for every row
                var numColumns = row.ItemArray.Length;
                using (var writer = new CsvFileWriter(filename, append, encoding))
                {
                    writer.Delimiter = delimeter;
                    writer.Quote = quote;

                    var columns = new List<string>();
                    for (var col = 0; col < numColumns; col++)
                    {
                        if (row[col] == null || row[col] == DBNull.Value)
                        {
                            columns.Add("");
                        }
                        else
                        {
                            columns.Add(row[col].ToString());
                        }

                    }

                    writer.WriteRow(columns);
                }

                return true;
            }
            catch (Exception)
            {
                // MessageBox.Show(String.Format("Error writing to {0}.\r\n\r\n{1}", filename, ex.Message));
            }
            return false;
        }