Extracting a URL from hyperlinked text in Excel cell

前端 未结 8 2332
孤街浪徒
孤街浪徒 2020-12-17 23:43

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

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 23:53

    Use Visual Studio Tools for Office (VSTO) to open Excel workbook and extract all hyperlinks.


    I put a hyperlink into A1 of Sheet1 in Book1.xlsx: text = "example.com, address = "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();
    
        foreach (var h in hyperlinks)
        {
            Console.WriteLine("text: {0}, address: {1}", h.TextToDisplay, h.Address);
        }
    }
    finally
    {
        if (app != null)
            app.Quit();
    }
    

    Output:

    text: example.com, address: http://www.example.com/
    

提交回复
热议问题