ThinkPHP6ÖÐÔõÑù¾ÙÐÐExcelµ¼ÈëºÍµ¼³ö£¿
Ëæ×Å»¥ÁªÍøÓ¦ÓÃÈÕÒæÆÕ¼°£¬ÒÔ¼°ÖÖÖÖÊý¾ÝÖ®¼äµÄÏ໥½»»¥ºÍÊý¾ÝתÒÆÐèÇóµÄÒ»Ö±ÔöÌí£¬ÎļþÃûÌõÄת»»ºÍµ¼Èëµ¼³öÒѳÉΪ³£¼ûÐèÇóÖ®Ò»¡£ÔÚwebÓ¦ÓóÌÐòÖУ¬µ¼ÈëºÍµ¼³öexcelÎļþÊÇÒ»Ñùƽ³£ÊÂÇéÖг£¼ûµÄÊý¾Ý½»“Q·½·¨¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6¿ò¼ÜÖÐʵÏÖexcelµ¼Èëµ¼³ö¹¦Ð§¡£
Ò»¡¢ ThinkPHP6¿ò¼ÜÏÈÈÝ
ThinkPHP6ÊÇÒ»¸ö×ÔÓÉ¡¢¿ªÔ´¡¢¿ìËÙ¡¢¼òÆÓµÄÃæÏò¹¤¾ßµÄÇáÁ¿¼¶PHP¿ò¼Ü¡£Ëü¾ßÓиßÐÔÄÜ¡¢¸»ºñµÄ¹¦Ð§¡¢ÎÞаµÄÉèÖá¢Ç¿Ê¢µÄÀ©Õ¹ÐÔµÈÌص㣬ÒѾ³ÉΪPHPÓ¦Óÿª·¢ÁìÓòµÄÒ»¸öÈÈÃÅ¿ò¼Ü¡£
¶þ¡¢Excelµ¼³ö
ʾÀý´úÂë
<?php namespace appindexcontroller; use PHPExcel; use PHPExcel_IOFactory; class ExcelExport { public function export() { $objPHPExcel = new PHPExcel(); // ÉèÖÃÄ¿½ñÔ˶¯sheet $objPHPExcel->setActiveSheetIndex(0); // ÉèÖÃÎÊÌâ $objPHPExcel->getActiveSheet()->setTitle('ѧÉúЧ¹û'); // ÉèÖñíÍ· $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ѧºÅ'); $objPHPExcel->getActiveSheet()->setCellValue('B1', 'ÐÕÃû'); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'ÓïÎÄ'); $objPHPExcel->getActiveSheet()->setCellValue('D1', 'Êýѧ'); $objPHPExcel->getActiveSheet()->setCellValue('E1', 'Ó¢Óï'); $objPHPExcel->getActiveSheet()->setCellValue('F1', '×Ü·Ö'); // ÉèÖÃÊý¾Ý $data = [ ['1001', 'ÕÅÈý', '85', '90', '88', '263'], ['1002', 'ÀîËÄ', '90', '88', '90', '268'], ['1003', 'ÍõÎå', '92', '87', '91', '270'] ]; $row = 2; foreach ($data as $val) { $objPHPExcel->getActiveSheet()->setCellValue('A' . $row, $val[0]); $objPHPExcel->getActiveSheet()->setCellValue('B' . $row, $val[1]); $objPHPExcel->getActiveSheet()->setCellValue('C' . $row, $val[2]); $objPHPExcel->getActiveSheet()->setCellValue('D' . $row, $val[3]); $objPHPExcel->getActiveSheet()->setCellValue('E' . $row, $val[4]); $objPHPExcel->getActiveSheet()->setCellValue('F' . $row, $val[5]); $row++; } // ÉèÖÃÁпí $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15); // µ¼³öExcel header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="ѧÉúЧ¹û.xlsx"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output'); exit; } }
µÇ¼ºó¸´ÖÆ
˵Ã÷
ÉÏÃæµÄʾÀý´úÂëÑÝʾÁËÔõÑùʹÓÃPHPExcel¿âʵÏÖExcelµ¼³ö¡£PHPExcel¿âÊÇÒ»¸öPHP¶ÁдExcelÎļþµÄ¿ªÔ´¿â£¬¹¦Ð§Ç¿Ê¢£¬²Ù×÷¼òÆÓ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ê×ÏÈ£¬ÎÒÃǽ¨ÉèÁËÒ»¸öPHPExcelʵÀý£¬È»ºóÉèÖÃÄ¿½ñÔ˶¯sheet£¬²¢ÉèÖÃÁËÎÊÌâºÍ±íÍ·¡£Ö®ºó£¬ÎÒÃÇÉèÖÃÊý¾Ý²¢¶ÔÁпí¾ÙÐÐÁËÉèÖá£×îºó£¬ÎÒÃÇʹÓÃheaderº¯ÊýÉèÖÃÁ˵¼³öExcelµÄÄÚÈÝÀàÐÍ¡¢ÎļþÃû»ººÍ´æ¿ØÖÆ£¬È»ºóʹÓÃPHPExcel_IOFactoryÀàµÄcreateWriterÒªÁ콨ÉèÒ»¸öExcel2007ÃûÌõÄдÈëÆ÷£¬²¢½«Êý¾ÝÁ÷Êä³öµ½ä¯ÀÀÆ÷¡£
Èý¡¢Excelµ¼Èë
ʾÀý´úÂë
<?php namespace appindexcontroller; use PHPExcel; use PHPExcel_IOFactory; class ExcelImport { public function import() { if (empty($_FILES['file']['tmp_name'])) { echo 'ÇëÑ¡ÔñÎļþ£¡'; exit; } $reader = PHPExcel_IOFactory::createReader('Excel2007'); $PHPExcel = $reader->load($_FILES['file']['tmp_name']); $sheet = $PHPExcel->getSheet(0); // »ñµÃµÚ1¸öÊÂÇé±í $highestRow = $sheet->getHighestRow(); // È¡µÃ×ÜÐÐÊý $highestColumn = $sheet->getHighestColumn(); // È¡µÃ×ÜÁÐÊý $data = []; for ($row = 2; $row <= $highestRow; $row++) { $rowData = []; for ($col = 'A'; $col <= $highestColumn; $col++) { $rowData[$col] = (string)$sheet->getCell($col . $row)->getValue(); } $data[] = $rowData; } var_dump($data); } }
µÇ¼ºó¸´ÖÆ
˵Ã÷
ÉÏÃæµÄʾÀý´úÂëÑÝʾÁËÔõÑùʹÓÃPHPExcel¿âʵÏÖExcelµ¼Èë¡£Ê×ÏÈ£¬ÎÒÃÇÅжϿͻ§¶ËÊÇ·ñÉÏ´«ÁËÎļþ£¬²¢Ê¹ÓÃPHPExcel_IOFactoryÀàµÄcreateReaderÒªÁ콨ÉèÒ»¸öExcel2007ÃûÌõĶÁÈ¡Æ÷¡£È»ºó£¬ÎÒÃÇʹÓÃ$readerµÄloadÒªÁ콫ExcelÎļþ¶ÁÈëÄڴ棬²¢»ñÈ¡ÆäÖеĵÚÒ»¸öÊÂÇé±í¡£½ÓÏÂÀ´£¬ÎÒÃÇͨ¹ý×î¸ßÐкÍ×î¸ßÁеÄË÷Òýֵѻ·±éÀúExcel±í¸ñ£¬½«Ã¿ÐÐÊý¾ÝÒÔ¹ØÁªÊý×éµÄÐÎʽ´æ´¢µ½$data±äÁ¿ÖУ¬²¢×îÖÕÊä³ö¸Ã±äÁ¿¡£
ËÄ¡¢×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃThinkPHP6¿ò¼ÜÍŽáPHPExcel¿âʵÏÖExcelÎļþµÄµ¼Èëµ¼³ö¹¦Ð§£¬ÕâΪ×ðÁú¿Ê±WebÓ¦ÓóÌÐò¿ª·¢ÌṩÁ˱㵱ºÍÖ§³Ö¡£ÎÒÃÇ¿ÉÒÔƾ֤ÏÖʵµÄÐèÇó£¬ÎÞаÔËÓÃPHPExcel¿âµÄÖÖÖÖÒªÁìºÍÊôÐÔ£¬À´ÊµÏÖÎļþÃûÌý»Á÷£¬²¢¸üºÃµØЧÀÍÓÚ×ðÁú¿Ê±Ó¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐExcelµ¼ÈëºÍµ¼³ö£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡