php 导入导出 excel 工具:PHPExcel
PHPExcel 下载地址:
https://github.com/PHPOffice/PHPExcel
git clone https://github.com/PHPOffice/PHPExcel.git
使用方法
<?php class ExcelToArrary { public function __construct() { /*导入phpExcel核心类 注意 :你的路径跟我不一样就不能直接复制*/ include_once './Classes/PHPExcel.php'; } /* 导出excel函数*/ public function push($data, $name = 'Excel') { error_reporting(E_ALL); date_default_timezone_set('Europe/London'); $objPHPExcel = new PHPExcel(); /*以下是一些设置 ,什么作者 标题啊之类的*/ $objPHPExcel->getProperties()->setCreator(iconv("UTF-8", "GBK", "转弯的阳光")) ->setLastModifiedBy("转弯的阳光") ->setTitle("数据EXCEL导出") ->setSubject("数据EXCEL导出") ->setDescription("备份数据") ->setKeywords("excel") ->setCategory("result file"); /*以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/ foreach ($data as $k => $v) { $num = $k + 1; $objPHPExcel->setActiveSheetIndex(0) //Excel的第A列,uid是你查出数组的键值,下面以此类推 ->setCellValue('A' . $num, $v['uid']) ->setCellValue('B' . $num, $v['email']) ->setCellValue('C' . $num, $v['password']); } $objPHPExcel->getActiveSheet()->setTitle('User'); $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara'); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(20); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $objPHPExcel->getActiveSheet()->getStyle('A1')->getBorders()->getLeft()->getColor()->setARGB('FF993300'); $objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); $objPHPExcel->setActiveSheetIndex(0); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $name . '.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit; } } $info = new ExcelToArrary(); $data = [ ['uid' => 1, 'email' => '[email protected]', 'password' => 'md5'], ['uid' => 2, 'email' => '[email protected]', 'password' => 'md5'], ['uid' => 3, 'email' => '[email protected]', 'password' => 'md5'], ['uid' => 4, 'email' => '[email protected]', 'password' => 'md5'], ['uid' => 4, 'email' => '[email protected]', 'password' => 'md5'], ['uid' => 4, 'email' => '[email protected]', 'password' => 'md5'], ]; $info->push($data);
参考: