Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2009-02-09
8
Medium Priority
?
3,884 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

0
Comment
Question by:COANetwork
  • 4
  • 3
8 Comments
 
LVL 19

Expert Comment

by:daveamour
ID: 23589463
How about have your actual data within tr and td tags of that table then you can just surround with <b> and </b>?
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23589474
CSV file format contains no styling information
0
 
LVL 15

Accepted Solution

by:
Praveen Venu earned 1500 total points
ID: 23589495
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

0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 9

Author Comment

by:COANetwork
ID: 23598029
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.
0
 
LVL 15

Expert Comment

by:Praveen Venu
ID: 23598041
It is not possible since CSV format doesnot hold any styling information
0
 
LVL 9

Author Comment

by:COANetwork
ID: 23598401
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.
0
 
LVL 9

Author Comment

by:COANetwork
ID: 23598995
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

0
 
LVL 9

Author Closing Comment

by:COANetwork
ID: 31544469
thanks for help
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question