从Excel单元格中的超链接文本中提取URL [英] Extracting a URL from hyperlinked text in Excel cell
问题描述
$ b $所以我在我的程序中从这个excel表中提取文本,但是当我从这些超链接单元格中提取时,我获得的值是字符串里面的值,当我想要在excel文件中链接字符串的URL 。
所以我想有两种方法可以做到这一点。我可以将excel文件中的所有超链接文本转换为相应的URL,或者我可以使用C#以某种方式从单元格而不是文本中提取URL值。
我不知道如何做这些任何事情,但任何帮助将不胜感激。
C#代码到目前为止:
Excel.ApplicationClass excelApp = new Excel.ApplicationClass();
//excelApp.Visible = true;
Excel.Workbook excelWorkbook =
excelApp.Workbooks.Open(C:\\ersers\\\\\Desktop\\list,
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();
对于excel文件,只是一长串名称超链接。例如,单元格A2将包含文本:
我想提取字符串:
http://allrecipes.com//Recipes/desserts/cookies/Main.aspx
您可以使用vba宏:
Hit Alt + F11 打开VBA编辑器并粘贴以下内容:
功能URL rg As Range)As String
Dim Hyper As Hyperlink
设置Hyper = 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单元格中的超链接文本中提取URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!