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

5

10

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: A hyperlink from Sheet1 Cell A1 to Sheet2 Cell B10

Sensual answered 26/8, 2009 at 10:34 Comment(0)
C
9

What you want to use here is the Hyperlinks.Add method.

You can call it with code that looks something like this:

Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "Sheet2!A1";

worksheet.Hyperlinks.Add(
    rangeToHoldHyperlink,
    string.Empty,
    hyperlinkTargetAddress,
    "Screen Tip Text",
    "Hyperlink Title");

Here is a full automation example that you can test:

void AutomateExcel()
{
    Excel.Application excelApp = new Excel.Application();
    excelApp.Visible = true;

    Excel.Workbook workbook = excelApp.Workbooks.Add(Type.Missing);
    workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);
    workbook.Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);

    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets[1];
    Excel.Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
    string hyperlinkTargetAddress = "Sheet2!A1";

    worksheet.Hyperlinks.Add(
        rangeToHoldHyperlink,
        string.Empty,
        hyperlinkTargetAddress,
        "Screen Tip Text",
        "Hyperlink Title");

    MessageBox.Show("Ready to clean up?");

   // Cleanup:
    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

    Marshal.FinalReleaseComObject(range);

    Marshal.FinalReleaseComObject(worksheet);

    workbook.Close(false, Type.Missing, Type.Missing);
    Marshal.FinalReleaseComObject(workbook);

    excelApp.Quit();
    Marshal.FinalReleaseComObject(excelApp);
}

Hope this helps!

Mike

Crowning answered 26/8, 2009 at 17:21 Comment(3)
I was using the address parameter instead of the subaddress... the first doesn't work.Rawls
The key point that I was missing was that the cell pointed to by the hyperlink goes in the subaddress. I was attempting to put it in the addressAmboceptor
Thank you, I was able to translate this to Delphi and get it working:Gadolinium
L
2

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

Lawgiver answered 18/4, 2012 at 5:50 Comment(0)
E
0

Hope below one will help you.

xlNewSheet.Hyperlinks.Add(xlWorkRange, string.Empty, "'Detailed Testcase Summary'!A1", "Click Here", "Please click me to go to Detailed Test case Summary Result");    
Extremely answered 21/7, 2015 at 6:3 Comment(0)
M
0

To add a link to a picture (already inserted into the sheet):

Hyperlinks hyperlinks = ws.Hyperlinks;
Hyperlink hyperlink = hyperlinks.Add(picture.ShapeRange.Item(1), "http://stackoverflow.com");

You are not adding it directly to the picture, but the 1st item in it's ShapeRange. (Whatever that is...)

Mittiemittimus answered 5/5, 2016 at 17:9 Comment(0)
G
0

@Mike Rosenblums answer translated to Delphi that also works:

uses
  Excel2000;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  AExcelApplication: ExcelApplication;
  AExcelWorkSheet: ExcelWorkSheet;
  ARange: Range;
  hyperlinkTargetAddress:String;
begin

  AExcelApplication := CoExcelApplication.Create;
  AExcelApplication.Visible[0] := true;
  AExcelApplication.Workbooks.Add('',0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelApplication.Worksheets.Add(EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0);
  AExcelWorkSheet := AExcelApplication.Worksheets.Item[1] as ExcelWorkSheet;
  ARange := AExcelWorkSheet.Range['A1','A1'];

  hyperlinkTargetAddress := 'Sheet2!A1';
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    hyperlinkTargetAddress,
    'Screen Tip Text',
    'Hyperlink Title1'
  );

  ARange := AExcelWorkSheet.Range['A2','A2'];
  AExcelWorkSheet.Hyperlinks.Add(
    ARange,
    '',
    'Sheet1!A1',
    'Screen Tip Text',
    'Hyperlink Title2'
  );

end;
Gadolinium answered 1/6, 2021 at 8:4 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.