Note:- column names are dynamic based on report. commn function is used for number of reports.
Expand|Select|Wrap|Line Numbers
- private void exportDataTableToCsv(DataTable formattedDataTable, string filename)
- {
- DataTable toExcel = formattedDataTable.Copy();
- HttpContext context = HttpContext.Current;
- context.Response.Clear();
- foreach (DataColumn column in toExcel.Columns)
- {
- context.Response.Write(column.ColumnName + ",");
- }
- context.Response.Write(Environment.NewLine);
- foreach (DataRow row in toExcel.Rows)
- {
- for (int i = 0; i < toExcel.Columns.Count; i++)
- {
- context.Response.Write(row[i].ToString().Replace(",", " ").Replace(Environment.NewLine, " ") + ",");
- }
- context.Response.Write(Environment.NewLine);
- }
- context.Response.ContentType = "text/csv"; // "application/vnd.xls"; // "text/csv";
- context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
- context.Response.End();
- })
- coulmn names in bold and colour
- applying border in reports.
- any other formatting which increases the visibilty of report.