首页 > 其他分享 >laravel导出excel文件

laravel导出excel文件

时间:2022-11-19 10:34:29浏览次数:45  
标签:laravel use excel 导出 myHeadings myArray Excel private columns

导出多个sheet

//ExportMulSheets.php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMultipleSheets;

class ExportMulSheets implements WithMultipleSheets
{
    use Exportable;

    private $size;
    private $myArray;
    private $myHeadings;
    private $sheetNames;
    private $columns;

    public function __construct($size, $myArray, $myHeadings, $sheetNames, $columns)
    {
        $this->size = $size;
        $this->myArray = $myArray;
        $this->myHeadings  = $myHeadings;
        $this->sheetNames = $sheetNames;
        $this->columns = $columns;
    }

    /**
     * @return array
     */
    public function sheets(): array
    {
        $sheets = [];
        for ($i = 0; $i < $this->size; $i++) {
            $sheets[] = new ExportSheet($this->myArray[$i], $this->myHeadings[$i], $this->sheetNames[$i], $this->columns[$i]);
        }
        return $sheets;
    }
}
//ExportSheet.php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\FromArray;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

class ExportSheet implements FromArray, WithHeadings, WithTitle, ShouldAutoSize, WithColumnFormatting
{
    private $myArray;
    private $myHeadings;
    private $title;
    private $columns;

    public function __construct($myArray, $myHeadings, $title, $columns = [])
    {
        $this->myArray = $myArray;
        $this->myHeadings = $myHeadings;
        $this->title = $title;
        $this->columns = $columns;
    }

    public function array(): array
    {
        return $this->myArray;
    }

    public function headings(): array
    {
        return $this->myHeadings;
    }

    public function title(): string
    {
        return $this->title;
    }

    public function columnFormats(): array
    {
        return $this->columns;
    }
}

excel导出下载

use App\Exports\ExportMulSheets;
use Excel;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

$columns = ['F' => NumberFormat::FORMAT_PERCENTAGE_00]; //用于sheet列的格式设定 
$excel = Excel::download(new ExportMulSheets(1, [$data], [$header], [$sheetName], [$columns]), $xlsFileName); 
return $excel;

 

标签:laravel,use,excel,导出,myHeadings,myArray,Excel,private,columns
From: https://www.cnblogs.com/caroline2016/p/16905582.html

相关文章

  • delphi TMS FlexCel 取消合并单元格
    TMSFlexCel取消合并单元格属性和方法TXlsFile.UnMergeCellsprocedureUnMergeCells(constfirstRow:Integer;constfirstCol:Integer;constlastRow:Integer;co......
  • delphi TMS FlexCel 合并单元格
    TMSFlexCel合并单元格属性和方法TXlsFile.MergeCellsprocedureMergeCells(constfirstRow:Integer;constfirstCol:Integer;constlastRow:Integer;constlast......
  • 16.导入excel数据
    导入外部数据导入.xls或.xlsx文件.pd.read_excel(io,sheet_name,header)常用参数说明.io:表示.xls或.xlsx文件路径或类文件对象.sheet_name:表示工作表,取值......
  • 【Azure 环境】向Azure Key Vault中导入证书有输入密码,那么导出pfx证书的时候,为什么没
    问题描述将pfx证书导入KeyVault的证书时,这个PFX需要输入正确的密码导入成功。但是当需要导出时,生成的pfx证书则不需要密码。这是正常的情况吗?  问题解答是的,这是A......
  • mysql导出 TIDB导入
    tiupdumpling-h10.10.14.229-P22066-uroot-p123456-t16-r200000-F256MiB-Btest_db-o's3://dsideal/HuangHai/TiDB?access-key=AK&secret-access-key=SK......
  • Excel函数Vlookup经典用法
    一、常规用法常规用法相信几乎所有职场人都用过,比如在这里,我们想要根据姓名来查找班级,只需要将公式设置为:=VLOOKUP(F5,B2:C11,2,0)二、多条件查找这种情况适用于查找值存在......
  • Excel2007选项卡打造(Ribbon菜单)
    打造ExcelRibbon菜单,2007版本适合最新的2021Excel。本质是XLM文件,注意,写错一个字符,Excel都不会显示您所写的菜单。还是借助工具写比较方便,参见:《RibbonX:自定义Office20......
  • EasyExcel对大数据量表格操作导入导出
    前言最近有个项目里面中有大量的Excel文档导入导出需求,数据量最多的文档有上百万条数据,之前的导入导出都是用apache的POI,于是这次也决定使用POI,结果导入一个四十多万的文......
  • vue2导出word文件
    安装依赖cnpminstall--savedocxtemplaterpizzip jszip-utilsfile-saver模板文件(模板文件中使用{name},‘{name}’将会被替换成‘张三’)test.docx放在public......
  • Global Mapperv17 裁剪dem并导出等高线
    1、加载dem数据数据投影2、加载shp范围3、选中范围数据4、裁剪5、生成等高线6、导出等高线矢量......