如何在ASP.NET中从gridview导出时向excel添加超链接? [英] How do I add hyperlink to excel at the time of exporting from gridview in ASP.NET?

查看:164
本文介绍了如何在ASP.NET中从gridview导出时向excel添加超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,每个人

我正在开发Web应用程序。我已经在excel中导出了gridview数据。但是现在我想在export.ie之前添加超链接到excel文件。当我得到excel文件时我应该在excel中有超链接,当用户点击此链接时,目录会自动打开。



我尝试了什么:



 Response.Clear(); 
Response.AddHeader(content-disposition,attachment; filename = ExportData1.xls);
Response.Charset =;
Response.ContentType =application / vnd.xls;
StringWriter StringWriter = new System.IO.StringWriter();
HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);

gvDetails.RenderControl(HtmlTextWriter);

Response.Write(StringWriter.ToString());
Response.End();





导出gridview数据的上述代码为excel。

即gridviews有id,文件名,内容类型和数据类型为列。

我必须导出除数据之外的所有这些字段,并添加超链接以命中excel文件上的文件位置。

excel将看起来像...



 Id FileName FileType DownLoad 
56 Sujata_PDF.pdf application / PDF下载
57 irctcticket.pdf application / PDF下载
46 PBILLJG0050715-16.pdf申请/ PDF下载
47 PBILLJG0051215-16.pdf申请/ PDF下载
48 PBILLJG0051315-16.pdf申请/ PDF下载
49 PNSJG200001715-16.pdf application / PDF下载
50购买请求.pdf申请/ PDF下载
51 SujataMadam.pdf申请/ PDF下载
52 frmMatPDFPreview.pdf application / PDF下载
53 G3 Motors Ltd.pdf application / PDF下载
54 parsan Overseas Pvt Ltd(1).pdf a pplication / PDF下载
55 parsan Overseas Pvt Ltd.pdf application / PDF下载
58 irctcticket2.pdf应用程序/ PDF下载
60 Hacking.pdf application / PDF下载

解决方案

问题是您导出Excel工作表。您正在导出HTML文档,并要求浏览器在Excel中打开它。



Excel将尝试将HTML转换为Excel工作表,但您对结果的控制很少。



相反,您应该使用一个工具来导出真正的Excel工作表。例如:


Hello,Everyone
I am working on web application .I have Exported gridview data in excel .but now i want add hyperlink to excel file before exporting.i.e when i get excel file the i should have hyperlink in excel and when user hit this link then directories open automatically.

What I have tried:

Response.Clear();
           Response.AddHeader("content-disposition", "attachment;filename=ExportData1.xls");
           Response.Charset = "";
           Response.ContentType = "application/vnd.xls";
           StringWriter StringWriter = new System.IO.StringWriter();
           HtmlTextWriter HtmlTextWriter = new HtmlTextWriter(StringWriter);

             gvDetails.RenderControl(HtmlTextWriter);

           Response.Write(StringWriter.ToString());
           Response.End();



the above code for export gridview data to excel.
I.e gridviews have id,filename,contenttype,and data type as columns.
I have to export all these field except data and additionaly add hyperlink to hit the file location on excel file.
the excel will be look like as...

Id	 FileName	          FileType	  DownLoad
56	Sujata_PDF.pdf	        application/PDF	Download
57	irctcticket.pdf	        application/PDF	Download
46	PBILLJG0050715-16.pdf	application/PDF	Download
47	PBILLJG0051215-16.pdf	application/PDF	Download
48	PBILLJG0051315-16.pdf	application/PDF	Download
49	PNSJG200001715-16.pdf	application/PDF	Download
50	purchase request.pdf	application/PDF	Download
51	SujataMadam.pdf	        application/PDF	Download
52	frmMatPDFPreview.pdf	application/PDF	Download
53	G3 Motors Ltd.pdf	application/PDF	Download
54	parsan Overseas Pvt Ltd (1).pdf	application/PDF	Download
55	parsan Overseas Pvt Ltd.pdf	application/PDF	Download
58	irctcticket2.pdf       application/PDF	Download
60	Hacking.pdf	       application/PDF	Download

解决方案

The problem is that you're not exporting an Excel sheet. You're exporting an HTML document, and asking the browser to open it in Excel.

Excel will then try to convert the HTML to an Excel sheet, but you have very little control over the result.

Instead, you should use a tool which will let you export a real Excel sheet. For example:


这篇关于如何在ASP.NET中从gridview导出时向excel添加超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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