New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to insert clickable hyperlink in excel? #926
Comments
Show us how you tried it please and please us the github issue template like automatically provided. |
I am just replacing the access_token with complete URL. Then converting collection object to array as below,
Then creating sheet from array as below,
from this script it actually exports data as required but hyperlink is exported as text format not as clickable link. @patrickbrouwers |
I think you have to do it like this with PHPExcel native methods:
|
Thank you @patrickbrouwers, but setTooltip is not working (empty cell). I am using setValue to show something as a text. Additionally my text is an ID and it was interpreted as a numeric field (with E notation). To fix it I am using:
|
|
I spent far too long trying to figure out why links were not being generated in my Excel file. Turns out that
Which wasn't working, but also wasn't generating any errors, such as
Solved my issue. Might help someone in the future with the same problem. |
solve my issue, this my code
thanks bro @TimothyDLewis |
@TimothyDLewis or @herarya could I trouble you for how you are iterating over the sheet? - maybe just post a bit more code as to the complete method you used? (i.e. getting I am exporting a simple array that has 5 columns from a database, - this is one sheet and it has a header row. Once I grab it, I have this code which works of course: //$trademark = [an array of data with 5 columns and a header row];
Excel::create('trademarks', function($excel) use($trademarks) {
$excel->sheet('trademarks', function($sheet) use($trademarks) {
$sheet->fromArray($trademarks,null,'A1',false,false);
//need to iterate over $sheet, get each row, and find column 1 in that row and set a URL
})->export('xlsx'); I would like to iterate over $sheet, and change one cell in each row (other than the first row) with a URL. I have tried up to a 3 deep foreach loop (i keep getting an error that the result is an object, an array or I get "PHPExcel_Exception Invalid cell coordinate CREATOR in Cell.php (line 590)", and I tried $sheet->each (which apparently is a method available only in loading in a file as it err's that there is no method each). |
setUrl("sheet://'test'!A1") |
when use v3.*, u can like this to set hyper link.
|
Helpful solution, but you can do it more understandable with refactored <?php
namespace App\Exports;
use Excel;
use Maatwebsite\Excel\Sheet;
// add event support
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\AfterSheet;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use PhpOffice\PhpSpreadsheet\Cell\Hyperlink;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
class CommentsExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithEvents
{
// ...
/**
* @return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
/** @var Worksheet $sheet */
foreach ($event->sheet->getColumnIterator('H') as $row) {
foreach ($row->getCellIterator() as $cell) {
if (str_contains($cell->getValue(), '://')) {
$cell->setHyperlink(new Hyperlink($cell->getValue(), 'Read'));
// Upd: Link styling added
$event->sheet->getStyle($cell->getCoordinate())->applyFromArray([
'font' => [
'color' => ['rgb' => '0000FF'],
'underline' => 'single'
]
]);
}
}
}
},
];
}
// ...
} Usage, just: Excel::download(CommentsExport::class); |
You are right. this is a better solution then me. @AlexMcDee |
GOT THIS TODAY after findings.... https://stackoverflow.com/questions/41053636/laravel-excel-export-each-cell-style |
how return error message collection module? |
Just a quick note for anyone coming here from google, Thank you @mouyong @AlexMcDee for the provided solution. While using Without the end column, the iterator will iterate over each column until the last column. I noticed this when I wanted to make a hyperlink for a column in the middle of the sheet not the last column as usual. |
Another approach, that I ended up following, is to use Excel's hyperlink function.
and to style them:
|
Anyone has issues with http links not working in MS Excel's read-only (protected) mode ? This behavior is the same with both Excel's built-in hyperlink and PhpSpreadsheet's |
if you don't use external link my solution
|
After some googling around I finally settled with the following solution: I have created a custom ValueBinder class that can transform arrays to a cell with a hyperlink. I can then activate it in the excel.php by setting the This way I can have different text and hyperlink without the need of costly postprocessing. Example: use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Cell\DefaultValueBinder;
/***
* This custom value binder support link creation in Excel
* You should return an array with the following structure:
* [
* 'text' => 'The text to display',
* 'url' => 'The url to link to',
* ]
* And the cell will be displayed as a link
*/
class CustomValueBinder extends DefaultValueBinder
{
/**
* @param Cell $cell Cell to bind value to
* @param mixed $value Value to bind in cell
* @return bool
*/
public function bindValue(Cell $cell, $value)
{
if (is_array($value)) {
if (isset($value['text']) && isset($value['url'])) {
parent::bindValue($cell, $value['text']);
$cell->getHyperlink()->setUrl($value['url']);
return true;
}
$value = \json_encode($value);
}
return parent::bindValue($cell, $value);
}
} |
I tried inserting hyperlink in sheet, but it is just in text format. How can I insert clickable Hyperlink?
The text was updated successfully, but these errors were encountered: