1. 程式人生 > >php實現excel單元格合並,字體加粗居中等操作

php實現excel單元格合並,字體加粗居中等操作

src eight tle osi dex modified 內容 type hdr

使用的是phpexcel,基本用的原生語法,所見即所得,直接復制下面代碼,即可:

// 引用phpexcel類
            $this->load->library(‘PHPExcel‘);

            // 創建對象
            $objPHPExcel = new PHPExcel();

            // 顯示錯誤信息
            error_reporting(E_ALL);

            // Set properties
            $objPHPExcel->getProperties()->setCreator("[email protected]")
                
->setLastModifiedBy("[email protected]") ->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("Test result file"); // 字體和樣式 $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(12); //字體大小 $objPHPExcel->getActiveSheet()->getStyle(‘A2:D2‘)->getFont()->setBold(false); //第二行是否加粗 $objPHPExcel
->getActiveSheet()->getStyle(‘A1‘)->getFont()->setBold(true); //第一行是否加粗 $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setSize(16); //第一行字體大小 $objPHPExcel->getActiveSheet()->getStyle(‘A2‘)->getFont()->setSize(14); //第二行字體大小 $objPHPExcel->getActiveSheet()->getStyle(‘A6‘)->getFont()->setSize(14); //第六行字體大小 $objPHPExcel->getActiveSheet()->getStyle(‘A11‘)->getFont()->setSize(14); //第十一行字體大小 // 設置垂直居中 $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle(‘A2:D2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 設置水平居中 $objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle(‘A3:G5‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle(‘A7:G10‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle(‘A12:B15‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //設置左對齊 $objPHPExcel->getActiveSheet()->getStyle(‘A2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle(‘A6‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); $objPHPExcel->getActiveSheet()->getStyle(‘A11‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); // 設置行高度 $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20); //設置默認行高 $objPHPExcel->getActiveSheet()->getRowDimension(‘1‘)->setRowHeight(30); //第一行行高 //設置單元格寬度 $objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(20); $objPHPExcel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(20); // 合並 $objPHPExcel->getActiveSheet()->mergeCells(‘A1:G1‘); $objPHPExcel->getActiveSheet()->mergeCells(‘A2:G2‘); $objPHPExcel->getActiveSheet()->mergeCells(‘A6:G6‘); $objPHPExcel->getActiveSheet()->mergeCells(‘A11:B11‘); // 設置內容 $objPHPExcel->setActiveSheetIndex(0) ->setCellValue(‘A1‘, ‘利潤核算統計‘) ->setCellValue(‘A2‘, ‘【用戶充值】‘) ->setCellValue(‘A3‘, ‘費率‘) ->setCellValue(‘B3‘, ‘筆數 ‘) ->setCellValue(‘C3‘, ‘充值進賬‘) ->setCellValue(‘D3‘, ‘手續費‘) ->setCellValue(‘E3‘, ‘利潤‘) ->setCellValue(‘A4‘, ‘1.0%‘) ->setCellValue(‘B4‘, $pay_log_list[‘aa‘]) ->setCellValue(‘C4‘, $pay_log_list[‘bb‘]) ->setCellValue(‘D4‘, $pay_log_list[‘cc‘]) ->setCellValue(‘E4‘, $pay_log_list[‘dd‘]) ->setCellValue(‘A5‘, ‘合計‘) ->setCellValue(‘B5‘, $pay_log_list[‘ee‘]) ->setCellValue(‘C5‘, $pay_log_list[‘ff‘]) ->setCellValue(‘D5‘, $pay_log_list[‘gg‘]) ->setCellValue(‘E5‘, $pay_log_list[‘hh‘]) ->setCellValue(‘A6‘, ‘【用戶提現】‘) ->setCellValue(‘A7‘, ‘類別‘) ->setCellValue(‘B7‘, ‘筆數‘) ->setCellValue(‘C7‘, ‘申請提現‘) ->setCellValue(‘D7‘, ‘手續費‘) ->setCellValue(‘E7‘, ‘快錢提現‘) ->setCellValue(‘F7‘, ‘手續費‘) ->setCellValue(‘G7‘, ‘利潤‘) ->setCellValue(‘A8‘, ‘收取手續費‘) ->setCellValue(‘B8‘, $withdraw_list[‘ii‘]) ->setCellValue(‘C8‘, $withdraw_list[‘kk‘]) ->setCellValue(‘D8‘, $withdraw_list[‘ll‘]) ->setCellValue(‘E8‘, $withdraw_list[‘mm‘]) ->setCellValue(‘F8‘, $withdraw_list[‘nn‘]) ->setCellValue(‘G8‘, $withdraw_list[‘oo‘]) ->setCellValue(‘A9‘, ‘未收取手續費‘) ->setCellValue(‘B9‘, $withdraw_list[‘pp‘]) ->setCellValue(‘C9‘, $withdraw_list[‘qq‘]) ->setCellValue(‘D9‘, $withdraw_list[‘rr‘]) ->setCellValue(‘E9‘, $withdraw_list[‘ss‘]) ->setCellValue(‘F9‘, $withdraw_list[‘tt‘]) ->setCellValue(‘G9‘, $withdraw_list[‘ww‘]) ->setCellValue(‘A10‘, ‘合計‘) ->setCellValue(‘B10‘, $withdraw_list[‘uu‘]) ->setCellValue(‘C10‘, $withdraw_list[‘vv‘]) ->setCellValue(‘D10‘, $withdraw_list[‘xx‘]) ->setCellValue(‘E10‘, $withdraw_list[‘yy‘]) ->setCellValue(‘F10‘, $withdraw_list[‘zz‘]) ->setCellValue(‘G10‘, $withdraw_list[‘aaa‘]) ->setCellValue(‘A11‘, ‘【利潤匯總】‘) ->setCellValue(‘A12‘, ‘充值手續費收‘) ->setCellValue(‘B12‘, $all_summary[‘bbb‘]) ->setCellValue(‘A13‘, ‘提現手續費支‘) ->setCellValue(‘B13‘, $all_summary[‘ccc‘]) ->setCellValue(‘A14‘, ‘快錢提現手續費支‘) ->setCellValue(‘B14‘, $all_summary[‘ddd‘]) ->setCellValue(‘A15‘, ‘利潤‘) ->setCellValue(‘B15‘, $all_summary[‘eee‘]); // Rename sheet $objPHPExcel->getActiveSheet()->setTitle(‘利潤核算表‘); // Set active sheet index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setActiveSheetIndex(0); // 設置輸出 $tableName = ‘利潤核算表【‘.$st.‘--‘.$et.‘】‘; header(‘Content-Type: application/vnd.ms-excel‘); header(‘Content-Disposition: attachment;filename="‘ .$tableName. ‘.xls"‘); header(‘Cache-Control: max-age=0‘); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘); $objWriter->save(‘php://output‘); exit;

基本效果展示,視圖頁面樣式:

技術分享圖片

下載excel後,效果展示:

技術分享圖片

轉 : https://www.cnblogs.com/qwgshare/p/9782889.html

php實現excel單元格合並,字體加粗居中等操作