php excel給excel批量插入圖片

本人最近在藥房網採集了3w多數據,結果藥品的批准文號是圖片格式,結果ocr解析完之後,準確率達不到100%,於是想到把圖片插入到excel中,給產品人員人工校對.於是就寫了個php程序,切入正題:

1.首先去官網下載php excel lib,

2.然後寫入如下代碼,代碼在附件

<?php
require 'Classes/PHPExcel.php';
set_time_limit(0);
ini_set("memory_limit", "512M"); // not required unless you are hitting a limit
$dir = "./split_approve";
$files = scandir($dir);
$fileType = 'Excel2007';
foreach ($files as $file) {
    if ($file != "." && $file != "..") {
        $fileName = $dir . "/" . $file;
        // Load the workbook
        $objPHPExcelReader = PHPExcel_IOFactory::createReader($fileType);
        $objPHPExcel = $objPHPExcelReader->load($fileName);
        $count = 0;
        $j = 0;
        try {
            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                foreach ($worksheet->getRowIterator() as $row) {
                    $rowIndex = $row->getRowIndex();
                    //if($rowIndex<22)continue;
                    $cell = $worksheet->getCell('C' . $rowIndex);
                    $pic_path = $cell->getCalculatedValue();
                    $cell = $worksheet->getCell('D' . $rowIndex);
                    $pic_valid = $cell->getCalculatedValue();
                    if ($pic_valid == 2)
                        continue;
                    if (!file_exists($pic_path))
                        continue;
                    // Add an image to the worksheet
                    $objDrawing = new PHPExcel_Worksheet_Drawing();
                    $objDrawing->setPath($pic_path);
                    $objDrawing->setCoordinates('E' . $rowIndex);
                    $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
                    $count++;
                    if ($count > 1000) {
                        $j++;
                        $num = $j * 1000;
                        file_put_contents('excel.log', "count_" . $num . "_index_" . $rowIndex . PHP_EOL,
                            FILE_APPEND);
                        $count = 0;
                    }
                }
            }
        }
        catch (exception $e) {
            echo $e->getMessage();
            file_put_contents('excel.log', $e->getMessage(), FILE_APPEND);
            exit;
        }
        // Save the workbook
        $objPHPExcelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
        $objPHPExcelWriter->save("./split_approve_pic/" . $file);
        echo "finished!";
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章