在Excel单元格中的文本超链接提取网址 [英] Extracting a URL from hyperlinked text in Excel cell
问题描述
我有一个完整的Excel中的超链接文本的表,所以它基本上是一堆名字,但是当我点击其中一个,它需要我在我的默认浏览器的一些网址。
所以我在我的程序提取这个Excel表中的文本,但我得到当我从这些超链接细胞中提取的值是字符串里面的,当我想要的网址字符串连接到Excel文件
所以我想有两种方法可以做到这一点。要么我可以转换所有的Excel中的超链接的文本文件,以相应的URL,或者我可以使用C#以某种方式提取细胞,而不是文本的URL值。
我不知道该怎么做任何的这些东西,但任何帮助将不胜感激。
C#代码至今:
Excel.ApplicationClass excelApp =新Excel.ApplicationClass();
//excelApp.Visible = TRUE;
Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(C:\\Users\\use\\Desktop\\list.xls
0,假,5,,,假Excel.XlPlatform.xlWindows,,
真,假0,真的,假的,假的);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
串currentSheet =工作表Sheet1
Excel.Worksheet xlws =(Excel.Worksheet)excelSheets.get_Item(currentSheet);
字符串的myString =((Excel.Range)xlws.Cells [2,1])Value.ToString()。
对于Excel文件,它只是一个超链接的名字长排。比如A2单元格将包含文本:
和我想提取的字符串:
http://allrecipes.com//Recipes/desserts/cookies/Main.aspx
您可以使用VBA宏:
按<大骨节病>替代骨节病> + <大骨节病> F11 骨节病>打开VBA编辑器,并粘贴以下:
<预类=朗动prettyprint-覆盖>
功能URL( RG由于范围)作为字符串
超昏暗为超链接
将超级= rg.Hyperlinks.Item(1)
URL = Hyper.Address
端功能
和那么您可以在您的工作表使用它,就像这样:
= URL(B4)
I have a table full of Hyperlinked text in excel, so it's basically a bunch of names but when I click on one, it takes me to some URL in my default browser.
So I am extracting text from this excel table in my program, but the value I get when I extract from these hyperlink cells is that of the string inside, when I want the URL the string is linked to in the excel file.
So I'm thinking there are two ways to do this. Either I can convert all the hyperlinked text in the excel file to the corresponding URLs, or I can use C# to somehow extract the URL value from the cell and not the text.
I don't know how to do either of these things, but any help would be greatly appreciated.
C# code so far:
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
//excelApp.Visible = true;
Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open("C:\\Users\\use\\Desktop\\list.xls",
0, false, 5, "", "",false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1";
Excel.Worksheet xlws = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
string myString = ((Excel.Range)xlws.Cells[2, 1]).Value.ToString();
As for the excel file, it's just one long row of names hyperlinked. For instance cell A2 would contain the text:
And I want to extract the string:
http://allrecipes.com//Recipes/desserts/cookies/Main.aspx
You could use a vba macro:
Hit Alt+F11 to open the VBA editor and paste in the following:
Function URL(rg As Range) As String
Dim Hyper As Hyperlink
Set Hyper = rg.Hyperlinks.Item(1)
URL = Hyper.Address
End Function
And then you can use it in your Worksheet, like this:
=URL(B4)
这篇关于在Excel单元格中的文本超链接提取网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!