Angular 12 CRUD Example
In this article, we will learn angular 12 CRUD example with web API as well as way to implement cascading dropdown, searching, sorting, and pagination...
May 30, 2021 read moreIn this article, we will learn angular 12 CRUD example with web API as well as way to implement cascading dropdown, searching, sorting, and pagination...
May 30, 2021 read moreIn this article i am going to explain how you can use angular js table with bootstrap 4 in asp.net web form, and also show you how you can display records...
December 08, 2018 read moreCodingvila also allowing a guest post for digital marketing, where you can explore your business, product, or services in terms of articles. write quality.......
August, 21, 2021 read moreIn this article, we will learn how to create a bar chart in angular 12 using ng2-charts. Here, I'll explain how to create an angular 12 project in visual........
May 29, 2021 read moreThis article gives an explanation about convert Datatable to CSV in c# and explains the efficient way to write CSV files from Datatable as well as show...
March 01, 2020 read moreThis article provides an explanation about how to merge multiple pdf files into single pdf in using Itextsharp in c# here I also explained the use of Itextsharp.
January 22, 2019 read more![]() |
Export Dataset/Datatable to CSV File Using C# and VB.NET |
DataTable GetData() { DataTable dt = new DataTable(); dt.Columns.Add("CustomerId", typeof(int)); dt.Columns.Add("CustomerName", typeof(string)); dt.Columns.Add("ProductName", typeof(string)); dt.Columns.Add("Price", typeof(double)); dt.Rows.Add(1, "Nikunj Satasiya", "Laptop", 37000); dt.Rows.Add(2, "Hiren Dobariya", "Mouse", 820); dt.Rows.Add(3, "Vivek Ghadiya", "Pen", 250); dt.Rows.Add(4, "Pratik Pansuriya", "Laptop", 42000); dt.Rows.Add(5, "Sneha Patel", "Lip Bam", 130); dt.Rows.Add(6, "Jhon Smith", "Sigar", 150); return dt; }
Private Function GetData() As DataTable Dim dt As DataTable = New DataTable dt.Columns.Add("CustomerId", GetType(System.Int32)) dt.Columns.Add("CustomerName", GetType(System.String)) dt.Columns.Add("ProductName", GetType(System.String)) dt.Columns.Add("Price", GetType(System.Double)) dt.Rows.Add(1, "Nikunj Satasiya", "Laptop", 37000) dt.Rows.Add(2, "Hiren Dobariya", "Mouse", 820) dt.Rows.Add(3, "Vivek Ghadiya", "Pen", 250) dt.Rows.Add(4, "Pratik Pansuriya", "Laptop", 42000) dt.Rows.Add(5, "Sneha Patel", "Lip Bam", 130) dt.Rows.Add(6, "Jhon Smith", "Sigar", 150) Return dt End Function
public static string WriteCSV(string input) { try { if (input == null) return string.Empty; bool containsQuote = false; bool containsComma = false; int len = input.Length; for (int i = 0; i < len && (containsComma == false || containsQuote == false); i++) { char ch = input[i]; if (ch == '"') containsQuote = true; else if (ch == ',') containsComma = true; } if (containsQuote && containsComma) input = input.Replace("\"", "\"\""); if (containsComma) return "\"" + input + "\""; else return input; } catch { throw; } }
Public Shared Function WriteCSV(ByVal input As String) As String Try If (input Is Nothing) Then Return String.Empty End If Dim containsQuote As Boolean = False Dim containsComma As Boolean = False Dim len As Integer = input.Length Dim i As Integer = 0 Do While ((i < len) _ AndAlso ((containsComma = False) _ OrElse (containsQuote = False))) Dim ch As Char = input(i) If (ch = Microsoft.VisualBasic.ChrW(34)) Then containsQuote = True ElseIf (ch = Microsoft.VisualBasic.ChrW(44)) Then containsComma = True End If i = (i + 1) Loop If (containsQuote AndAlso containsComma) Then input = input.Replace(""""", ", """"")", if, (containsCommaUnknown, """"" + input + ", "") Else Return input End If Catch As System.Exception Throw End Try End Function
private void btnCSV_Click(object sender, EventArgs e) { StringBuilder sb = new StringBuilder(); DataTable dt = GetData(); foreach (DataRow dr in dt.Rows) { foreach (DataColumn dc in dt.Columns) sb.Append(WriteCSV(dr[dc.ColumnName].ToString()) + ","); sb.Remove(sb.Length - 1, 1); sb.AppendLine(); } File.WriteAllText(@"C:\Users\Nikunj-PC\Desktop\Demo\CodingvilaCSV.csv", sb.ToString()); lblMsg.Text = "Successful"; }
Private Sub btnCSV_Click(ByVal sender As Object, ByVal e As EventArgs) Dim sb As StringBuilder = New StringBuilder Dim dt As DataTable = GetData For Each dr As DataRow In dt.Rows For Each dc As DataColumn In dt.Columns sb.Append((WriteCSV(dr(dc.ColumnName).ToString) + ",")) Next sb.Remove((sb.Length - 1), 1) sb.AppendLine() Next File.WriteAllText("C:\Users\Nikunj-PC\Desktop\Demo\CodingvilaCSV.csv", sb.ToString) lblMsg.Text = "Successful" End Sub
Visualizer |
![]() |
CSV File |
Codingvila provides articles and blogs on web and software development for beginners as well as free Academic projects for final year students in Asp.Net, MVC, C#, Vb.Net, SQL Server, Angular Js, Android, PHP, Java, Python, Desktop Software Application and etc.
Good example. Thank you! It helped me with a project I am working on. There were some errors with the VB code for the input function. I fixed them in the code below.
Public Shared Function WriteCSV(ByVal input As String) As String
Try
If (input Is Nothing) Then
Return String.Empty
End If
Dim containsQuote As Boolean = False
Dim containsComma As Boolean = False
Dim len As Integer = input.Length
Dim i As Integer = 0
Do While ((i < len) _
AndAlso ((containsComma = False) _
OrElse (containsQuote = False)))
Dim ch As Char = input(i)
If (ch = Microsoft.VisualBasic.ChrW(34)) Then
containsQuote = True
ElseIf (ch = Microsoft.VisualBasic.ChrW(44)) Then
containsComma = True
End If
i = (i + 1)
Loop
If (containsQuote AndAlso containsComma) Then
input = input.Replace("""", """""")
End If
If (containsComma) Then
Return """" & input & """"
Else
Return input
End If
Catch ex As Exception
Throw
End Try
End Function
Thank you for your valuable feedback. I will review your code and update the code in the article that you have fixed.
Please continue this great work and I look forward to more of your awesome blog posts. view
Thank you for your valuable time, to read this article, If you like this article, please share this article and post your valuable comments.
Once, you post your comment, we will review your posted comment and publish it. It may take a time around 24 business working hours.
Sometimes I not able to give detailed level explanation for your questions or comments, if you want detailed explanation, your can mansion your contact email id along with your question or you can do select given checkbox "Notify me" the time of write comment. So we can drop mail to you.
If you have any questions regarding this article/blog you can contact us on info.codingvila@gmail.com
Tutup Konverter!sentiment_satisfied Emoticon