Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# convert datatable to csv

  /// <summary>
        /// Convert DataTable to CSV
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="table"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public bool Write(string filePath, DataTable table, string fileName)
        {
            bool success = false;
            try
            {
                var columns = table.Columns.Cast<DataColumn>().Select(x => x.ColumnName).ToList();
                string[] columnsArray = Util_DataConversion.ConvertListToArray(columns);
                sb.AppendLine(string.Join(",", columnsArray));

                foreach (DataRow rw in table.Rows)
                {
                   
                    string[] flds = rw.ItemArray.Select(x => x.ToString()).ToArray();
                    sb.AppendLine(string.Join(",", flds));
                }

                File.WriteAllText(filePath + "" + fileName, sb.ToString());
                success = true;
                return success;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: rest api in c# 
Csharp :: how to make infinite loop in c# 
Csharp :: partial mvc 
Csharp :: custom vscode snippet 
Csharp :: c# online code editor 
Csharp :: unity int to bool 
Csharp :: dotnet core webapp 
Csharp :: check list exist in list c# if matches any 
Csharp :: c# loop backwards 
Csharp :: c# web scraping get images from specific url 
Csharp :: C#: casting string to enum object 
Csharp :: unity3d sort list 
Csharp :: unity destroy gameobject with delay 
Csharp :: unity getcomponent transform.position 
Csharp :: how to scale text from center in unity 
Csharp :: getawaiter and no extension method 
Csharp :: cancellationtoken.linkedtokensource c# example 
Html :: qs cdn 
Html :: html disable editing textbox 
Html :: placeholder select html 
Html :: html new target 
Html :: python find remove html tags 
Html :: add title logo html 
Html :: adding a favicon in html 
Html :: input type="file" and display image 
Html :: Hello World HTML Code Example 
Html :: html make file input only images 
Html :: auto update copyright year javascript 
Html :: how to change tab logo html 
Html :: django html checkbox if condition chexked 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =