Adding hyperlinks in Excel[2007] in C# - Within Excel it self

前端 未结 4 2133
醉梦人生
醉梦人生 2021-02-08 11:00

Can anybody tell me how we can add a hyperlink in Excel (2007 or later) from a cell in one sheet to a cell in another sheet using Office Interop in .NET (c#)

For example

4条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-08 11:40

    I do it so:

            Excel.Application xlApp;
            Excel.Workbook xlWorkBook;
            Excel.Worksheet xlWorkSheet;
            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Add(System.Reflection.Missing.Value);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 
            Excel.Hyperlink link =
                (Excel.Hyperlink)
                xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("L500", Type.Missing), "#Sheet1!B1", Type.Missing,
                                           "Go top",
                                           "UP");
    
            xlWorkSheet.Hyperlinks.Add(xlWorkSheet.get_Range("C5", Type.Missing), "www.google.com", Type.Missing, "Click me to go to Google ","Google.com");                                                     
            xlApp.Visible = true;
    

    It is important to insert the symbol # in the link that leads to a cell inside the book, if this symbol is not inserted, then the link is broken.

    I described this decision in an article in Russian language, an example can be found here

提交回复
热议问题