thinkphp5.1 PHPExcel报错误: Class PHPExcel_IOFactory not found的解决方法

<?php
namespace excel;

require_once '../vendor/phpoffice/phpexcel/Classes/PHPExcel.php';
require_once '../vendor/phpoffice/phpexcel/Classes/PHPExcel/IOFactory.php';

class ExcelPHP{
    // 读取Excel表格数据
    public static function importexcel($path)
    {
        // 判断文件是什么格式
        $type = pathinfo($path);
        $type = strtolower($type["extension"]);
        
        if ($type == 'xlsx') {
            $type = 'Excel2007';
        } elseif ($type == 'xls') {
            $type = 'Excel5';
        }
        // 最长执行时间,php默认为30秒,这里设置为0秒的意思是保持等待直到程序执行完成
        ini_set('max_execution_time', '0');
        // 判断使用哪种格式
        $objReader = \PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($path);
        $sheet = $objPHPExcel->getSheet(0); // 获取工作薄
        // 取得总行数
        $highestRow = $sheet->getHighestRow();
        // 取得总列数
        $highestColumn = $sheet->getHighestColumn();
        // 循环读取excel文件,读取一条,插入一条
        $data = array();
        // 从第一行开始读取数据 这里类似冒泡算法
        for ($j = 1; $j <= $highestRow; $j ++) {
            // 从A列读取数据
            for ($k = 'A'; $k <= $highestColumn; $k ++) {
                // 读取单元格
                $data[$j][] = $objPHPExcel->getActiveSheet()
                ->getCell("$k$j")
                ->getValue();
            }
        }
        return $data;
    }
    
    /**
     * 数组转xls格式的excel文件
     *
     * @param array $data
     *            需要生成excel文件的数组
     * @param string $filename
     *            生成的excel文件名
     *            示例数据:
     *            $data = array( // 按照该结构封装即可
     *            'cols' => array('姓名','班级','年龄'),
     *            'rows' =>array(
     *            array('小明','三年一班','10岁'),
     *            array('小波','三年二班','30岁'),
     *            array('小薛','三年三班','11岁'),
     *            ),
     *            );
     */
    // 这个封装的方法值得学习一下,可以参看上边的export()方法看看规律
    public static function exportexcel($data, $title)
    {
        ini_set('max_execution_time', '0'); // 最长执行时间,php默认为30秒,这里设置为0秒的意思是保持等待直到程序执行完成
        
        $phpexcel = new \PHPExcel();
        
        // Set properties 设置文件属性
        $properties = $phpexcel->getProperties();
        $properties->setCreator("Boge"); // 作者是谁 可以不设置
        $properties->setLastModifiedBy("Boge"); // 最后一次修改的作者
        $properties->setTitle($title); // 设置标题
        $properties->setSubject('测试'); // 设置主题
        $properties->setDescription("备注"); // 设置备注
        $properties->setKeywords("关键词"); // 设置关键词
        $properties->setCategory("类别"); // 设置类别
        
        $sheet = $phpexcel->getActiveSheet();
        $sheet->setTitle('Sheet1'); // 设置sheet名称
        
        // 从A开始
        $startLetter = 'A';
        $rowNumber = 1;
        
        // 遍历表头
        foreach ($data['cols'] as $key => $col) {
            $sheet->setCellValue($startLetter ++ . $rowNumber, $col);
        }
        ++ $rowNumber;
        // 遍历数据
        foreach ($data['rows'] as $key => $row) {
            $startLetter = 'A';
            foreach ($row as $key => $value) {
                $sheet->setCellValue($startLetter ++ . $rowNumber, $value);
            }
            ++ $rowNumber;
        }
        
        $phpexcel->setActiveSheetIndex(0);
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=" . $title . ".xls");
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');
        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
        $objwriter = PHPExcel_IOFactory::createWriter($phpexcel, 'Excel5');
        $objwriter->save('php://output');
        exit();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梅坞茶坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值