Following function is used to write DataTable into the text file and then download that file. Pass the datatable as an argument to this function.
public void WriteDataToFile(DataTable submittedDataTable) { int i = 0; MemoryStream ms = new MemoryStream(); TextWriter tw = new StreamWriter(ms); foreach (DataRow row in submittedDataTable.Rows) { object[] array = row.ItemArray; for (i = 0; i < array.Length - 1; i++) { tw.Write(array[i].ToString() + ";"); } tw.Write(array[i].ToString()); tw.WriteLine(); } tw.Flush(); byte[] bytes = ms.ToArray(); ms.Close(); Response.Clear(); Response.ContentType = "application/force-download"; Response.AddHeader("content-disposition", "attachment;filename=MPBP.txt"); Response.BinaryWrite(bytes); Response.End(); }
No comments:
Post a Comment