phpspreadsheet导出数据到Excel,PHP读取Excel内的图片

图片 2

今天采纳了四个从Excel内读取图片的必要,在网络寻觅了某些材质,基本落到实处了温馨的急需,但是由于查到的部分代码相比浓郁,不可能向来移植到温馨的等级次序里,要求稍加改变一下。

事情发生从前大家利用PHP导出Excel数据时接收的是PHPExcel库,然而phpoffice已经官方发布PHPExcel已经被放任不在维护,推荐应用phpspreadsheet,如下图所示

此地介绍一下各自使用phpspreadsheet和PHPExcel扩张库来促成读取Excel内图片的功效:

图片 1

PHPSpreadsheet

咱俩得以因此composer安装phpspreadsheet

第一安装phpspreadsheet,由于线上服务器PHP版本是PHP5.6,所以须要安装宽容PHP5.6的版本,这里设置1.8.2本子

图片 2

composer require phpoffice/phpspreadsheet=1.8.2
composer require phpoffice/phpspreadsheet

下一场就可以在类型里应用了

动用phpspreadsheet导出学生消息到浏览器代码示例

use PhpOfficePhpSpreadsheetCellCoordinate;use PhpOfficePhpSpreadsheetIOFactory;$imageFilePath = './uploads/imgs/'; //图片本地存储的路径if (!file_exists { //如果目录不存在则递归创建 mkdir($imageFilePath, 0777, true);}try { $inputFileName = './files/1.xlsx'; //包含图片的Excel文件 $objRead = IOFactory::createReader; $objSpreadsheet = $objRead->load; $objWorksheet = $objSpreadsheet->getSheet; $data = $objWorksheet->toArray(); foreach ($objWorksheet->getDrawingCollection { list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates; $imageFileName = $drawing->getCoordinates() . mt_rand; switch ($drawing->getExtension { case 'jpg': case 'jpeg': $imageFileName .= '.jpg'; $source = imagecreatefromjpeg; imagejpeg($source, $imageFilePath . $imageFileName); break; case 'gif': $imageFileName .= '.gif'; $source = imagecreatefromgif; imagegif($source, $imageFilePath . $imageFileName); break; case 'png': $imageFileName .= '.png'; $source = imagecreatefrompng; imagepng($source, $imageFilePath, $imageFileName); break; } $startColumn = ABC2decimal; $data[$startRow-1][$startColumn] = $imageFilePath . $imageFileName; } dump;} catch  { throw $e;}public function ABC2decimal{ $ten = 0; $len = strlen; for{ $char = substr;//反向获取单个字符 $int = ord; $ten += ; } return $ten;}
/** * 输出到浏览器(需要设置header头) * @param string $fileName 文件名 * @param string $fileType 文件类型 */function excelBrowserExport($fileName, $fileType) {    //文件名称校验    if(!$fileName) {        trigger_error('文件名不能为空', E_USER_ERROR);    }    //Excel文件类型校验    $type = ['Excel2007', 'Xlsx', 'Excel5', 'xls'];    if(!in_array($fileType, $type)) {        trigger_error('未知文件类型', E_USER_ERROR);    }    if($fileType == 'Excel2007' || $fileType == 'Xlsx') {        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');        header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');        header('Cache-Control: max-age=0');    } else { //Excel5        header('Content-Type: application/vnd.ms-excel');        header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');        header('Cache-Control: max-age=0');    }}$data = [    [1, 'jack', 10],    [2, 'mike', 12],    [3, 'jane', 21],    [4, 'paul', 26],    [5, 'kitty', 25],    [6, 'yami', 60],];$title = ['id', 'name', 'age'];$spreadsheet = new Spreadsheet();$worksheet = $spreadsheet->getActiveSheet();//设置工作表标题名称$worksheet->setTitle;//表头//设置单元格内容foreach ($title as $key => $value) {    $worksheet->setCellValueByColumnAndRow($key+1, 1, $value);}$row = 2; //从第二行开始foreach ($data as $item) {    $column = 1;    foreach ($item as $value) {        $worksheet->setCellValueByColumnAndRow($column, $row, $value);        $column++;    }    $row++;}$fileName = '学生信息';$fileType = 'Xlsx';//1.下载到服务器//$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');//$writer->save($fileName.'.'.$fileType);//2.输出到浏览器$writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); //按照指定格式生成Excel文件excelBrowserExport($fileName, 'Xlsx');$writer->save('php://output');

发表评论

电子邮件地址不会被公开。 必填项已用*标注