简体   繁体   中英

Reference not valid error in excel hyperlink

I am using the below piece of code to add hyperlink to a given cell( "A1" here):

Workbook workbook = _excelApp.Workbooks.Open("C:\\temp\\test1.xlsx",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);


Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
Range rangeToHoldHyperlink = worksheet.get_Range("A1", Type.Missing);
string hyperlinkTargetAddress = "www.bing.com";

Range excelRange = worksheet.UsedRange;
object[,] valueArray = (object[,])excelRange.get_Value(
                XlRangeValueDataType.xlRangeValueDefault);


 worksheet.Hyperlinks.Add(
                rangeToHoldHyperlink,
                hyperlinkTargetAddress,
                string.Empty,
                string.Empty,
                valueArray[1, 1].ToString());

It adds the hyperlink. But, on clicking it, it says:
Reference not valid
On hovering the mouse over cell text, it displays the hyperlink as:
excel_file_path\\hyperlinkTargetAddress

Why it is doing so?
How this can be overcome?

You can add different types of hyperlinks. By default, Excel assumes that you want to link a file in your hard drive (in the folder where the spreadsheet it). To tell Excel that you want a website, you have to write the full address (with the starting http:// bit).

string hyperlinkTargetAddress = "http://www.bing.com"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM