Click here to Skip to main content
15,792,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have used export to excel from gridview.when click on the download button, the excel sheet open but with formatting(like black borders).

how to remove the black border ..i just want to use the default cell style of excel.

Please help..

What I have tried:

C#
try {
	string excel_name = "Call-Log-(KBD/SWH)";
	HttpContext.Current.Response.Clear();
	//  HttpContext.Current.Response.ClearContent()
	//  HttpContext.Current.Response.ClearHeaders()
	HttpContext.Current.Response.Buffer = true;
	HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
	HttpContext.Current.Response.Write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
	HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + excel_name + "(" + System.DateTime.Now + ").xls");

	HttpContext.Current.Response.Charset = "utf-8";
	HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("windows-1250");
	//sets font
	HttpContext.Current.Response.Write("<font style="font-size: 10.0pt; font-family: Calibri">");
	HttpContext.Current.Response.Write("<br><br><br>");
	//sets the table border, cell spacing, border color, font of the text, background, foreground, font height
	HttpContext.Current.Response.Write("");
	//am getting my grid's column headers
	int columnscount = GridView1.Columns.Count;

	for (int j = 0; j <= columnscount - 1; j++) {
		//write in new column
		HttpContext.Current.Response.Write("");
	}

	HttpContext.Current.Response.Write("");
	foreach (DataRow row in table.Rows) {
		//write in new row
		HttpContext.Current.Response.Write("");
		for (int i = 0; i <= table.Columns.Count - 1; i++) {
			HttpContext.Current.Response.Write("");
		}

		HttpContext.Current.Response.Write("");
	}
	HttpContext.Current.Response.Write("<table border="1" cellspacing="0" cellpadding="0"> <tbody><tr><td>");
		//Get column headers  and make it as bold in excel columns
		HttpContext.Current.Response.Write("");
		HttpContext.Current.Response.Write(GridView1.Columns(j).HeaderText.ToString());
		HttpContext.Current.Response.Write("");
		HttpContext.Current.Response.Write("</td></tr><tr><td>");
			HttpContext.Current.Response.Write(row(i).ToString());
			HttpContext.Current.Response.Write("</td></tr></tbody></table>");
	HttpContext.Current.Response.Write("</font>");
	HttpContext.Current.Response.Flush();
	HttpContext.Current.Response.End();

} catch (Exception ex) {
}
Posted
Updated 20-Oct-16 3:29am

What you are doing is not exporting to Excel. You are exporting to HTML with an xls extension.

I would start with using a component that can help work with OpenXML format for Office documents.

EPPlus, EPPlus-Create advanced Excel spreadsheets on the server - Home[^]
Or
Open XML SDK, Welcome to the Open XML SDK 2.5 for Office[^]
are two components that work well. I personally use EPPlus in several of my project.
 
Share this answer
 
With current approach, you can trick html by adding style tags, it will export to excel with formatting added from code behind .
It's not flexible solution, if your grid has very basic stylesheet you can opt for it.

Please refer Export GridView to Excel in ASP.Net with Formatting using C# and VB.Net[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900