error_reporting(0); require_once 'phpexcel/PHPExcel.php'; require_once 'phpexcel/PHPExcel/IOFactory.php'; $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("BillHong") ->setLastModifiedBy("BillHong")//創建人 ->setTitle("export to Borwser")//標題 ->setSubject("export to Borwser")//題目 ->setDescription("export to Borwser")//描述 ->setKeywords("export to Borwser")//關鍵字 ->setCategory("BillHong export to Borwser");//種類 /*設置單元格的標題*/ $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A1:G1');//合並單元格 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', '杭州佳和電氣有限公司南京分公司用電量周報表'); //設置font $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getFont()->setSize(14); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getFont()->setBold(true); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE); //水平居中===垂直居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->mergeCells('A2:G2');//合並單元格 $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2', '計量時間段2012-03-18 00:00:00--2012-03-25 00:00:00'); //設置font $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->getFont()->setSize(10); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE); //水平居中===垂直居中 $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle('A2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); /*設置特別表格的寬度*/ $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('A')->setWidth(5);//表示設置A這一列的寬度,以下一樣 $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('B')->setWidth(40); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('C')->setWidth(8); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('D')->setWidth(5); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('E')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('F')->setWidth(20); $objPHPExcel->setActiveSheetIndex(0)->getColumnDimension('G')->setWidth(10); /*設置第3行的內容居中,字體顏色等*/ for ($i = 0; $i <= 6; $i++) { $n = 65; $col = chr($n+$i).'3'; $objPHPExcel->setActiveSheetIndex(0)->getStyle($col)->getFont()->setSize(11); $objPHPExcel->setActiveSheetIndex(0)->getStyle($col)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE); $objPHPExcel->setActiveSheetIndex(0)->getStyle($col)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->setActiveSheetIndex(0)->getStyle($col)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); } // 以下實現生成Excel單元格坐標。感謝php緣來緣去的Q群大力技術支持 $abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $firstChar = 0; $flag = false; // 是否已經變成AA模式。 $d = 65; $titleLength = count($titles[1]); for ($i = 0; $i < $titleLength; $i++) { // 當已經超過'Z'字母的時候。 if ($d == 91) { $d = 65; // 又將$d設置為初始化值65。 if ($flag) { $firstChar++; $offset = $abc{$firstChar} . chr($d); } else { $flag = true; $offset = $abc{$firstChar} . chr($d); } } // 當循環沒有超過'Z'的時候。 else { if ($flag) { $offset = $abc{$firstChar} . chr($d); } else { $offset = chr($d); } } // 設置單元格的值。 $offsets = $offset . '3'; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($offsets, $titles[1][$i]); $d++; } // 以下按照對應坐標將值放進去。以下實現生成Excel單元格坐標。 $k = 4; foreach ($rowset as $rows) { $abc_ = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $firstChar_ = 0; $flag_ = false; $d_ = 65; $rowsetLength = count($rowset); // 回調函數。 if($backfunc) { $rows = $backfunc($rows); } $rowsLength = count($rows); for ($a = 0; $a < $rowsLength; $a++) { // 當已經超過'Z'字母的時候。 if ($d_ == 91) { $d_ = 65; // 又將$d設置為初始化值65。 if ($flag_) { $firstChar_++; $offset_ = $abc_{$firstChar_} . chr($d_); } else { $flag_ = true; $offset_ = $abc_{$firstChar_} . chr($d_); } } // 當循環沒有超過'Z'的時候。 else { if ($flag_) { $offset_ = $abc_{$firstChar_} . chr($d_); } else { $offset_ = chr($d_); } } $offsets_ = $offset_ . $k; $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($offsets_, $rows[$a]); $d_++; } $k++; } $objPHPExcel->getActiveSheet()->setTitle('zenlove test'); $objPHPExcel->setActiveSheetIndex(0); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); header("Content-Disposition:attachment;filename=$filename.xls"); header("Content-Type:application/octet-stream"); header("Content-Transfer-Encoding:binary"); header("Pragma:no-cache"); $objWriter->save('php://output'); exit();