我在excel中有一个满是超链接文本的表格,所以它基本上是一堆名字,但当我点击其中一个时,它会把我带到我默认浏览器中的一些URL。
所以我在我的程序中从这个excel表中提取文本,但是当我从这些超链接单元格中提取时,我得到的值是里面的字符串的值,当我想要该字符串链接到excel文件中的URL时。
所以我认为有两种方法可以做到这一点。我可以将excel文件中的所有超链接文本转换为相应的URL,也可以使用C#以某种方式从单元格而不是文本中提取URL值。
我不知道如何做这两件事,但任何帮助都会非常感谢。
到目前为止的C#代码:
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();至于excel文件,它只是一长排超链接的名称。例如,单元格A2将包含以下文本:
Yummy cookie recipe
我想提取字符串:
http://allrecipes.com//Recipes/desserts/cookies/Main.aspx发布于 2011-04-27 06:11:09
在您的代码中只需添加
string myString = ((Excel.Range)xlws.Cells[2, 1]).Cells.Hyperlinks[1].Address;显然,我建议在访问“超级链接”属性之前做一些检查。
发布于 2011-04-13 16:49:46
使用Visual Studio Tools for Office (VSTO)打开Excel工作簿并提取所有超链接。
我在Book1.xlsx: A1 = "example.com,Sheet1 = "http://www.example.com“中的文本中放置了一个超链接。
_Application app = null;
try
{
app = new Application();
string path = @"c:\temp\Book1.xlsx";
var workbook = app.Workbooks.Open(path, 0, true, 5, "", "", true, XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
var sheets = workbook.Worksheets;
var sheet = (Worksheet)sheets.get_Item("Sheet1");
var range = sheet.get_Range("A1", "A1");
var hyperlinks = range.Cells.Hyperlinks.OfType<Hyperlink>();
foreach (var h in hyperlinks)
{
Console.WriteLine("text: {0}, address: {1}", h.TextToDisplay, h.Address);
}
}
finally
{
if (app != null)
app.Quit();
}输出:
text: example.com, address: http://www.example.com/发布于 2011-04-13 16:48:33
为什么不使用Uri类将字符串转换为URL:
Uri uri = new Uri("http://myUrl/test.html");https://stackoverflow.com/questions/5646549
复制相似问题