Reader Level:
ARTICLE

How to Download DataTable to Text File in C#

Posted by Mohammed Kaleem Articles | Visual C# July 22, 2012
In this article we will see how to download a DataTable to a text file.
  • 0
  • 0
  • 21692
Download Files:
 

In this article we will see how to download a DataTable to a text file.

Add a new .aspx page.

In the design (.aspx) page add a button to it:

<asp:Button ID="btn_Download" runat="server" BackColor="#0033CC" Font-Bold="True"
            ForeColor="White" Text="Download" onclick="btn_Download_Click" />


Now in the code behind on the click event of this button, create an object to fetch the data from the database and store it in a DataSet.

protected void btn_Download_Click(object sender, EventArgs e)
{
    IInvoice obj = new Invoice();

    DataSet ds = obj.DownloadCustomerFile();

    string filename = "MyObCustomerCard.txt";

   // Exporting Data to text file

    ExportDataTabletoFile(ds.Tables[0], "    ", true, Server.MapPath("contents/MyObCustomerCard.txt"));

    #region download notepad or text file.

     Response.ContentType = "application/octet-stream";

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);

    string aaa = Server.MapPath("~/contents/" + filename);

    Response.TransmitFile(Server.MapPath("~/contents/" + filename));

    HttpContext.Current.ApplicationInstance.CompleteRequest();

    Response.End();

 

    #endregion

}
 

    #region download notepad or text file.

 

    Response.ContentType = "application/octet-stream";

        Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);

    string aaa = Server.MapPath("~/contents/" + filename);

    Response.TransmitFile(Server.MapPath("~/contents/" + filename));

    HttpContext.Current.ApplicationInstance.CompleteRequest();

    Response.End();

 

    #endregion

}


Here in the code above , "filename" is the name of the file in which we need to download the DataTable.

"MyObCustomerCard.txt" is the file which I have created manually and kept it in a separate "contents" folder in the root directory.

"ExportDataTabletoFile()" is a method which writes the data into the text file. We need to pass arguments to this method; namely DataTable, Delimeter (in this case "tab" is passed) , true or false for (whether to write column headers in the file) and file name with path.

Then there is the region of the download text file.

The following is the "ExportDataTabletoFile()" method:

public void ExportDataTabletoFile(DataTable datatable, string delimited, bool exportcolumnsheader, string file)

{

    StreamWriter str = new StreamWriter(file, false, System.Text.Encoding.Default);

    if (exportcolumnsheader)

    {

        string Columns = string.Empty;

        foreach (DataColumn column in datatable.Columns)

        {

            Columns += column.ColumnName + delimited;

        }

        str.WriteLine(Columns.Remove(Columns.Length - 1, 1));

    }

    foreach (DataRow datarow in datatable.Rows)

    {

        string row = string.Empty;

 

        foreach (object items in datarow.ItemArray)

        {

 

            row += items.ToString() + delimited;

        }

        str.WriteLine(row.Remove(row.Length - 1, 1));

 

    }

    str.Flush();

    str.Close();

 

}

That's it!!

Article Extensions
Contents added by nookaraju koppanati on Feb 10, 2013
COMMENT USING

Trending up