export datatable to csv c#- thumb

Tag | export datatable to csv c# windows application, export datatable to csv c# mvc, export datatable to csv c#.net, export datatable to csv file download in c#

Export datatable to CSV file download in C#

Logic working with ANY chars in the column(s), it uses StringBuilder for build output string, this approach radically increases performance. Find the source code below:-

export datatable to csv c# - 1 export datatable to csv c# Export Datatable to CSV C# ASP.Net export datatable to csv c 1

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Demo1.aspx.cs" Inherits="Demo1" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>export datatable to csv c#</title>
    <form id="form1" runat="server">
            <h2>Export Datatable to CSV File in C# - Click Below:</h2>
            <asp:Button ID="btnExport" runat="server" Text="Export To CSV" OnClick="btnExport_Click" />


using System;
using System.Data;
using System.Text;

public partial class Demo1 : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)

    protected void btnExport_Click(object sender, EventArgs e)
        DataTable dtTable = new DataTable();
        DataRow dtRow;

        dtTable.Columns.Add("SNo", typeof(int));
        dtTable.Columns.Add("Address", typeof(string));

        for (int i = 0; i <= 9; i++)
            dtRow = dtTable.NewRow();
            dtRow[0] = i;
            dtRow[1] = "Address " + i.ToString();

        Response.ContentType = "Application/x-msexcel";
        Response.AddHeader("content-disposition", "attachment;filename=TestFile.csv");

    private string ExportDatatableToCSV(DataTable dtTable)
        StringBuilder sbldr = new StringBuilder();
        if (dtTable.Columns.Count != 0)
            foreach (DataColumn col in dtTable.Columns)
                sbldr.Append(col.ColumnName + ',');
            foreach (DataRow row in dtTable.Rows)
                foreach (DataColumn column in dtTable.Columns)
                    sbldr.Append(row[column].ToString() + ',');
        return sbldr.ToString();

Export datatable to csv C# as Output

export datatable to csv c# 2 export datatable to csv c# Export Datatable to CSV C# ASP.Net export datatable to csv c 2

Download Source Code

Did you find this post useful? I hope you liked this article. Please share with me your valuable suggestions and feedback.


Please enter your comment!
Please enter your name here