Click here to Skip to main content
15,886,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In my scenario there is "Product Name" column which I have to export in excel along with the hyper link. The hyper link contains url of that product. There are 4000 products and each product will contain it's individual url. Again there is also possibility that for some product we don't have url. For example:
Product 1
Product 2
Product 3
Product 4
Product 5

I am using following code to export the data into excel. But I don't know how to add individual hyper link(anchor link) to such huge data.
Please provide some solution.
C#
MyExcel.Application excelApp = new MyExcel.Application();
excelApp.Workbooks.Add();
 
MyExcel._Worksheet workSheet = excelApp.ActiveSheet;
 
for (int i = 0; i < Tbl.Columns.Count; i++)
{
workSheet.Cells[1, (i + 1)] = Tbl.Columns[i].ColumnName;
}
 
for (int i = 0; i < Tbl.Rows.Count; i++)
{
for (int j = 0; j < Tbl.Columns.Count; j++)
{
workSheet.Cells[(i + 2), (j + 1)] = Tbl.Rows[i][j];
}
}
 
if (ExcelFilePath != null && ExcelFilePath != "")
{
try
{
workSheet.SaveAs(ExcelFilePath);
excelApp.Quit();
MessageBox.Show("Data is exported to Excel successfully!", "Successfull", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("ExportToExcel: Excel file could not be saved! Check filepath.\n" + ex.Message);
}
}
else
{
excelApp.Visible = true;
}
Posted
Updated 17-Nov-14 4:59am
v2
Comments
Richard MacCutchan 17-Nov-14 9:20am    
You need to separate the URL from the product name and add it to the worksheet in the relevant column.
Member 7695634 17-Nov-14 9:35am    
Thanks for the reply Richard. But above shown will be the output in the excel. I already have two separate columns for product name and url. The question is how to attach this url to the product name as there might be 4000 different urls.
BillWoodruff 17-Nov-14 9:41am    
If the each row of your Table has a product name field, and a url field (which can be null/empty), and each row in your Excel sheet has a column for product name and a column for url: what's the problem ?
Member 7695634 17-Nov-14 10:03am    
Hi Bill, I want to export the product name column from c# datatable to excel by attaching url to it so that when user click on the product name it will redirect him/her to the product page. I want to add hyper link column to excel sheet using C#.

1 solution

You're using Interop, so... Hyperlinks.Add method [^] is what you're looking for.
 
Share this answer
 
Comments
Member 7695634 17-Nov-14 11:52am    
Hey Maciej, I have gone through this link but in this approach I have to use get_Range method to specify the range and as I stated there are 4000 different kind of products having 4000 different urls. So how it will be possible to set range for each individual cell?
Maciej Los 17-Nov-14 12:06pm    
Range rng = Worksheets.Cells(r,c);

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