如何使用默认的excel行格式将数据从gridview导出到excel。 [英] How to export data from gridview to excel with default excel row fomatting.

查看:80
本文介绍了如何使用默认的excel行格式将数据从gridview导出到excel。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用export to excel from gridview.when点击下载按钮,excel表打开但格式化(如黑色边框)。



如何删除黑色边框..我只想使用excel的默认单元格样式。



请帮助..



我尝试过:



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:

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) {
}

推荐答案

您正在做的是不导出到Excel。您正在导出为带有xls扩展名的HTML。



我首先要使用一个可以帮助处理Office文档的OpenXML格式的组件。



EPPlus, EPPlus - 在服务器上创建高级Excel电子表格 - 主页 [ ^ ]



Open XML SDK,欢迎使用Open XML SDK 2.5 for Office [ ^ ]

是两个运行良好的组件。我个人在我的几个项目中使用EPPlus。
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.


使用当前的方法,你可以通过添加样式标签来欺骗html,它将导出到excel,并从后面的代码中添加格式。

这不是灵活的解决方案,如果您的网格有非常基本的样式表,您可以选择它。



请参考将GridView导出到ASP.Net中,使用C#和VB.Net进行格式化 [ ^ ]
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[^]


这篇关于如何使用默认的excel行格式将数据从gridview导出到excel。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆