创建文件Export.php
namespace App;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Style\Border;
class Export implements FromCollection,WithHeadings, WithEvents
protected $data;
protected $headings;
protected $columnWidth = [];
protected $rowHeight = [];
protected $mergeCells = [];
protected $font = [];
protected $fontSize = [];
protected $bold = [];
protected $background = [];
protected $vertical = [];
protected $sheetName;
protected $borders = [];
public function __construct($data, $headings,$sheetName)
$this->data = $data;
$this->headings = $headings;
$this->sheetName = $sheetName;
$this->createData();
public function headings(): array
return $this->headings;
public function collection()
return new Collection($this->data);
public function createData()
$this->data = collect($this->data)->toArray();
* @return array
* 'B' => 40,
* 'C' => 60
public function setColumnWidth (array $columnwidth)
$this->columnWidth = array_change_key_case($columnwidth, CASE_UPPER);
* @return array
* 1 => 40,
* 2 => 60
public function setRowHeight (array $rowHeight)
$this->rowHeight = $rowHeight;
* @return array
* A1:K7 => '宋体'
public function setFont (array $font)
$this->font = array_change_key_case($font, CASE_UPPER);
* @return array
* @2020/3/22 10:33
* A1:K7 => true
public function setBold (array $bold)
$this->bold = array_change_key_case($bold, CASE_UPPER);
* @return array
* @2020/3/22 10:33
* A1:K7 => F0FF0F
public function setBackground (array $background)
$this->background = array_change_key_case($background, CASE_UPPER);
* @return array
* A1:K7
public function setMergeCells (array $mergeCells)
$this->mergeCells = array_change_key_case($mergeCells, CASE_UPPER);
* @return array
* A1:K7 => 14
public function setFontSize (array $fontSize)
$this->fontSize = array_change_key_case($fontSize, CASE_UPPER);
* @return array
* A1:K7 => #000000
public function setBorders (array $borders)
$this->borders = array_change_key_case($borders, CASE_UPPER);
在控制器中调用导出下载:
use App\Export;
use Maatwebsite\Excel\Facades\Excel as LaravelExcel;
$data = [];
$head = [];
$filename = 'excel';
$excel = new Export($data, $head , 'Sheet1');
return LaravelExcel::download($excel, $filename . date('Y-m-d') . '.xls');
注意:download
方法只能在控制器中使用
今天项目需要提取excel的内容,composer require maatwebsite/excel;
下边开始代码部分
public function readFile(\Maatwebsite\Excel\Excel $excel)
$excel_file_path = storage_path('zip/weather_forecast/weather_forecast/Ch...
$filePath = 'xxx.xlsx';
//$filePath 根据 config/filesystems.php文件中配置找到文件
Excel::import($import,$filePath,null,\Maatwebsite\Excel\Excel::XLSX);
XXImport
<?php
namespace App\Imports;
private static $host="localhost";
private static $user="root";
private static $password="123456";
private static $dbName=
//showPage(页号,总页数,分隔符)
function showPage($page,$totalPage,$sep=" "){
$url = $_SERVER ['PHP_SELF']; //获取当前路径
$index = ($page == 1) ? "首页" : "首页";
$last
个人博客:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:jian1098@qq.com
1.安装方式
thinkphp6只能通过composer安装
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer # 设.
你可以在以下网站中下载jsp-api.jar 3.1版本:
1. Maven仓库:https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api/2.3.3
2. Jar下载网站:http://www.java2s.com/Code/Jar/j/Downloadjspapi313jar.htm
请注意,这个版本的jsp-api.jar适用于Servlet API 3.1。如果你使用的是其他版本的Servlet API,请下载相应版本的jsp-api.jar。