Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have created an excel file with multiple sheets containing some data in each sheet.

And created one more sheet to have hyperlink entry for each sheet all these sheets are created programmatically .


For example suppose there are 10 sheets in excel file which are created programmatically there will be one more sheet named hyperlink which will
contain the hyperlink for all other sheets.

I want to add all these hyperlinks programmatically.
Posted
Updated 26-Dec-17 17:36pm
Comments
ZurdoDev 11-Jun-15 14:33pm    
All you have to do is record a macro doing it and then you can see the code you need and can modify if necessary.

Quote:
@BNotr I am using the following code it creates Hyperlink and add it to the given location its giving error "Reference not valid"

//Code Snippet

private void AddHyperLink(Workbook xlWorkBook,int nfaultCount)
{

Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
DateTime now = DateTime.Now;
xlWorkSheet.Name = "Summary";
xlWorkSheet.Cells[3, 2] = "SnapShot Details";
xlWorkSheet.Cells[3,2].EntireRow.Font.Bold = true;
xlWorkSheet.Range[xlWorkSheet.Cells[3, 1], xlWorkSheet.Cells[3, 2]].Merge(); //code to merge cells
xlWorkSheet.Select(Type.Missing);

xlWorkSheet.Cells[6, 1] = "Snapshot taken at";
xlWorkSheet.Cells[6, 2] = now;
xlWorkSheet.Cells[8, 1] = "Click on the Hyperlinks below to navigate to the corresponding sheet";
xlWorkSheet.Range[xlWorkSheet.Cells[8, 1], xlWorkSheet.Cells[8, 6]].Merge(); //code to merge the cells
xlWorkSheet.Cells[8, 1].EntireRow.Font.Bold = true;


for (int i = 1; i <= nfaultCount; i++)
{
int x = i + 9;
string str = "Fault Code "+i + " of " + nfaultCount; //Test link as
string strRange1 = "A" + x;
string strRange2 = "A" + x;
string strRange = strRange1 + ":" + strRange2; //Location of link

var Range = xlWorkSheet.get_Range(strRange);

// strRange1 = "!" + strRange1;

// i+" of "+nfaultCount name of target worksheet
string strp="#" + i + " of " +nfaultCount + strRange1;

Range.Cells.Hyperlinks.Add(Range,strp, Type.Missing, "Fault Code Link", str);
}

xlWorkSheet.Columns["A:B"].AutoFit();



}


Kindly help me out
 
Share this answer
 
Comments
Outeriesself 25-Aug-15 6:19am    
Thakur try this:

ExcelFile workbook = ExcelFile.Load("Sample.xlsx");
ExcelWorksheet hyperlinkSheet = workbook.Worksheets.Add("Hyperlinks");

for (int i = 0; i < workbook.Worksheets.Count - 1; i++)
{
string sheetName = workbook.Worksheets[i].Name;

ExcelCell hyperlinkCell = hyperlinkSheet.Cells[i, 0];
hyperlinkCell.Value = "Go to " + sheetName;

hyperlinkCell.Style.Font.UnderlineStyle = UnderlineStyle.Single;
hyperlinkCell.Style.Font.Color = SpreadsheetColor.FromName(ColorName.Blue);
hyperlinkCell.Hyperlink.Location = sheetName + "!A1";
}

workbook.Save("Sample Out.xlsx");

The code uses this excel processing library for .net.
It'll enable you to read both xls and xlsx files with C#, iterate through excel file's worksheets and create a new worksheet with a hyperlinks pointing to each worksheet.
Thanks Jai, but At the last line of for loop you need to add sheetName in single quote then it will work fine.

hyperlinkCell.Hyperlink.Location = "'" + sheetName.ToString()  + "'" + "!A1";


-Shakti Pratap Pandey
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900