简体   繁体   English

PHP mysql通过Excel导入数据

[英]PHP mysql import data through excel

I want to import data using php mysql from excel sheet containing 1.6 million records. 我想从包含160万条记录的Excel工作表中使用php mysql导入数据。 What is the best way to do this? 做这个的最好方式是什么?

this is the sample code I have used to iterate excel file and insert data in database: 这是我用来迭代excel文件并在数据库中插入数据的示例代码:

public function iterateData($file_name) {
        $fileDirectory = '';
        $file_name = $fileDirectory . $file_name;
        if (file_exists($file_name)) {
            $this->truncateTable();
            include 'PHPExcel2/Classes/PHPExcel/IOFactory.php';
            $objReader = PHPExcel_IOFactory::createReader('Excel2007');
            $objPHPExcel = $objReader->load($file_name);
            $count = 1;
            foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
                foreach ($worksheet->getRowIterator() as $row) {
                    $cellIterator = $row->getCellIterator();
                    $cellIterator->setIterateOnlyExistingCells(true); // Loop all cells, even if it is not set
                    $cellValues = array();
                    foreach ($cellIterator as $cell) {
                        if (!is_null($cell)) {
                            $cellValues[] = $cell->getCalculatedValue();
                        }
                    }
                    if (isset($cellValues[0]) && $cellValues[0] != 'Product' && $cellValues[0] != '') {
                        $this->inserInDatabase($cellValues);
                    } elseif (empty($cellValues[0]) && empty($cellValues[1]) && empty($cellValues[2])) {
                        continue;
                    }
                }
                if ($objPHPExcel->getSheetCount() == $count) {
                    return TRUE;
                }
                $count++;
            }
        } else {
            return FALSE;
        }
    } private function inserInDatabase($data) {
        $dbDetails = array(
            'db_name' => '*',
            'db_pass' => '*',
            'db_host' => 'localhost',
            'db_user' => '*'
        );
        $dbh = dbConnect::connect($dbDetails);
        $date = date('Y-m-d H:i:s');
        $sql = "INSERT INTO product_description (product_id, prpoduct_description, price, created_date) values ('" . mysql_escape_string($data[0]) . "', '" . mysql_escape_string($data[1]) . "', '" . mysql_escape_string($data[2]) . "', '$date')";
        if (!$dbh->dbh->query($sql)) {
            die('Database Connection Failed.');
        }
    }

将您的excel数据导出为csv格式,然后将csv格式导入到mysql

you can import using ; 您可以使用导入; if($request->hasFile('excelFile')){ if($ request-> hasFile('excelFile')){

        $inputFileType = 'Xlsx';

        $inputFileName = $request->file('excelFile')->getRealPath();

        /**  Create a new Reader of the type defined in $inputFileType  **/

        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

        /**  Advise the Reader that we only want to load cell data  **/

        $reader->setReadDataOnly(true);

        /**  Load $inputFileName to a Spreadsheet Object  **/

        $spreadsheet = $reader->load($inputFileName);

        foreach ($spreadsheet->getActiveSheet()->toArray() as $key => $row) {

                $data['question'] = $row[0];

                $data['option1'] = $row[1];

                $data['option2'] = $row[2];

                $data['option3'] = $row[3];

                $data['option4'] = $row[4];

                $data['correct'] = $row[5];

                $data['status'] = 1;

                $data['receiver'] = 'all';

                $data['createdOn'] = date("Y-m-d H:i:s");

                if(!empty($data)) {
                    DB::table('questions')->insert($data);
                }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM