Solved

Grid view - Export to Excel without hyperlink

Posted on 2007-04-09
6
2,644 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Accepted Solution

by:
viveksg earned 500 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
0
 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now