Solved

Grid view - Export to Excel without hyperlink

Posted on 2007-04-09
6
2,656 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

785 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