Home  >  Article  >  Backend Development  >  How to save pictures in EXCEL with PHP

How to save pictures in EXCEL with PHP

*文
*文Original
2017-12-25 15:21:181989browse

How to save pictures in EXCEL in PHP? This article mainly introduces the method of reading pictures in EXCEL through PHPExcel and saving the file. I hope that using PHPExcel to read pictures in EXCEL and save the file will give everyone a clearer understanding of PHPExcel.

HPExcel is a very powerful MS Office Excel document generation class library. When you need to output data in a more complex format, PHPExcel is a good choice.

After carefully studying the API documentation and checking the official documentation, I finally found the way to read pictures in EXCEL. Currently, I can only read the excel 2003 format. It seems that excel2007 does not support it yet. The main APIs used are PHPExcel_Worksheet, PHPExcel_Worksheet_BaseDrawing, PHPExcel_Worksheet_MemoryDrawing.

Stop talking nonsense and go straight to the code:

require_once './Classes/PHPExcel.php';
$objPHPExcel = new PHPExcel();
$objReader = PHPExcel_IOFactory::createReader('Excel5');  //加载2003的
$objPHPExcel = $objReader->load("goods_list.xls");  //载入文件
foreach ($objPHPExcel->getSheet(0)->getDrawingCollection() as $k => $drawing) {
        $codata = $drawing->getCoordinates(); //得到单元数据 比如G2单元
        $filename = $drawing->getIndexedFilename();  //文件名
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        file_put_contents('pic/'.$codata.'_'.$filename.'.jpg',$imageContents); //把文件保存到本地
        ob_end_clean();
}

Related recommendations:

Example sharing of solving PHPExcel memory leaks

Thinkphp5+PHPExcel realizes the function of batch uploading table data_php example

##Using ThinkPHP, uploadify, upload, PHPExcel how to import data without refreshing

The above is the detailed content of How to save pictures in EXCEL with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn