Yii2 使用PhpSpreadsheet扩展导出多sheet表格

先composer安装PhpSpreadsheet扩展

composer require "phpoffice/phpspreadsheet": "^1.11"

 直接贴代码

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

public function exportExcel($title, $header, $sheet_title, $data)
{
        $count = count($data);
        $spreadsheet = new Spreadsheet();
        for($i=0;$i<$count;$i++){
            $spreadsheet->createSheet();//创建sheet
            $objActSheet = $spreadsheet->setActiveSheetIndex($i);//设置当前的活动sheet
            $objActSheet->setTitle($sheet_title[$i]);
            foreach ($header[$i] as $index => $item) {
                $cellnum = Coordinate::stringFromColumnIndex($index + 1) . "1";
                $objActSheet->setCellValue($cellnum, $item);
                $objActSheet->getStyle($cellnum)->getFont()->setBold(true); //标题栏加粗
                $objActSheet->getStyle($cellnum)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //居中
            }
            $loop = 2;
            foreach ($data[$i] as $values) {
                foreach ($values as $index => $val) {
                    $cellnum = Coordinate::stringFromColumnIndex($index + 1);
                    $objActSheet->setCellValue($cellnum . $loop, $val);
                    $objActSheet->getStyle($cellnum. $loop)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER); //居中
                }
                $loop++;
            }
        }

        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename=' . $title . '.xlsx');
        header('Cache-Control: max-age=0');
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //删除清空:
        $spreadsheet->disconnectWorksheets();
        unset($spreadsheet);

        exit;
}

数据格式说明:

$title = '班级1快速问答报告';

//每个sheet的表头
$header = Array
(
    [0] => Array
        (
            [0] => 题号
            [1] => 题型
            [2] => A
            [3] => B
            [4] => C
            [5] => D
            [6] => 正确
            [7] => 错误
            [8] => 正确答案
            [9] => 正确率
        )

    [1] => Array
        (
            [0] => 姓名
            [1] => 1
            [2] => 2
            [3] => 3
            [4] => 正确率
        )

    [2] => Array
        (
            [0] => 姓名
            [1] => 1
            [2] => 2
            [3] => 3
        )

)
//每个sheet的名称
$sheet_title = Array
(
    [0] => 题目统计
    [1] => 学生统计
    [2] => 原始数据
)
//数据,对应每个表格的表头
$data = Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 1
                    [1] => 多选题
                    [2] => 39
                    [3] => 37
                    [4] => 3
                    [5] => 1
                    [6] => --
                    [7] => --
                    [8] => AB
                    [9] => 87.5%
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => 诸葛大师
                    [1] => ✔
                    [2] => ○
                    [3] => ✔
                    [4] => 66.67%
                )
        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => 诸葛大师
                    [1] => AB
                    [2] => 错误
                    [3] => A
                )

        )

)

好啦,就这样吧,自己测试成功了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Yii2框架是一个基于PHP的Web应用程序开发框架,而PhpSpreadsheet是一个用于操作Excel文件的PHP类库。结合使用Yii2PhpSpreadsheet插件可以实现Excel文件的导入和导出功能。 以下是使用PhpSpreadsheet插件实现Excel文件导入和导出的示例代码: 1. Excel文件导入 在控制器中引入PhpSpreadsheet插件: ```php use PhpOffice\PhpSpreadsheet\IOFactory; use PhpOffice\PhpSpreadsheet\Spreadsheet; ``` 在action中实现Excel文件导入: ```php public function actionImportExcel() { $inputFileName = 'path/to/file.xlsx'; // 文件路径 $spreadsheet = IOFactory::load($inputFileName); $worksheet = $spreadsheet->getActiveSheet(); $highestRow = $worksheet->getHighestRow(); $highestColumn = $worksheet->getHighestColumn(); $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); for ($row = 1; $row <= $highestRow; ++$row) { for ($col = 0; $col < $highestColumnIndex; ++$col) { $cellValue = $worksheet->getCellByColumnAndRow($col, $row)->getValue(); // 处理单元格数据 } } } ``` 2. Excel文件导出 在控制器中引入PhpSpreadsheet插件: ```php use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; ``` 在action中实现Excel文件导出: ```php public function actionExportExcel() { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World!'); $writer = new Xlsx($spreadsheet); $filename = 'export.xlsx'; // 文件名称 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Cache-Control: max-age=0'); $writer->save('php://output'); } ``` 以上是使用Yii2框架和PhpSpreadsheet插件实现Excel文件导入和导出的示例代码,可以根据实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值