如圖 最近要做一個這樣的導出,一個團長對應一個sheet,然后一鍵導出
使用PhpOffice\PhpSpreadsheet實現 直接上代碼
/** * 多sheet的導出 * @author bwy <xxx@xxx.com> * @param [type] $data_array * @return void */ public function xtexport($data_array) { $name = '團長' . date("Y-m-d", time()); $spreadsheet = new Spreadsheet(); foreach ($data_array as $key => $data) { $this->opSheet($spreadsheet,$key,$data); } header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $name . '.xlsx"'); header('Cache-Control: max-age=0'); $writer = new Xlsx($spreadsheet); $writer->save('php://output'); //刪除清空: $spreadsheet->disconnectWorksheets(); unset($spreadsheet); exit; }
然后就是處理sheet的多循環
/** * 處理多sheet * @author bwy <xxx@xxx.com> * @param [type] $spreadsheet * @param [type] $n * @param [type] $data * @return void */ public function opSheet($spreadsheet,$n, $data) { $spreadsheet->createSheet();//創建sheet $objActSheet = $spreadsheet->setActiveSheetIndex($n);//設置當前的活動sheet $keys = $data['rows'][0];//這是你的數據鍵名 $count = count($keys);//計算你所占的列數 $infoNum = ceil(count($data['info']) / 2);//求k-v值的所占行數 $infoStart = $infoNum + 2 ;//下面的詳細信息的開始行數 $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'); $sheet = $spreadsheet->getActiveSheet($n)->setTitle($data['info']['社區團長姓名:']);//設置sheet的名稱 $spreadsheet->getActiveSheet($n)->mergeCells('A1:' . $cellName[$count - 1] . '1'); //合並單元格 $spreadsheet->getActiveSheet($n)->getStyle('A1')->getFont()->setSize(20); //設置title的字體大小 $spreadsheet->getActiveSheet($n)->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('C')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('E')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle("$infoStart")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中 $spreadsheet->getActiveSheet($n)->getStyle("$infoStart")->getFont()->setBold(true); //標題欄加粗 $objActSheet->setCellValue('A1', $data['title']); //設置每個sheet中的名稱title /** * 圖中最下面的數據信息循環 */ foreach ($data['rows'] as $key => $item) { //循環設置單元格: //$key+$infoStart,因為第一行是表頭,所以寫到表格時 從第數據行開始寫 for ($i = 65; $i < $count + 65; $i++) { //數字轉字母從65開始: //$sheet->setCellValue(strtoupper(chr($i)) . ($key + "$infoStart"), $item[[$keys][$i - 65]]); $sheet->setCellValue(strtoupper(chr($i)) . ($key+"$infoStart"), $item[$i - 65]); $spreadsheet->getActiveSheet($n)->getColumnDimension(strtoupper(chr($i)))->setWidth(20); //固定列寬 } } /** * 處理圖中的中間區 團長名稱之類的 */ $rowNumber = 1; $infoIndex = 0; foreach ($data['info'] as $key => $value) { if ($infoIndex % 2 == 0) { $rowNumber++; $infoCellName1 = 'A' . $rowNumber; $infoCellMegreRange = 'B' . $rowNumber . ':C' . $rowNumber; $infoCellName2 = 'B' . $rowNumber; } else { $infoCellName1 = 'D' . $rowNumber; $infoCellMegreRange = 'E' . $rowNumber . ':F' . $rowNumber; $infoCellName2 = 'E' . $rowNumber; } $spreadsheet->setActiveSheetIndex($n)->setCellValue($infoCellName1, $key); $spreadsheet->getActiveSheet($n)->mergeCells($infoCellMegreRange); $spreadsheet->setActiveSheetIndex($n)->setCellValue($infoCellName2, $value); $infoIndex++; } }
每個人的數據格式不一致,所以自行改一改,我的數據格式是這樣可以參考下
public function xtexport() { $data = array( array( 'title' => 'AAA', 'info' => [ '社區團長店鋪地址:' => 'AAA', '社區團長姓名:' => 'AAA', '收貨地址:' => 'AAA', '聯系方式:' => 'AAA', '配送日期:' => 'AAA', '手打阿達:' => 'AAA', '手動蝶閥:' => 'AAA' ], 'rows' => [ [ 'id', 'name', 'sex', 'phone', 'res', 'aaa', 'bbb' ], [ 1111,2222,3333,4444,5555,6666,7777 ], [ 111,222,333,444,555,666,777 ] ] ), array( 'title' => 'BBBB', 'info' => [ '社區團長店鋪地址:' => 'BBBB', '社區團長姓名:' => 'BBBB', '收貨地址:' => 'BBBB', '聯系方式:' => 'BBBB', '配送日期:' => 'BBBB', '手打阿達:' => 'BBBB', '手動蝶閥:' => 'BBBB' ], 'rows' => [ [ 'id', 'name', 'sex', 'phone', 'res', 'aaa', 'bbb' ], [ 1,2,3,4,5,6,7 ], [ 11,22,33,44,55,66,77 ] ] ) ); model('Excel')->xtexport($data); }
導出如圖:
好了 以上就是我的Excel多sheet導出,有幫助的可以點個贊哈哈