thinkphpPHPExcel报过失:ClassPHPExcel_IOFactorynotfound的解决方式

原创
小哥 3年前 (2022-11-21) 阅读数 8 #大杂烩

<?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();
}
}

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除