【问题标题】:How to rename file before ->download() with Laravel Excel如何使用 Laravel Excel 在 ->download() 之前重命名文件
【发布时间】:2018-09-26 20:26:31
【问题描述】:

在 Laravel Excel 的文档中找不到如何在下载文件之前为加载的文件指定一个新名称。我试过 ->setTitle 但它不起作用。

Excel::load(public_path().'/bills/bill.template.xlsx', function($doc) {

            $doc->setTitle = 'test';
            $sheet = $doc->setActiveSheetIndex(0);

            $sheet->setCellValue('G21', '{buyer}');
            $sheet->setCellValue('AB24', '{sum}');
            $sheet->setCellValue('B30', '{sum_propis}');


        })->download('xlsx');

当我等待“test.xlsx”时,它给了我“bill.template.xlsx”

【问题讨论】:

  • 我不知道这个特定的库,但是下载的文件名通常由响应中发送的 HTTP 标头控制 - 也许您可以查看库是否这样做,或者自己添加标头为此目的的响应。
  • setTitle() 设置工作簿中工作表的“标题”
  • @MarkBaker 我知道,这只是一个尝试

标签: php excel laravel


【解决方案1】:

我之前没用过这个库,但是看代码好像可以set the filename attributewhichwill then get used in the headers to set the name of the file downloaded

大概是这样的:

Excel::load(public_path().'/bills/bill.template.xlsx', function($doc) 
{...})
    ->setFilename('whatever')
    ->download('xlsx');

【讨论】:

    【解决方案2】:
    $data = Excel::download($query, 'TimeSheetExport.xlsx');
    $data->setContentDisposition('attachment','TimeSheetExport')->getFile()->move(public_path('xlsx/'), $data->getFile().'xlsx');
    

    $query 是我们的收藏。

    'TimeSheetExport.xlsx' -> 不会是我们的文件名

    move()中,您可以在第二个参数中给出您的文件名,第一个参数将是该文件的位置

    【讨论】:

      猜你喜欢
      • 2016-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      相关资源
      最近更新 更多