/// <summary> /// Export CSV File /// </summary> /// <param name="fileName">Name of file</param> /// <param name="dataSource">Data source report</param> /// <param name="response">HttpResponse</param> public static void ExportCSVFile(string fileName, DataTable dataSource, System.Web.HttpResponse response) { byte[] bytes; if ((dataSource.Rows.Count == 0)) { string text = ""; if (dataSource.Columns.Count != 0) { text = dataSource.Columns[0].ColumnName; for (int i = 1; i < dataSource.Columns.Count; i++) { text = string.Format("{0},{1}", text, dataSource.Columns[i].ColumnName); } } text = string.Format("{0}\r\n", text); bytes = System.Text.Encoding.UTF8.GetBytes(text); } else { string text = dataSource.Columns[0].ColumnName; for (int i = 1; i < dataSource.Columns.Count; i++) { text = string.Format("{0},{1}", text, dataSource.Columns[i].ColumnName); } text = string.Format("{0}", text); for (int i = 0; i < dataSource.Rows.Count; i++) { string txtStr = StripCommaCharArray(dataSource.Rows[i][0].ToString()); for (int j = 1; j < dataSource.Columns.Count; j++) { txtStr = string.Format("{0},{1}", txtStr, ReportUtil.StripAllTagsCharArray(StripCommaCharArray(dataSource.Rows[i][j].ToString()))); } text = string.Format("{0}\r\n{1}", text, txtStr); txtStr = ""; } text = string.Format("{0}\r\n", text); bytes = System.Text.Encoding.UTF8.GetBytes(text); } // Now that you have all the bytes representing the PDF report, buffer it and send it to the client. response.Buffer = true; response.Clear(); response.ClearContent(); response.ClearHeaders(); response.ContentType = "text/csv"; response.ContentEncoding = System.Text.Encoding.UTF8; response.AddHeader("content-disposition", "attachment; filename=" + fileName + ".csv"); response.BinaryWrite(bytes); // create the file response.End(); // send it to the client to download }
/// <summary> /// Clean Tag HTML and NewLine /// </summary> /// <param name="dataSource">DataTable</param> /// <returns>DataTable with clean Tag HTML and NewLine</returns> public static DataTable CleanCSV(DataTable dataSource) { if ((dataSource.Rows.Count == 0)) { return(dataSource); } else { for (int i = 0; i < dataSource.Rows.Count; i++) { dataSource.Rows[i][0] = StripCommaCharArray(dataSource.Rows[i][0].ToString()); for (int j = 1; j < dataSource.Columns.Count; j++) { dataSource.Rows[i][j] = ReportUtil.StripAllTagsCharArray(StripCommaCharArray(dataSource.Rows[i][j].ToString())); } } return(dataSource); } }