您好,登錄后才能下訂單哦!
<?php namespace app\excel; use PHPExcel; use PHPExcel_IOFactory; class ExcelMultiSheetHelper { /** * 多表單數據導出 * @param $data * $data = [ * [ * 'title' => 'sheet1', * 'list' => * [ * ['name' => 'sheet1_line1_name', 'item' => 'sheet1_line1_item'], * ['name' => 'sheet1_line2_name', 'item' => 'sheet1_line1_item'] * ], * 'column' => * [ * 'A' => ['title' => 'title_name', 'field' => 'name'], * 'B' => ['title' => 'title_item', 'field' => 'item'] * ], * ], * [ * 'title' => 'sheet2', * 'list' => [ * ['name' => 'sheet2_line1_name', 'item' => 'sheet2_line1_item'], * ['name' => 'sheet2_line2_name', 'item' => 'sheet2_line1_item'] * ], * 'column' => * [ * 'A' => ['title' => 'title_name', 'field' => 'name'], * 'B' => ['title' => 'title_item', 'field' => 'item'] * ], * ], * ]; * @param string $fileName */ public static function exportToExcel($data, $fileName = '數據導出') { $objPHPExcel = new PHPExcel(); //實例化Excel類 foreach ($data as $key => $value) { $objPHPExcel->createSheet($key); $objPHPExcel->setActiveSheetIndex($key); $activeSheet = $objPHPExcel->getactivesheet(); $activeSheet->setTitle($value['title']); $column = $value['column']; $list = $value['list']; foreach ($list as $kk => $vv) { $line = $kk + 2; foreach ($column as $kkk => $vvv) { $title = $vvv['title']; $field = $vvv['field']; $activeSheet->getColumnDimension($kkk)->setWidth(15); //設置單元格寬 $fieldData = !empty($vv[$field]) ? $vv[$field] : ''; $activeSheet->getStyle($kkk . 1)->getFont()->setName('宋體')->setBold(true); //字體加粗; //字體 $activeSheet->setcellvalue($kkk . 1, $title);//第A列 第1行,標題 $activeSheet->setcellvalue($kkk . $line, $fieldData);//第A列 第$k行 } } } if ($objPHPExcel->getSheetCount() > 1) { $objPHPExcel->removeSheetByIndex($objPHPExcel->getSheetCount() - 1); //刪除模板sheet } //最后通過瀏覽器輸出 static:: responseExcelFile($fileName, $objPHPExcel, 'Excel5'); exit; } /** * 最后通過瀏覽器輸出 * @param $fileName * @param $objPHPExcel * @param string $excelVersion */ public static function responseExcelFile($fileName, $objPHPExcel, $excelVersion = 'Excel5') { if (static:: isIE()) {//解決文件名含中文時下載亂碼問題。測試瀏覽器:IE8\IE11\Chrome41\Firefox38 $fileName = urlencode($fileName); } $fileName .= $excelVersion === 'Excel5' ? '.xls' : '.xlsx'; ob_end_clean(); ob_start(); //添加fileDownload cookie便于jquery.fileDownload.js判斷下載狀態 header('Set-Cookie: fileDownload=true; path=/'); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $fileName . '"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $excelVersion); $objWriter->save('php://output'); } /** * 判斷當前請求瀏覽器是否IE * @return bool */ private static function isIE() { $u_agent = $_SERVER['HTTP_USER_AGENT']; return (preg_match('/MSIE/i', $u_agent) || preg_match('/Trident/i', $u_agent)) && !preg_match('/Opera/i', $u_agent); } }
參考博客
https://www.cnblogs.com/xiaoli3007/p/5315595.html
http://www.thinkphp.cn/topic/3739.html
http://www.thinkphp.cn/code/1893.html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。