我尝试在工作表中插入超链接,但它只是文本格式。 如何插入可点击的超链接?
请向我们展示您是如何尝试的,并请我们自动提供 github 问题模板。
`$candidate; //Collection object from model
foreach ($candidates as $key => $model) {
if($model->grade == 'Z') {
$model->grade="Reject";
}
$model->access_token=url('static-access/'.$model->access_token);`
我只是用完整的 URL 替换 access_token。 然后将集合对象转换为数组,如下所示,
`foreach ($candidates as $candidate) {
$candidatesArray[] = $candidate->toArray();
}`
然后从数组创建工作表,如下所示,
`$sheet=$excel->sheet('sheet1', function($sheet) use ($candidatesArray,$jd) {
$sheet->fromArray($candidatesArray, null, 'A1', false, false);
});`
从这个脚本它实际上根据需要导出数据,但超链接导出为文本格式而不是可点击链接。 @patrickbrouwers
我认为您必须使用 PHPExcel 本机方法这样做:
$sheet->getCell('E26')
->getHyperlink()
->setUrl('http://examle.com/uploads/cv/' . $cellValue)
->setTooltip('Click here to access file');
https://github.com/PHPOffice/PHPExcel/blob/develop/Documentation/markdown/Overview/08-Recipes.md#change -a-cell-into-a-clickable-url
谢谢@patrickbrouwers ,但 setTooltip 不起作用(空单元格)。
至少对于版本"maatwebsite/excel": "~2.1.0"
。
我正在使用 setValue 将某些内容显示为文本。
此外,我的文本是一个 ID,它被解释为一个数字字段(使用 E 表示法)。 要修复它,我正在使用:
->setValueExplicit($fbId, PHPExcel_Cell_DataType::TYPE_STRING)
setTooltip
是将鼠标悬停在单元格上时显示的文本。 在使用它之前,您确实需要执行setValue
或setValueExplicit
。
我花了太长时间试图弄清楚为什么我的 Excel 文件中没有生成链接。 原来$sheet->cell("A1")
和$sheet->getCell("A1")
是不一样的。 我有很多情况下我正在做类似以下的事情:
$sheet->cell($cellLetter.$rowIndex, $url)
->getHyperlink()
->setUrl($url);
这不起作用,但也没有产生任何错误,例如Method [...] doesn't exist for ...
。 将我的代码更改为:
$sheet->getCell($cellLetter.$rowIndex) // A1, B2
->setValueExplicit("Click Here", \PHPExcel_Cell_DataType::TYPE_STRING);
$sheet->getStyle($cellLetter.$rowIndex)
->applyFromArray($urlStyle); // Blue, Bold
$sheet->getCell($cellLetter.$rowIndex)
->getHyperlink()
->setUrl($url);
解决了我的问题。 将来可能会帮助遇到同样问题的人。
解决我的问题,这是我的代码
$sheet->getCell($cell)
->getHyperlink()
->setUrl($url);
$sheet->getStyle($cell)
->applyFromArray(array(
'font' => array(
'color' => ['rgb' => '0000FF'],
'underline' => 'single'
)
));
谢谢兄弟@TimothyDLewis
@TimothyDLewis或@herarya我可以麻烦您如何遍历工作表吗? - 也许只是发布更多关于您使用的完整方法的代码? (即得到$cellLetter.$rowIndex
或$cell
?)
我正在从数据库中导出一个包含 5 列的简单数组 - 这是一张纸,它有一个标题行。 一旦我抓住它,我就有了这个当然可以工作的代码:
//$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');
我想遍历 $sheet,并使用 URL 更改每行(第一行除外)中的一个单元格。 我已经尝试了多达 3 个深度的 foreach 循环(我一直收到一个错误,即结果是一个对象、一个数组,或者我得到“PHPExcel_Exception Invalid cell coordinate CREATOR in Cell.php(第 590 行)”,我尝试了 $sheet- >each (这显然是一种仅在加载文件时可用的方法,因为它错误的是没有方法 each)。
setUrl("sheet://'test'!A1")
但文件链接是sheet://test!A1
为什么?
使用 v3.* 时,您可以像这样设置超链接。
<?php
namespace App\Exports;
use Excel;
use Maatwebsite\Excel\Sheet;
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
{
// ...
public function custom()
{
Excel::extend(static::class, function (CommentsExport $export, Sheet $sheet) {
/** <strong i="6">@var</strong> Worksheet $sheet */
foreach ($sheet->getColumnIterator('H') as $row) {
foreach ($row->getCellIterator() as $cell) {
if (str_contains($cell->getValue(), '://')) {
$cell->setHyperlink(new Hyperlink($cell->getValue(), '点击查看图片'));
}
}
}
}, AfterSheet::class);
}
// ...
}
(new CommentsExport)->custom());
Excel::download(CommentsExport::class);
使用 v3.* 时,您可以像这样设置超链接。
<?php namespace App\Exports; use Excel; use Maatwebsite\Excel\Sheet; 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 { // ... public function custom() { Excel::extend(static::class, function (CommentsExport $export, Sheet $sheet) { /** <strong i="7">@var</strong> Worksheet $sheet */ foreach ($sheet->getColumnIterator('H') as $row) { foreach ($row->getCellIterator() as $cell) { if (str_contains($cell->getValue(), '://')) { $cell->setHyperlink(new Hyperlink($cell->getValue(), '点击查看图片')); } } } }, AfterSheet::class); } // ... }
(new CommentsExport)->custom()); Excel::download(CommentsExport::class);
有用的解决方案,但您可以通过重构使其更易于理解
<?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
{
// ...
/**
* <strong i="13">@return</strong> array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
/** <strong i="14">@var</strong> 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'
]
]);
}
}
}
},
];
}
// ...
}
用法,只是:
Excel::download(CommentsExport::class);
你说的对。 这是一个比我更好的解决方案。 @AlexMcDee
@TimothyDLewis或@herarya我可以麻烦您如何遍历工作表吗? - 也许只是发布更多关于您使用的完整方法的代码? (即得到
$cellLetter.$rowIndex
或$cell
?)我正在从数据库中导出一个包含 5 列的简单数组 - 这是一张纸,它有一个标题行。 一旦我抓住它,我就有了这个当然可以工作的代码:
//$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');
我想遍历 $sheet,并使用 URL 更改每行(第一行除外)中的一个单元格。 我已经尝试了多达 3 个深度的 foreach 循环(我一直收到一个错误,即结果是一个对象、一个数组,或者我得到“PHPExcel_Exception Invalid cell coordinate CREATOR in Cell.php(第 590 行)”,我尝试了 $sheet- >each (这显然是一种仅在加载文件时可用的方法,因为它错误的是没有方法 each)。
在发现后今天得到了这个......
https://stackoverflow.com/questions/41053636/laravel-excel-export-each-cell-style
收集模块如何返回错误信息?
对于从谷歌来到这里的任何人来说,只是一个简短的说明,感谢@mouyong @AlexMcDee提供的解决方案。
使用$sheet->getColumnIterator('H')
时,指定第二个参数很重要,即结束列$sheet->getColumnIterator('H', 'H')
。
如果没有结束列,迭代器将遍历每一列,直到最后一列。
当我想为工作表中间的一列而不是像往常一样的最后一列创建超链接时,我注意到了这一点。
我最终遵循的另一种方法是使用 Excel 的超链接功能。
例如,我想创建一个从 sheet2 到 sheet1(范围 A1:A20)上具有相同文本的单元格的超链接:
在我的工作表2
public function array(): array
{
$data = [
['Id'],
];
foreach ($this->data as $id) {
$idformula = '=HYPERLINK("#"&CELL("address", INDEX(Sheet1!A1:A20, MATCH("'.$id.'",Sheet1!A1:A20,0), 1)),"'.$id.'")';
array_push($data, [$idformula]);
}
return $data;
}
并设置它们的样式:
public function styles(Worksheet $sheet)
{
return [
'A' => ['font' => [
'color' => ['rgb' => '0000FF'],
'underline' => 'single',
]],
];
}
最有用的评论
有用的解决方案,但您可以通过重构使其更易于理解
用法,只是: