![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1475391/image.png)
How to force conversion of text in Excel cell to a hyperlink?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
답변 (1개)
Shivang
2023년 9월 7일
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1475391/image.png)
Hope this helps.
-Shivang
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!