首先vendor下引入phpexcel
phpexcel\phpexcel\Classes\PHPExcel; (!('import_excel' import_excel( vendor('phpexcel.phpexcel.Classes.PHPExcel' (!(('no file!' = ( (, ( === 'csv' =(=("\r\n", ( => ( > 0[] = (",", = ('max_execution_time', '0' = PHPExcel_IOFactory::identify( = PHPExcel_IOFactory::createReader( = ->load( = -> = -> = PHPExcel_Cell::columnIndexFromString(-> = (=2;<=;++ = 0 (=0;<;++ = PHPExcel_Cell::stringFromColumnIndex( = ->getCell(.)-> ((++ [-2][]= ( == ([-2 (!('export_excel' export_excel(,,'phpexcel.phpexcel.Classes.PHPExcel'); = \PHPExcel(); = ->getActiveSheet(); ->setTitle(); = 'A' ( ->setCellValue(.'1', )->getStyle(.'1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::++ =2 ( = 'A' ( ->setCellValue(.,)->getStyle(.)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::->getColumnDimension()->setAutoSize(++++ = \PHPExcel_Writer_Excel2007( (); ("Pragma: public"("Expires: 0"("Cache-Control:must-revalidate,post-check=0,pre-check=0"("Content-Type:application/force-download"("Content-Type:application/vnd.ms-execl"("Content-Type:application/octet-stream"("Content-Type:application/download"('Content-Disposition:attachment;filename="'..'.xlsx"'("Content-Transfer-Encoding:binary"->save('php://output'
调用
= (request()-> = request()->(['0' = ->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel'=('\\','/',-> = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'excel' . DS. = import_excel(( = db('device')->field('id,device_no,name')-> = ["编号","设备序号","名称" = export_excel(,"test",();;
注意事项:
加载测试文件
下面主要判断Excel2007和Excel5类型,即xlsx/xlsm/xltx/xltm和xls/xlt格式文件
= PHPExcel_IOFactory::identify( ( !== "Excel5" && !== "Excel2007"() && str_alert(-1,"请确保导入的文件格式正确!" = PHPExcel_IOFactory::createReader( = ->load(( () && str_alert(-1,'加载文件发生错误:”'.(,PATHINFO_BASENAME).'”: '.->
获取工作表行数和列数
= -> = ->getHighestColumn();
注意:有时候你会发现你的表格明明有内容的行数就5行,但是获取到的$highestRow却有7,8行或者更多,这可能是因为你在操作你的表格的时候不小心点击了其它行数,虽然没有填写内容,但getHighestRow也是能够识别出行数;
想要获取有内容的行数应该使用getHighestDataRow和getHighestDataColumn,
源码注释是这样介绍的string Highest row number that contains data,即包含数据的字符串最高行数.
转换时间
获取表格中时间格式的内容,需要PHPExcel_Shared_Date::ExcelToPHP()来转换为php可识别的时间格式
('Y-m-d',PHPExcel_Shared_Date::ExcelToPHP();
或者在读取的时候进行处理
读出的时间是天数
excel 的日期是从 1900-01-01 开始计算的(php 是从 1970-01-01)
两者间有一个天数差 25569
时间是格林威治时间
所以有
= 25569 = 24 * 60 * 60 ('Y-m-d H:i:s', (41728.732916667 - ) * );
本文部分内容摘自:https://blog.csdn.net/weixin_36595597/article/details/81132472