Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i am exporting data from asp.net gridview to excel for some data.

In that, one of the cell which contains hyperlink in the excel sheet cell.

when i click the hyperlink in the excel, then i received popup like "Microsoft Excel Security Notice" followed by "cannot download the information you requested".

How to solve this problem.

SQL Query to map with gridview

SQL
IF @Spd_Type='Exportdata' 
Begin 

Create Table #Pkids(ID Int) 
Insert into #Pkids 
SELECT R.i.query('Pkid').value('.', 'VARCHAR(20)') 
FROM @Spd_XML.nodes('/DocumentElement/Test') R(i) 
Select

EmpNo,
Case When EmpNo ='' Then '' Else 'http://sas02.sanet.sam.intranet/D2/servlet/Download?auth=basic&event_name=d2_view&_docbase=uatprjarea&id=09139&version=CURRENT' End PDFEmpNo, 
Empname,
Dept, 
From tbl_Student 
Where A.Pkid in(Select * from #Pkids)


End


ASP.NET
ASP.NET
<asp:GridView ID="grdEmp" AutoGenerateColumns="false" HeaderStyle-BackColor="Yellow"
ShowHeader="false" runat="server">
<Columns>

<asp:TemplateField>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "EmpName")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblPOPDF" runat="server" Text='<%# Eval("EmpNo") %>' Visible="false" />
<asp:HyperLink ID="lnkbtnDownload" NavigateUrl='<%# DataBinder.Eval(Container.DataItem, "POFEmpNo")%>'
Text='<%# Eval("EmpNo") %>' runat="server"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Dept")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


ASP.NET.vb Code
VB.NET
Dim XML As String
XML = Session("XML").ToString

If XML = "<DocumentElement />" Then
Session("XML") = "NULL"
End If

Conn.sqlCmd.Parameters.AddWithValue("@Spd_Type", "ExportData")
Conn.sqlCmd.Parameters.AddWithValue("@Spd_XML", Session("XML").ToString)
ds = Conn.XcuteResult(str, CommandType.StoredProcedure, "SPD_StudentInfo")

grdEmp.DataSource = ds
grdEmp.DataBind()


Finally when i click the hyperlink i getting following error message.

Error : "cannot download the information you requested".

what is the solution for this..
Posted
Updated 14-Sep-16 9:41am
v2
Comments
njammy 14-Sep-16 5:24am    
What happens when you access the url from your browser directly?
gani7787 16-Sep-16 0:58am    
getting Error : "cannot download the information you requested".
njammy 16-Sep-16 2:33am    
Then there is something wrong with the server not your code
Richard Deeming 14-Sep-16 9:00am    
You're not exporting an Excel file. You're exporting an HTML fragment, but sending a header to make the browser pretend that it's an Excel file. Excel then tries to import that HTML fragment into a new worksheet, but you'll have very little control over how that import works.

You'll probably have better luck if you export a real Excel file instead. There are several libraries which let you do that - for example, EPPlus[^], ClosedXML[^], and The OpenXML SDK[^].

NB: Don't try to use Office Interop to generate the file, as it's not supported in ASP.NET applications[^].
gani7787 14-Sep-16 9:29am    
Thanks for your great help.
your suggestions is great. can you pls. give one example to open http link using asp.net hyperlink in excel file. (http://sas02.sanet.sam.intranet/D2/servlet/Download?auth=basic&event_name=d2_view&_docbase=uatprjarea&id=09139&version=CURRENT) --> URL as empno like "1001"

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