php匯出excel的
[PHP]代碼
- vendor("PHPExcel176.PHPExcel");
- // 建立新的PHPExcel 物件
- $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("ctos")
- ->setLastModifiedBy("ctos")
- ->setTitle("Office 2007 XLSX 測試文件") - "Office 2007 XLSX 測試文檔")
- ->setDescription("Office 2007 XLSX 測試文檔,使用PHP 類別產生。")
- ->setKeywords("office 2007 openxml php")
- ->setKeywords("office 2007 openxml php")
- - "測試結果檔案");
- //設定寬度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
- $objPHPExcel->getActiveSheet()- >getColumnDimension('B')->setWidth(80);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->getColumnDimension('C')-> ;setWidth(80);
- $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);
- $objPHPExcel->getActiveSheet()->getColumnDimension( 'E')->setWidth(80); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
- //設定行高度
- //$objPHPExcel->getActiveSheet()->getRowDimension('1' )->setRowHeight(22);
- //$objPHPExcel->getActiveSheet()->getRowDimension('2') ->setRowHeight(20);
- //設定字體大小粗體
- $objPHPExcel ->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
- //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()- >setBold(true);
- //$objPHPExcel->getActiveSheet()- >getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);getActiveSheet()->getStyle('A2:F2' )->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
- //設定居中水平getActiveSheet()->getStyle( 'A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet( >getAlignment()-> setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->Alignment()->Alzole_ccel HORIZONTAL_CENTER);
- $objPHPExcel ->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONLTALex_CEND; >getStyle('F ')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- //mergecell
- //$objPHPExcel->getActiveSheet()-1:Cege F1') ;
- //設定字體大小粗體
- //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
- //$objPHPExcel ->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
- //$objPHPExcel->getActiveSheet()->getStyle(' A2:F2')- >getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders ()->getAllBs( PHPExcel_Style_Border::BORDER_THIN);?????????
- $objPHPExcel->getActiveSheet()->getStyle('A1')- >getAlignment()->setHorizontal(PHPExcel_Style_Alignment()->setHorizontal(PHPExcel_Style_Aligny-CENA)130_PExcel. ;
- $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment:: HORIZONTAL_CENTER);
- $ ('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet( )->getStyle('D')->getAlignment()->getStyle('D')->getAlignment()->getStyle('D')->getAlignment()->getStyle('D')->getAlignment()->getStyle('D')->getAlignment()->getStyle('D')->getAlignment()> ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F')->getStyle('F')-> getAlignment()->setHorizonimtal( HORIZONTAL_CENTER);
- //???cell
- $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
- / / 設定表頭內容
- $objeExkcel (0)
- ->setCellValue('A1', iconv("utf-8","gb2312","訂單資料總結時間:'.date ('Y-m-d H:i:s')"))
- ->setCellValue('A2', iconv("utf-8","gb2312","訂單ID"))
- ->setCellValue('B2', iconv("utf-8","gb2312","下單人"))
- ->setCellValue('C2', iconv("utf-8","gb2312","客戶名稱"))
- ->setCellValue('D2', iconv("utf -8","gb2312","下單時間"))
- ->setCellValue('E2', iconv("utf-8","gb2312","需求機型"))
- -> setCellValue('F2', iconv("utf-8","gb2312","需求數量"));
- // Miscellaneous glyphs, UTF-8
- for($i=0;$i $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i 3), iconv("utf-8","gb2312",$OrdersData [$i]['id']));
- $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i 3), iconv("utf-8","gb2312",$ OrdersData[$i]['reid']));
- $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i 3), iconv("utf-8","gb2312", $OrdersData[$i]['topid']));
- //$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i 3), iconv('utf-8',' gb2312',toDate($OrdersData[$i]['typename']))); //?????????common.php??????
- $objPHPExcel->getActiveSheet(0 )->setCellValue('D'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['typename']));
- $objPHPExcel->getActiveSheet( 0)->setCellValue('E'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['typelink']));
- $objPHPExcel->getActiveSheet (0)->setCellValue('F'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['keyword']));
- //$objPHPExcel ->getActiveSheet()->getStyle('A'.($i 3).':F'.($i 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
- // $objPHPExcel->getActiveSheet()->getStyle('A'.($i 3).':F'.($i 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN );
- //$objPHPExcel->getActiveSheet()->getRowDimension($i 3)->setRowHeight(16);
- }
- // sheet
- $objPHPExcel->getActiveSheet()- >setTitle( iconv("utf-8","gb2312",'訂單總表'));
- // Set active sheet index to the first sheet, so Excel opens this as the first sheet
- $objPHPExcel ->setActiveSheetIndex(0);
- // excel頭參數
- header('Content-Type: application/vnd.ms-excel; charset=utf-8');
- header('Content-Type : application/force-download');
- header('Content-Type: application/octet-stream');
- header('Content-Type: application/download');
- //header( 'Content-type:charset=utf-8);
- //header('Content-Transfer-Encoding: utf-8');
- header('Content-Disposition: attachment;filename='.iconv( "utf-8","gb2312","訂單匯總".date('Ymd-His').".xls")); //日期為檔案名稱後綴
- //header('Cache-Control: max-age=0');
- $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'excel5'); //excel5為xls格式,excel2007為xlsx格式
- $objWriter->save('php: //output');
複製程式碼
|