zoukankan      html  css  js  c++  java
  • 从excel中读取数据

    include KIF_PATH . '/PHPExcel.class.php';
    
    //$inputFileType = 'Excel5';
            $inputFileType = 'Excel2007';
    
            $filePath = $this->excelFile;
    
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    
            if (!$objReader->canRead($filePath)) {
                echo 'error file type';
                exit();
            }
    
            $objExcel = $objReader->load($filePath);
    
    
            $objSheet = $objExcel->getSheet(0);
    
            $allRow = $objSheet->getHighestRow();
    
            $readColumns = array(
                'D', // 专柜名称
                'F', // 详细地址
                'J', //
                'K', //
                'L', // 店铺电话
            );
    
     $startRow = 2;
            for ($currentRow = $startRow; $currentRow <= $allRow; $currentRow++) {
                $row = $currentRow;
    
                $rowStr = '';
                foreach ($readColumns as $col) {
    
                    $value = $objSheet->getCell($col . $row)->getValue();
                    $value = trim($value);
                    if (strpos($value, ',') !== false) {
                        $value = str_replace(',', ',', $value);
                        echo $value . ' has ,  change to 中文逗号 ';
                        echo PHP_EOL;
                    }
    
                    // 如果有 <202d>, 则表格内是空的
                    $string = $value;
                    $tmpRes = array();
                    $length = strlen($string);
                    for ($i = 0; $i < $length; $i++) {
                        $tmpRes[] = '0x' . strtolower(base_convert(ord($string{$i}), 10, 16));//ord($string[$i]);
                    }
                    $tmpStr = implode('', $tmpRes);
                    if (strpos($tmpStr, '0xe20x800xad') !== false) {
                        echo $value . ' has special char, will set empty ';
                        echo PHP_EOL;
                        $value = '';
                    }
                    // end
    
    
                foreach ($wordSearch as $search => $replace) {
                    $rowStr = str_replace($search, $replace, $rowStr);
                }
    
                $rowStr = substr($rowStr, 0, -1);
    
                echo $rowStr . PHP_EOL;
    
                file_put_contents($csvFile, $rowStr . PHP_EOL, FILE_APPEND);
    
            }

    读取excel中的图片

    /**
         * 读excel里的内容
         */
        protected function readExcelData($filePath)
        {
            include KIF_PATH . '/PHPExcel.class.php';
    //        $inputFileType = 'Excel5';
            $inputFileType = 'Excel2007';
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            if (!$objReader->canRead($filePath)) {
                echo 'error file type';
                exit();
            }
            $objExcel = $objReader->load($filePath);
            $objSheet = $objExcel->getSheet(0);
            $allRow = $objSheet->getHighestRow();
            $datalist = array();
    
            $readColumns = array(
                'A' => 'name', // 抖音账号(选填)
                'B' => 'douyin_url', // 抖音主页地址
                'C' => 'star_url', // 个人主页地址
                'D' => 'tag_scores', // 任务商品分类及得分 - 自定义字段名,需特殊处理
               
            );
    
            // 读取产品图片
            $productImgs = array();
    
            $drawing = new PHPExcel_Writer_Excel2007_Drawing();
            $drawingHashTable = new PHPExcel_HashTable();
            $drawingHashTable->addFromSource($drawing->allDrawings($objExcel));
            for ($i = 0; $i < $drawingHashTable->count(); $i++) {
                $memoryDrawing = $drawingHashTable->getByIndex($i);
                if ($memoryDrawing instanceof PHPExcel_Worksheet_Drawing) {
                    $tmpFile = $memoryDrawing->getPath();
                    $ext = $this->getImgExtByName($memoryDrawing->getIndexedFilename());
                    $imgFileName = date("His") . '_' . rand(10000, 99999) . '.' . $ext;
                    $pathUrl = $this->imgFileUrl . '/' . $imgFileName;
                    $path = $this->imgFilePath . '/' . $imgFileName;
                    $copyRes = $this->copyfiles($tmpFile, $path, $pathUrl);
                    $codPos = $memoryDrawing->getCoordinates(); // N20
                    if (!$copyRes) {
                        $this->echo_msg($filePath . "	" . $codPos . " read img fail");
                        continue;
                    }
                    $productImgs[$codPos] = $pathUrl;
                }
            }
    
            $startRow = 2;
            for ($currentRow = $startRow; $currentRow <= $allRow; $currentRow++) {
                $row = $currentRow;
                $tmpData = array();
                foreach ($readColumns as $col => $field) {
                    $value = $objSheet->getCell($col . $row)->getValue();
                    $value = trim($value);
                    if (strpos($value, ',') !== false) {
                        $value = str_replace(',', ',', $value);
                        $this->echo_msg($value . ' has ,  change to 中文逗号 ');
                    }
    
                    // 如果有 <202d>, 则表格内是空的
                    $string = $value;
                    $tmpRes = array();
                    $length = strlen($string);
                    for ($i = 0; $i < $length; $i++) {
                        $tmpRes[] = '0x' . strtolower(base_convert(ord($string{$i}), 10, 16));//ord($string[$i]);
                    }
                    $tmpStr = implode('', $tmpRes);
                    if (strpos($tmpStr, '0xe20x800xad') !== false) {
                        $this->echo_msg($value . ' has special char, will set empty ');
                    }
                    // 开始时间, 结束时间 - 特殊处理
                    if ($value && in_array($col, ['P', 'Q'])) {
                        $value = date('Y-m-d', PHPExcel_Shared_Date::ExcelToPHP($value));
                    }
                    // 产品图片 - 特殊处理
                    if ($col == 'N') {
                        $value = isset($productImgs[$col . $row]) ? $productImgs[$col . $row] : '';
                    }
    
                    $tmpData[$field] = $value;
                }
                if (!implode('', array_values($tmpData))) {
                    $this->echo_msg($filePath . ' Line=' . $currentRow . ' is empty line');
                    continue;
                }
                $datalist[] = $tmpData;
            }
    
    
            return $datalist;
        }
    
    
        /**
         * 读取目录下的excel文件列表
         * @param $dir
         * @return array
         */
        protected function scanDir($dir)
        {
            $allFiles = scanDir($dir);
            $validFiles = array();
    
            foreach ($allFiles as $file) {
                if (strpos($file, '.xlsx') == false) {
                    continue;
                }
                $validFiles[] = $file;
            }
            return $validFiles;
        }
    
        protected function copyfiles($tmpFile, $filePath, $fileUrl)
        {
            $status = true;
            $contentx = @file_get_contents($tmpFile);
            $openedfile = fopen($filePath, "w");
            fwrite($openedfile, $contentx);
            fclose($openedfile);
            if ($contentx === FALSE) {
                $status = false;
            }
            if (!$status) {
                return $status;
            }
    
            // 七牛存储
            if ($this->uploadImg) {
                $objQiniuClient = new QiniuClient();
                $qiniuRes = $objQiniuClient->uploadByFile($fileUrl, $filePath);
                if (!$qiniuRes->isSuccess()) {
                    $status = false;
                    @unlink($filePath);
                }
            }
    
            return $status;
        }
    
        protected function getImgExtByName($imgName)
        {
            $lastPos = strrpos($imgName, '.');
            if (!$lastPos) {
                return 'jpg';
            }
            $suffix = substr($imgName, $lastPos + 1);
            if ($suffix == 'jpeg') {
                $suffix = 'jpg';
            }
            return $suffix;
        }
  • 相关阅读:
    day1-python简介+安装
    dgango中admin下添加搜索功能
    调用zabbix 分组api
    python 调用zabbix api实现查询主机信息,输出所有主机ip
    python实现用户登录界面
    怎样过滤跨站恶意脚本攻击(XSS)
    java服务安装(一):使用java service wrapper及maven打zip包
    详解Maven项目利用java service wrapper将Java程序生成Windows服务
    使用tomcat7-maven-plugin部署Web项目
    常用Maven插件介绍
  • 原文地址:https://www.cnblogs.com/bandbandme/p/12083092.html
Copyright © 2011-2022 走看看