COANetwork
asked on
How to make text font as bold while exporting gridview to .CSV
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();
How about have your actual data within tr and td tags of that table then you can just surround with <b> and </b>?
CSV file format contains no styling information
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
It is not possible since CSV format doesnot hold any styling information
ASKER
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.
ASKER
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") + " " + 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") + " " + 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") + " " + 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>");
}
}
ASKER
thanks for help