David Ferry
David Ferry

Reputation: 293

how to export the converted datatable to textfile in web application using mvc

I using the mvc format program. i have list of the value in my data table. I convert this data-table --> text file using the stream-writer . But I don't know how its through the client for download option my sample code is give below, Please suggest your answer.

Upvotes: 0

Views: 2216

Answers (1)

Freelancer
Freelancer

Reputation: 9074

Refer Important part of code:

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();



}

Referance Link:

http://www.c-sharpcorner.com/UploadFile/cd19b9/how-to-download-datatable-to-text-file-in-C-Sharp/

Upvotes: 1

Related Questions