Adding hyperlink , bookmarks to Excel File - Excel Automation

Posted by SurajRane under C# category on | Views : 8097
This snippet create bookmark in Book1.xls file for cell U100.

user can jump from A12 to U100 by clicking on link "Microsoft" in A12 cell. :)

first Add reference of "Microsoft.Office.Interop.Excel.dll".

Microsoft.Office.Interop.Excel.Application excel = new ApplicationClass();

//open excel file
Microsoft.Office.Interop.Excel.Workbook workbk = excel.Workbooks.Open(@"D:\ExcelAutomation\Book1.xls",
0, false, 5, System.Reflection.Missing.Value, System.Reflection.Missing.Value, false,
System.Reflection.Missing.Value, System.Reflection.Missing.Value, true, false,
System.Reflection.Missing.Value, false, false, false);

Microsoft.Office.Interop.Excel.Sheets xlsheets = workbk.Sheets; //Get the sheets from workbook
Microsoft.Office.Interop.Excel.Worksheet excelWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)xlsheets[1]; //Select the first sheet

//select cell cell
Microsoft.Office.Interop.Excel.Range excelCell = (Microsoft.Office.Interop.Excel.Range)excelWorksheet.get_Range("A12:A12", Type.Missing);

//set bookmark here
excelCell.Cells.Hyperlinks.Add(excelCell, "#Sheet1!U100", Type.Missing, "Microsoft", "Microsoft");

workbk.Save(); // save

excel.Quit();// close. dont forget this

Comments or Responses

Login to post response