473,654 Members | 3,109 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strip Hyperlinks Out Of An HTML String For Export to Excel ASP.NET

Hi

I have been trying to find an easy way to strip the hyperlinks out of
an html string to make exporting ASP.NET GridView's to Excel a bit more
user friendly. Couldn't find anything in these groups, so I have
written this code which will work fine as long as you don't use the ">"
character in your hyperlink name.

Hope somebody finds it useful.

Jared
public static string fnStripHyperLin kFromHTML(strin g strHTML)
{
string strHTMLFinal = "";

int intPlaceHolderS tart = 0;
int intPlaceHolderE nd = strHTML.IndexOf ("<a href");

strHTMLFinal += strHTML.Substri ng(0, intPlaceHolderE nd);

intPlaceHolderS tart = intPlaceHolderE nd;

while (intPlaceHolder End > -1)
{
intPlaceHolderS tart = strHTML.IndexOf (">",
intPlaceHolderS tart) + 1;
intPlaceHolderE nd = strHTML.IndexOf ("</a>",
intPlaceHolderS tart);
strHTMLFinal += strHTML.Substri ng(intPlaceHold erStart,
(intPlaceHolder End - intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd + 4;
intPlaceHolderE nd = strHTML.IndexOf ("<a href",
intPlaceHolderS tart);
if (intPlaceHolder End > -1)
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart, (intPlaceHolder End -
intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd;
}
else
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart);
}

}
return strHTMLFinal;
}

Jun 7 '06 #1
2 4506
"Jared" <ja***********@ gmail.com> wrote in message
news:11******** **************@ f6g2000cwb.goog legroups.com...
Hi

I have been trying to find an easy way to strip the hyperlinks out of
an html string to make exporting ASP.NET GridView's to Excel a bit more
user friendly. Couldn't find anything in these groups, so I have
written this code which will work fine as long as you don't use the ">"
character in your hyperlink name.
And no nested anchor tags, and all lowercase, and no one put another
attribute ahead of href... You should use a DOM parser. With the Microsoft
one, you could just find each anchor tag and set outerHTML=inner HTML thereby
removing the anchor.

Hope somebody finds it useful.

Jared
public static string fnStripHyperLin kFromHTML(strin g strHTML)
{
string strHTMLFinal = "";

int intPlaceHolderS tart = 0;
int intPlaceHolderE nd = strHTML.IndexOf ("<a href");

strHTMLFinal += strHTML.Substri ng(0, intPlaceHolderE nd);

intPlaceHolderS tart = intPlaceHolderE nd;

while (intPlaceHolder End > -1)
{
intPlaceHolderS tart = strHTML.IndexOf (">",
intPlaceHolderS tart) + 1;
intPlaceHolderE nd = strHTML.IndexOf ("</a>",
intPlaceHolderS tart);
strHTMLFinal += strHTML.Substri ng(intPlaceHold erStart,
(intPlaceHolder End - intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd + 4;
intPlaceHolderE nd = strHTML.IndexOf ("<a href",
intPlaceHolderS tart);
if (intPlaceHolder End > -1)
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart, (intPlaceHolder End -
intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd;
}
else
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart);
}

}
return strHTMLFinal;
}

Jun 7 '06 #2
using System.Text.Reg ularExpressions ;
//replace any and all of script / body /embed / object / frameset / frame /
iframe / meta / link /style with "")
strContent = Regex.Replace(s trContent,
@"</?(?i:script|bod y|embed|object| frameset|frame| iframe|meta|lin k|style)(.|\n)* ?>", "");
Just use the "a" tag and that should do it, e.g.,

strContent = Regex.Replace(s trContent, @"</?(?i:a)(.|\n)*? >", "");
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Jared" wrote:
Hi

I have been trying to find an easy way to strip the hyperlinks out of
an html string to make exporting ASP.NET GridView's to Excel a bit more
user friendly. Couldn't find anything in these groups, so I have
written this code which will work fine as long as you don't use the ">"
character in your hyperlink name.

Hope somebody finds it useful.

Jared
public static string fnStripHyperLin kFromHTML(strin g strHTML)
{
string strHTMLFinal = "";

int intPlaceHolderS tart = 0;
int intPlaceHolderE nd = strHTML.IndexOf ("<a href");

strHTMLFinal += strHTML.Substri ng(0, intPlaceHolderE nd);

intPlaceHolderS tart = intPlaceHolderE nd;

while (intPlaceHolder End > -1)
{
intPlaceHolderS tart = strHTML.IndexOf (">",
intPlaceHolderS tart) + 1;
intPlaceHolderE nd = strHTML.IndexOf ("</a>",
intPlaceHolderS tart);
strHTMLFinal += strHTML.Substri ng(intPlaceHold erStart,
(intPlaceHolder End - intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd + 4;
intPlaceHolderE nd = strHTML.IndexOf ("<a href",
intPlaceHolderS tart);
if (intPlaceHolder End > -1)
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart, (intPlaceHolder End -
intPlaceHolderS tart));
intPlaceHolderS tart = intPlaceHolderE nd;
}
else
{
strHTMLFinal +=
strHTML.Substri ng(intPlaceHold erStart);
}

}
return strHTMLFinal;
}

Jun 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5769
by: A P | last post by:
Is this possible? I am planning to export data from SQL that generates HTML table. Instead of selecting the table and then copy then paste to word, I need a script(JS or VB) that will automatically export or open the Query to word.
9
8890
by: Julie Miles | last post by:
I need to pull several tables of data from Excel into a web page, but when I use Excel's "Save as web page" function, I get an enormous file containing a massive amount of css formatting. I'd like to strip out this css so that I can apply my own style sheet; does anyone know whether there is a way to do this, other than manually? Either a way to export the data from Excel that doesn't include the css formatting, or a utility that will...
2
10020
by: bill | last post by:
Hi All, When I send html content with Excel MIME type, the page break style sheet {page-break-before: always} doesn't work at Excel spreadsheet. Any idea to convert html page break style sheet to Excel page break? Thanks in advance
9
2668
by: middletree | last post by:
I have a page which takes data in an HTML table, and exports it to an Excel file. It works fine, but I want several things about the spreadsheet to look different from the HTML version. Things like fonts and colors. I think I can use a style sheet for the latter, but I haven't figured out how to remove hyperlinks from some of the items. On the HTML page, the hyperlinks are useful, but I don't want the spreadsheet to have them. So on my...
6
2156
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem shows up on both my development machine (Windows XP Pro SP 1, .Net Framework 1.1) and on our production server (Windows 2K SP 4, .Net Framework 1.1). I have simplified the code and data to isolate the problem. When I use the xsl:strip-space (Line 12) declaration in conjunction with the xsl:sort...
3
3174
by: Joe | last post by:
I have a table with hundreds of hyperlinks in one field (defined as a hyperlink field) and a short name for each link in another field. I can successfully export a report to HTML with the short name followed by the hyperlink. What I would like to do is to combine the two so only the short name is displayed. In other words, I would like the output HTML to read: <a href="http://www.webpage.com">ShortName</a>
3
2417
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >. But if someonew has already written something similar for .NET, I would prefer more clever solution, which would try to retain original layout, at least paragraphs, hyperlinks etc - something like Outlook does when changing HTML to plain text.
5
4168
by: =?Utf-8?B?c2NobWlkdGU=?= | last post by:
Hi How can I Export an HTML Table to excel? My goal is a button, and when the user clicks this button a popup appears asking the user to 'open' or 'save' the generated Excel file. Actually I'm exporting by rendering my table to an htmlwriter and sending this to excel. This is working nearly perfect. There's a problem with Excel's auto-formatting of cells. Excel is changing the content of my cells. '1.23' is getting 'Jan 23',
12
5580
by: Frustratee | last post by:
Sorry guys, this is killing me. I have been fighting this issue for several weeks, to no avail. I am exporting the results of a query to an excel sheet, with one of the columns being from a field of hyperlinks that link to files on the shared drive. I CANNOT get the hyperlinks to be assigned. I can change the field name, but not the hyperlink address. Can't figure it out. 1) I have a header row 2) Column 16 is where the hyperlinks are...
0
8372
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8706
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8591
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4293
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2709
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.