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!";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值