?
Solved

Grid view - Export to Excel without hyperlink

Posted on 2007-04-09
6
Medium Priority
?
2,759 Views
Last Modified: 2012-08-14
We are working on ASP.Net 2.0 and VB.Net.
We have gridview whichis bounded to the dataset. The first column of the grid view is hyperlinked. We would like to export the grid into excel. When we do the same, we getting the hyperlink in the excel as well. We would like to remove the hyperlink when exported to excel. Please let me know how to do this? Also any code sample will be of great help
0
Comment
Question by:harsha74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 3

Expert Comment

by:viveksg
ID: 18880410
You can use regular expressions just before exporting to eliminate hyperlinks.
I think you are using stringwriter for exporting. Try the below code.

Regex.Replace(oStringWriter.ToString(),@"(<a[^>]*>)|(</a>)"," ",RegexOptions.IgnoreCase)

If you still have any doubt, pls post that.
0
 

Author Comment

by:harsha74
ID: 18880724
We are still getting the hyperlink. Pl see the code we are using. Pl let me know what's the problem in the code.
Protected Sub ExcelImage_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ExcelImage.Click
       
        'Variable Declaration

        Dim sw As StringWriter = New StringWriter()
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
       
        Try
            Response.Clear()
            Response.ContentType = "application/vnd.excel"
            Response.Charset = ""
            Response.AddHeader("content-disposition", "attachment; FileName=MyFile.xls")
            'To bind the dataset to the datagrid, to export the same to excel

            grdSummaryReport.RenderControl(htw)
            Regex.Replace(sw.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase)
            Response.Write(sw.ToString())
            Response.End()
        Catch ex As Exception
            lblErrorMsg.Text = ex.Message
        Finally
                  End Try
    End Sub
0
 
LVL 3

Expert Comment

by:viveksg
ID: 18880801
Instead of
Regex.Replace(sw.ToString(), "(<a[^>]*>)|(</a>)", " ",  
                         RegexOptions.IgnoreCase)
Response.Write(sw.ToString())

use

Response.Write(Regex.Replace(sw.ToString(),@"(<a[^>]*>)|
                           (</a>)"," ",RegexOptions.IgnoreCase))
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
viveksg earned 2000 total points
ID: 18880805
or full code

Protected Sub ExcelImage_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ExcelImage.Click
       
        'Variable Declaration

        Dim sw As StringWriter = New StringWriter()
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
       
        Try
            Response.Clear()
            Response.ContentType = "application/vnd.excel"
            Response.Charset = ""
            Response.AddHeader("content-disposition", "attachment; FileName=MyFile.xls")
            'To bind the dataset to the datagrid, to export the same to excel

            grdSummaryReport.RenderControl(htw)
         
            Response.Write(Regex.Replace(sw.ToString(), "(<a[^>]*>)|(</a>)", " ", RegexOptions.IgnoreCase))

            Response.End()
        Catch ex As Exception
            lblErrorMsg.Text = ex.Message
        Finally
                  End Try
    End Sub
1
 

Expert Comment

by:babalooey
ID: 21156473
Works great - just what I was looking for.  Be sure to include the regular expressions namespace on the page. C#: using System.Text.RegularExpressions;
0
 
LVL 1

Expert Comment

by:icastel080801
ID: 21674438
Excellent solution.  Simple and functional.  Thank you.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question