0

我在从 excel 文件中获取图像和文本数据时遇到问题。目前我正在使用 PHPspreadsheet 库从 excel 中获取数据。所以它给了我图像数据,但没有给我文本数据。如果我寻找文本数据而不是获取图像数据。所以我需要两种类型的数据都可以在单个代码进程中获取所以如果有人对如何在一个进程中获取图像和文本数据有一些想法,请给我一些建议或示例

我正在使用 laravel 5.5,这是我的代码

$path = \Storage::disk('local')->getDriver()->getAdapter()->applyPathPrefix("Book1.xlsx");
    $objPHPExcel = IOFactory::load($path);
    $i = 0;
    $obj =$objPHPExcel->getSheetByName("Sheet1")->getDrawingCollection();
    foreach ($obj as $drawing) {
        if ($drawing instanceof \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing) {
            ob_start();
            call_user_func(
                $drawing->getRenderingFunction(),
                $drawing->getImageResource()
            );
            $imageContents = ob_get_contents();
            ob_end_clean();
            switch ($drawing->getMimeType()) {
                case \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_PNG :
                    $extension = 'png';
                    break;
                case \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_GIF:
                    $extension = 'gif';
                    break;
                case \PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing::MIMETYPE_JPEG :
                    $extension = 'jpg';
                    break;
            }
        } else {
            $zipReader = fopen($drawing->getPath(),'r');
            $imageContents = '';
            while (!feof($zipReader)) {
                $imageContents .= fread($zipReader,2048);
            }
            fclose($zipReader);
            $extension = $drawing->getExtension();
        }
        /* Getting Column and Row name */
        $c = $drawing->getCoordinates();
        preg_match_all('!\d+!', $c, $matches);
        $column = $matches[0][0];
        $row = strstr($c,$column,true);

        $myFileName = '00_Image_'.++$i.'.'.$extension;
        file_put_contents($myFileName,$imageContents);
    }
4

0 回答 0