phpspreadsheet使用导出excel

安装

composer require phpoffice/phpspreadsheet

1、简单使用导出到服务器

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;

/**
     * 导出欠费明细表
     * */
    public function index(){
        $name  = date("YmdHis", time());
        $head = ['日期', '付款人数', '付款订单数', '付款金额', '付款件数'];// 表头信息
        $keys = ['time', 'order_num', 'did', 'total_price', 'good_num'];
        $count = count($head);  //计算表头数量
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();

        for ($i = 65; $i < $count + 65; $i++) {     //数字转字母从65开始,循环设置表头:
            $sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
        }

        for($s=1;$s<=1000000;$s++){
            $spreadsheet->getActiveSheet()->setCellValue('A' . $s, $s);
        }


        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename=/www/wwwroot/swoole/erp-test-zhoao2/excel/666.xlsx"');
        header('Cache-Control: max-age=0');
        $writer = new Xlsx($spreadsheet);
        $writer->save('/www/wwwroot/swoole/erp-test-zhoao2/excel/666.xlsx');

        //删除清空:
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);
        return 666;
    }

2、样式

1、字体
$sheet->getStyle('B1')->getFont()->setBold(true);//将B1单元格设置为粗体字
$sheet->getStyle('A7:B7')->getFont()->setBold(true)->setName('Arial')
            ->setSize(10);//将A7至B7两单元格设置为粗体字,Arial字体,10号字


2、改变字体颜色
$sheet->getStyle('A1')->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_RED);//将A1单元格文字颜色设为红色


3、设置列宽
$sheet->getColumnDimension('A')->setWidth(20);//将A列的宽度设为20(字符)
$sheet->getColumnDimension('B')->setAutoSize(true);//将B列的宽度设为自动宽度
$sheet->getDefaultColumnDimension()->setWidth(12);//设置默认列宽为12


4:设置行高(两个不能同时使用)
$sheet->getRowDimension('10')->setRowHeight(100);//将第十行的高度设为100pt
$sheet->getDefaultRowDimension()->setRowHeight(50);//设置默认行高为20

5:对齐
$sheet->getStyle('A:D')->getAlignment()
->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER) //设置垂直居中
->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER) //设置水平居中
->setWrapText(true); //设置自动换行
居中
$sheet->getStyle('A:N')->getAlignment()
            ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER) //设置水平居中
            ->setWrapText(true); //设置自动换行
左对齐
 $sheet->getStyle('A3:A4')->getAlignment()
            ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT) //设置左对齐
            ->setWrapText(true); //设置自动换行

6、合并单元格
$sheet->mergeCells('A1:D2');//A1到D2合并为一个单元格
1、将合并后的单元格拆分
$sheet->unmergeCells('A1:D2');//将合并后的单元格拆分。

2、使用applyFromArray实现单元格样式设置
//样式变量
$style = [
//设置字体样式
'font' => [
        'name' => 'Arial',
        'bold' => true,
        'italic' => false,
        'underline' => Font::UNDERLINE_DOUBLE,
        'strikethrough' => false,
        'color' => [
            'rgb' => '808080'
        ]
    ],
//设置边框线样式
'borders' => [
         //allBorders所有的边框线样式
         //左边框线
       'bottom' => [
           'borderStyle' => Border::BORDER_DASHDOT,
           'color' => [
              'rgb' => '808080'
            ]
       ],
         //上边框线
       'top' => [
           'borderStyle' => Border::BORDER_DASHDOT,
           'color' => [
               'rgb' => '808080'
           ]
       ]
],
//对齐样式
'alignment' => [
   'horizontal' => Alignment::HORIZONTAL_CENTER,
   'vertical' => Alignment::VERTICAL_CENTER,
   'wrapText' => true,
],
//是否使用前缀
'quotePrefix'    => true
];
$sheet->getStyle('A1:D1')->applyFromArray($style);

黑边框

$styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN //黑边框
                ]
            ]
];
$sheet->getStyle('A1:N1')->applyFromArray($styleArray);

设置边框为白色

$styleArray = [
     'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN, //黑边框
                    'color' => array ('argb' => 'FFFFFFFF')
                ]
     ]
];

$sheet->getStyle('A1:N1')->applyFromArray($styleArray);

phpspreadsheet使用导出excel_第1张图片

背景颜色

$sheet->getStyle("C1:N1")->getFill()->setFillType(Fill::FILL_SOLID)
->getStartColor()->setARGB("FFf1e4b5");

设置工作表标题,工作簿操作

//创建工作表
$spreadsheet->createSheet();
//切换工作表$key是数字,切换到那个工作簿
$spreadsheet->setActiveSheetIndex($key);
$sheet = $spreadsheet->getActiveSheet($key);

$sheet->setTitle('Hello');//设置当前工作表标题。

设置单元格的格式

$sheet->getStyle('D2')->getNumberFormat()
            ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);//将D2单元格的格式设为文本格式
$sheet->getStyle('A1:D2')->getNumberFormat()
            ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);//将A1到D2的单元格设置为文本格式

换行

$sheet->getCell('A4')->setValue("hello\nworld");//将A4单元格的hello和world换行

超链接

//将A2单元格内容设置blog并点击跳转https://www.wj0511.com
$sheet->setCellValue('A2', 'blog');
$sheet->getCell('A2')->getHyperlink()->setUrl('https://www.wj0511.com');

边框

$styleArray = [
    'borders' => [
  		'allBorders' => [
            'borderStyle' => Border::BORDER_THIN //细边框
        ]
   	 ]
];
  
$worksheet->getStyle('A1:N1')->applyFromArray($styleArray);

你可能感兴趣的:(excel,开发语言,php)