php+phpspreadsheet读取Excel数据存入mysql

前端之家收集整理的这篇文章主要介绍了php+phpspreadsheet读取Excel数据存入mysql前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

生成Excel模板,然后导入Excel数据到MysqL,每条数据对应图片上传到阿里云

PHP PHPStorm. PHPOffice\PHPSpreadsheet\Spreadsheet; PHPOffice\PHPSpreadsheet\Writer\Xlsx; ProductBatchModel add( (!->_validate(,'add' = = = = (((['excel_file'],'.'),1)<>'xlsx' ->addError('excel_file','请上传xlsx格式的Excel文件' = ('com/',['excel_file' = \PHPOffice\PHPSpreadsheet\IOFactory::createReader('Xlsx' ->setReadDataOnly( = ->load(DIR_UPLOAD.[1]); = -> = ->getHighestRow(); = ->getHighestColumn(); = \PHPOffice\PHPSpreadsheet\Cell\Coordinate::columnIndexFromString(); = - 1 ( <= 0 ->addError('Excel','Excel表格中没有数据' = = ( = 2; <= ; ++ = ->getCellByColumnAndRow(1,)-> (( ->addError('model_no'.,'第'..'行产品型号为空' } = ->getProductByModel( (( [] = ; 分类********************************** = ->getCellByColumnAndRow(2,)-> (( ->addError('category_name'.,'第'..'行分类名为空' } = ->getCategoryIdByName( (( ->addError('category_name'.,'第'..'行分类名不存在' = ->getCellByColumnAndRow(3,)-> (() || !( ->addError('sort_order'.,'第'..'行排序错误' = ->getCellByColumnAndRow(4,)-> (() || !( ->addError('status'.,'第'..'行状态错误' = ->getCellByColumnAndRow(5,)-> (( ->addError('language_name'.,'第'..'行语言为空' } = ->getSysLanguageByName( (( ->addError('languages'.,'第'..'行语言不存在' = ->getCellByColumnAndRow(6,)-> (( ->addError('name'.,'第'..'行名称为空' = ->getCellByColumnAndRow(7,)-> (( ->addError('keywords'.,'第'..'行关键词为空' = ->getCellByColumnAndRow(10,)-> (!( ((, ->addError('url'.,'第'..'行网址重复出现' } [] = (-> = ( => = ->getCellByColumnAndRow(1,)-> 获取分类ID = ->getCellByColumnAndRow(2,)-> = ->getCategoryIdByName( = ['category_id' = ->getCellByColumnAndRow(3,)-> = ->getCellByColumnAndRow(4,)-> 获取语言ID = ->getCellByColumnAndRow(5,)-> = ->getSysLanguageByName( = ['language_id' = ->getCellByColumnAndRow(6,)-> = ->getCellByColumnAndRow(7,)-> = ->getCellByColumnAndRow(8,)-> = ->getCellByColumnAndRow(9,)-> = ->getCellByColumnAndRow(10,)-> Meta_title = ->getCellByColumnAndRow(11,)-> Meta_keyword = ->getCellByColumnAndRow(12,)-> Meta_description = ->getCellByColumnAndRow(13,)-> (!([ [] = 'product_type_id'=>'', 'model_no'=>, 'sort_order'=>, 'status'=>, 'category'=>(), 'trade'=>'', 'producttradeinfo'=> []['desc'][] = 'description'=>, 'name'=>, 'abstract'=>, 'keywords'=>, 'language_name'=>, 'Meta_title'=>Meta_title, 'Meta_keyword'=>Meta_keyword, 'Meta_description'=>Meta_description, 'url_alias'=> ( => ((['desc']) && !(['desc' (['desc'] => = ->validate(['keyword' => ['url_alias'],'language_id' => ,'query' => ''],'add' (== 图片压缩包 = DIR_UPLOAD.->session->data['site_id' = .'/'.['images_file' (!() || (((,1)<>'zip' ->addError('imageZip','请上传产品图片压缩包(zip格式)' = = ->unzip(,.'/',, ( (); 删除压缩包 获取产品图片 ( => []['image'] = ->_getImage(,.'/'.(['images_file'],'.zip' ( => ->add( } ( ->addError('add',-> 生成模板 = = -> = = -> = ->_getCategoryName( = 3000 = 内容********************************** = -> ->setCellValue('A1','型号(必填)' ->setCellValue('B1','分类名(必填)' ->setCellValue('C1','排序(必填,请输入数字)' ->setCellValue('D1','状态(必填,1=上架|0=下架)' ->setCellValue('E1','语言(必填)' ->setCellValue('F1','名称(必填)' ->setCellValue('G1','关键词(必填)' ->setCellValue('H1','简要描述' ->setCellValue('I1','详细描述' ->setCellValue('J1','网址' ->setCellValue('K1','SEO标题' ->setCellValue('L1','SEO关键词' ->setCellValue('M1','SEO描述' 属性 ->setTitle('Product' ->getDefaultColumnDimension()->setWidth(30 ->getStyle('A1:M1')->getFill()->setFillType(\PHPOffice\PHPSpreadsheet\Style\Fill:: ->getStyle('A1:M1')->getFill()->getStartColor()->setARGB('FF808080' ->getStyle('A1:M1')->getFont()->getColor()->setARGB(\PHPOffice\PHPSpreadsheet\Style\Color:: 分类列 (=2;<=;++ = ->getDataValidation('B'. -> (\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setErrorStyle(\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setAllowBlank( -> setShowInputMessage( -> setShowErrorMessage( -> setShowDropDown( -> setErrorTitle('输入的值有误' -> setError('您输入的值不在下拉框列表内' -> setPrompt('请选择下拉框列表中的值' -> setFormula1('data!$C$3:$C$'.(()+2 (=2;<=;++ = ->getDataValidation('D'. -> (\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setErrorStyle(\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setAllowBlank( -> setShowInputMessage( -> setShowErrorMessage( -> setShowDropDown( -> setErrorTitle('输入的值有误' -> setError('您输入的值不在下拉框列表内' -> setPrompt('请选择下拉框列表中的值' -> setFormula1('data!$A$3:$A$4' 301 (=2;<=;++ = ->getDataValidation('E'. -> (\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setErrorStyle(\PHPOffice\PHPSpreadsheet\Cell\DataValidation:: -> setAllowBlank( -> setShowInputMessage( -> setShowErrorMessage( -> setShowDropDown( -> setErrorTitle('输入的值有误' -> setError('您输入的值不在下拉框列表内' -> setPrompt('请选择下拉框列表中的值' -> setFormula1('data!$B$3:$B$'.(()+2 -> ->setActiveSheetIndex(1 = -> ->setTitle('data' ->getDefaultColumnDimension()->setWidth(25 ->mergeCells('A1:C1' = 'alignment' => 'horizontal' => \PHPOffice\PHPSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, 'vertical' => \PHPOffice\PHPSpreadsheet\Style\Alignment::VERTICAL_CENTER, ], ->getStyle('A1')->applyFromArray( ->getRowDimension('1')->setRowHeight(35 ->getStyle('A1')->getFill()->setFillType(\PHPOffice\PHPSpreadsheet\Style\Fill:: ->getStyle('A1')->getFill()->getStartColor()->setARGB('8B0000' ->getStyle('A1')->getFont()->getColor()->setARGB(\PHPOffice\PHPSpreadsheet\Style\Color:: ->setCellValue('A1','公共数据(请不要修改)' ->getStyle('A2:C2')->getFill()->setFillType(\PHPOffice\PHPSpreadsheet\Style\Fill:: ->getStyle('A2:C2')->getFill()->getStartColor()->setARGB('FF808080' ->getStyle('A2:C2')->getFont()->getColor()->setARGB(\PHPOffice\PHPSpreadsheet\Style\Color:: ->setCellValue('A2','状态(1=上架|0=下架)' ->setCellValue('A3','0' ->setCellValue('A4','1' ->setCellValue('B2','语言' ( => ->setCellValue('B'.(+3),['name' ->setCellValue('C2','分类名' ( => ->setCellValue('C'.(+3), ->setSheetState('hidden' ->setActiveSheetIndex(0 = Xlsx( = ->session->data['site_id'].'.xlsx' = DIR_UPLOAD.'excel_template/'. ->save( -> ( _validate(, = '' (!(['excel_file']) || (['excel_file' ->addError('excel_file','没有上传产品Excel文件' (!(['images_file']) || (['images_file' ->addError('images_file','没有上传产品图片文件' ->hasErrors() ? : 获取产品分类名称 _getCategoryName( 403 (!(404 ( => (!(['children' ->_getCategoryName(['children' } [] = ['name' } = 文件夹 _getDirFile( = (@ = ()) { 错误提示:) (( = ()) !== ( != ".." && != ".") { ((."/".)) { 文件夹,就进行递归 [] = ->_getDirFile(."/". } { 文件的名字存入数组; [] = ( 获取产品图片 _getImage(, = = = ->_getDirFile( (([ ([] => = ->uploadFile(.'/'..'/'.,'upload/images/'..'/'. (['success']==1 [] = 'name'=>, 'image'=>['info']['url' }

 

原文链接:https://www.f2er.com/php/403007.html

猜你在找的PHP相关文章