column_str($key) . $columnnum; } public function export($list, $params = array()) { if (PHP_SAPI == 'cli') { die('This example should only be run from a Web Browser'); } //ob_end_clean(); require_once base_path() . '/vendor/phpoffice/phpexcel/Classes/PHPExcel.php'; $excel = new \PHPExcel(); $excel->getProperties()->setCreator("芸众商城")->setLastModifiedBy("芸众商城")->setTitle("Office 2007 XLSX Test Document")->setSubject("Office 2007 XLSX Test Document")->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")->setKeywords("office 2007 openxml php")->setCategory("report file"); $sheet = $excel->setActiveSheetIndex(0); $rownum = 1; foreach ($params['columns'] as $key => $column) { $sheet->setCellValue($this->column($key, $rownum), $column['title']); if (!empty($column['width'])) { $sheet->getColumnDimension($this->column_str($key))->setWidth($column['width']); } } $rownum++; foreach ($list as $row) { $len = count($params['columns']); for ($i = 0; $i < $len; $i++) { $value = $row[$params['columns'][$i]['field']]; $value = @iconv("utf-8", "gbk", $value); $value = @iconv("gbk", "utf-8", $value); $sheet->setCellValueExplicit($this->column($i, $rownum), $value, \PHPExcel_Cell_DataType::TYPE_STRING); } $rownum++; } $excel->getActiveSheet()->setTitle($params['title']); $filename = $params['title'] . '-' . date('Y-m-d H:i', time()); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $filename . '.xls"'); header('Cache-Control: max-age=0'); $writer = \PHPExcel_IOFactory::createWriter($excel, 'Excel5'); $writer->save('php://output'); exit; } }