how to format CSV Reports generated

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khushbu shah
    New Member
    • Aug 2008
    • 8

    how to format CSV Reports generated

    C# ASP.Net
    Note:- column names are dynamic based on report. commn function is used for number of reports.

    Code:
      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();
            })
    In my web application we are generating reports in csv format we want to do some formatting in report format like-
    - coulmn names in bold and colour
    - applying border in reports.
    - any other formatting which increases the visibilty of report.
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Here is an article that may help:
    Comma-separated values

    Comment

    Working...