Clear Filters
Clear Filters

How to force conversion of text in Excel cell to a hyperlink?

10 views (last 30 days)
I have a Matlab script that generates an Excel sheet. One column of the table or sheet has links to files, for example this text might be in a cell:
"file:\\T:/MyFolder/Myfile.pdf" (The goal is to give the user a hyperlink to the PDF file on the T drive...)
I can generate the Excel file with the text filled in properly, but Excel does not recognize (or format) the cell as a hyperlink until the user clicks inside the cell, does nothing, then hits <return>, then goes back outside the cell and clicks on it again. That is, the text is the right value, but Excel doesn't think it is a hyperlink. It would be nicer for the user to just click once on the cell. Is there a way to get Excel to recognize the whole column as hyperlinks? (I've tried adding a newline at the end of the text string, that did not work).
Thanks for any suggestions.
John

Answers (1)

Shivang
Shivang on 7 Sep 2023
Hi John,
I understand you're running into an issue while writing hyperlinks to an Excel sheet using a MATLAB script.
You can use the 'HyperLinks.Add' method to add hyperlinks to an Excel sheet, after creating an Excel object using 'actxserver'. Excel will then automatically recognize the added cell data as a hyperlink.
This is a sample code that writes three links present in a MATLAB table to the cells A1:A3 in an Excel sheet:
T = table({'file:\\T:/MyFolder/Myfile.pdf'; 'https://mathworks.com'; 'https://example.com'});
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
exlFile = exlWkbk.Open([pwd, '\outputfile.xlsx']); %outputfile.xlsx must be present in current working directory
exlSheet1 = exlFile.Sheets.Item('Sheet1');
for i=1:size(T,1)
rngObj = exlSheet1.Range('A'+string(i));
exlSheet1.HyperLinks.Add(rngObj, string(T{i,1}));
end
exlFile.Save();
exlFile.Close();
exl.Quit;
exl.delete;
In the Excel file, the data in cells A1:A3 is automatically formatted as a hyperlink:
Hope this helps.
-Shivang

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!