We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

How to make text font as bold while exporting gridview to .CSV

Medium Priority
5,946 Views
Last Modified: 2012-05-06
I have a header column while exporting  to .CSV.How to make header font as bold while exporting gridview to .CSV
System.Text.StringBuilder sbCsv = new StringBuilder();
        DataSet ds = (DataSet)Session[this.ReportDSID];
        DataTable dtTemp = new DataTable();
        dtTemp = ds.Tables[0];
        int colcount = dtTemp.Columns.Count;
        int rowcount = dtTemp.Rows.Count;
        sbCsv.Append("<table><tr><td>Downloaded Date</td></tr></table>");
        sbCsv.Append(",");
        sbCsv.Append("\n");
        for (int i = 0; i < colcount; i++)
        {
            sbCsv.Append(this.ReportGridView.Columns[i]);
            if (i < colcount - 1)
            {
                sbCsv.Append(",");
            }
        }
        for (int intRow = 0; intRow < rowcount; intRow++)
        {
            StringBuilder sbRow = new StringBuilder();
            for (int intCol = 0; intCol < colcount; intCol++)
            {
                if (sbRow.Length > 0)
                {
                    sbRow.Append(",");
                }
                sbRow.Append(_EscapeCsvField(dtTemp.Rows[intRow][intCol].ToString()));
            }
            if (sbCsv.Length > 0)
            {
                sbCsv.Append("\n");
            }
            sbCsv.Append(sbRow.ToString());
        }
 
        Response.Clear();
        Response.ContentType = "application/excel";
        Response.BinaryWrite(System.Text.ASCIIEncoding.ASCII.GetBytes(sbCsv.ToString()));
        Response.AppendHeader("content-disposition", "attachment; filename=wtf.csv");
        Response.End();

Open in new window

Comment
Watch Question

How about have your actual data within tr and td tags of that table then you can just surround with <b> and </b>?
Praveen VenuTechnical Project Manager

Commented:
CSV file format contains no styling information
Technical Project Manager
Commented:
if you want to export gridview to excel use this

 private void ExportGridView()
 
    {
 
        string attachment = "attachment; filename=Contacts.xls";
 
        Response.ClearContent();
 
        Response.AddHeader("content-disposition", attachment);
 
        Response.ContentType = "application/ms-excel";
 
        StringWriter sw = new StringWriter();
 
        HtmlTextWriter htw = new HtmlTextWriter(sw);
 
        GridView1.RenderControl(htw);
 
        Response.Write(sw.ToString());
 
        Response.End();
 
    }

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Excel sheet is not supporting large number of records.I need .CSV format only.bu I needs to be put header font as bold.Kindly help me.
Praveen VenuTechnical Project Manager

Commented:
It is not possible since CSV format doesnot hold any styling information

Author

Commented:
Ok.But I have 59,000 records in my gridview.When I tried to export to excel sheet,the system.outofmemory exception occured.Thats why i have choosen .CSV format.but styles are not working here.Can any one provide the code for export to excel to overcome that exception.

Author

Commented:
pls can any one respond.My Export to Excel code is
private void ExportToExcel()
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + this.ReportName + ".xls");
        this.EnableViewState = false;
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        HttpContext.Current.Response.ContentType = "application/vnd.xls";
        DataSet ds = (DataSet)Session[this.ReportDSID];
 
        this.Page.DataBind();
 
        this.ReportGridView.AllowPaging = false;
        if (ds.Tables[0].Rows.Count < 60000)
        {
 
            //DataTable dttemp = new DataTable();
 
            //dttemp = ds.Tables[0].Clone();
 
 
            //foreach (DataColumn dc in dttemp.Columns)
            //{
            //    if (dc.DataType == typeof(DateTime))
            //    {
            //        dc.DataType = typeof(string);
            //    }
            //}
 
            //foreach (DataRow dr in ds.Tables[0].Rows)
            //{
            //    dttemp.ImportRow(dr);
            //}
            this.ReportGridView.DataSource = ds;
            this.ReportGridView.DataBind();
            #region Modified Again
 
 
            using (System.IO.StringWriter sw = new System.IO.StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {
                    Table table = new Table();
 
                    table.GridLines = ReportGridView.GridLines;
                    if (this.ReportGridView.HeaderRow != null)
                    {
                        PrepareControlForExport(this.ReportGridView.HeaderRow);
                        table.Rows.Add(this.ReportGridView.HeaderRow);
                    }
 
                    foreach (GridViewRow row in this.ReportGridView.Rows)
                    {
                        PrepareControlForExport(row);
                        table.Rows.Add(row);
                    }
 
                    if (this.ReportGridView.FooterRow != null)
                    {
                        PrepareControlForExport(this.ReportGridView.FooterRow);
                        table.Rows.Add(this.ReportGridView.FooterRow);
                    }
                    #region Modified by Srikanth on 30102008
                    globalwms_userobj ObjUser = new globalwms_userobj();
                    ObjUser = (globalwms_userobj)Session["userinfo"];
                    globalwms_bll objBLL = new globalwms_bll();
                    GeoReturnObject objReturn = new GeoReturnObject();
                    objReturn = objBLL.GetGMTOffsetTime(ObjUser.UserName);
                    DataSet dsGMTOffsetTime = new DataSet();
                    dsGMTOffsetTime = (DataSet)objReturn.RetObject;
                    double GMTOffsetTime = Convert.ToDouble(dsGMTOffsetTime.Tables[0].Rows[0][1].ToString());
                    string TempDate = DateTime.Now.AddHours(GMTOffsetTime).ToString();
                    if (ObjUser.DateFormat == "dd/MM/yyyy")
                    {
                        htw.WriteLine("<b><u><i><font size='5'>" +
                        "Downloaded Date and Time: " + DateTime.Now.ToString("dd/MM/yyyy") + "&nbsp;" + DateTime.Now.AddHours(GMTOffsetTime).ToShortTimeString() +
                        "</font></i></u></b>");
                    }
                    else if (ObjUser.DateFormat == "MM/dd/yyyy")
                    {
                        htw.WriteLine("<b><u><i><font size='5'>" +
                        "Downloaded Date and Time: " + DateTime.Now.ToString("MM/dd/yyyy") + "&nbsp;" + DateTime.Now.AddHours(GMTOffsetTime).ToShortTimeString() +
                        "</font></i></u></b>");
                    }
                    else
                    {
                        htw.WriteLine("<b><u><i><font size='5'>" +
                        "Downloaded Date and Time: " + DateTime.Now.ToString("yyyy/MM/dd") + "&nbsp;" + DateTime.Now.AddHours(GMTOffsetTime).ToShortTimeString() +
                        "</font></i></u></b>");
                    }
                    #endregion
 
                    table.RenderControl(htw);
                    //for (int count = 0; count < table.Rows.Count; count++)
                    //{
                    //    if (Response.BufferOutput == true && count % 500 == 0)
                    //    {
                    //        Response.Flush();
                    //    }
                    //}
                    string finalHTML = sw.ToString();
                    finalHTML = finalHTML.Replace("<a", "<o");
                    finalHTML = finalHTML.Replace("</a", "</o");
                    HttpContext.Current.Response.Write(finalHTML);
                    HttpContext.Current.Response.End();
                }
            }
            #endregion
        }
        else
        {
            Response.Write("<script>alert('The Record count was crossed the Excel Sheet Limit.');history.back(1);</script>");
        }
    }

Open in new window

Author

Commented:
thanks for help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.