php使用PHPExcel匯出excel程式碼

WBOY
發布: 2016-07-25 08:44:20
原創
865 人瀏覽過
  1. vendor("PHPExcel176.PHPExcel");
  2. // 建立新的PHPExcel 物件
  3. $objPHPExcel = new PHPExcel(); $objPHPExcel->getProperties()->setCreator("ctos")
  4. ->setLastModifiedBy("ctos")
  5. ->setTitle("Office 2007 XLSX 測試文件") - "Office 2007 XLSX 測試文檔")
  6. ->setDescription("Office 2007 XLSX 測試文檔,使用PHP 類別產生。")
  7. ->setKeywords("office 2007 openxml php")
  8. ->setKeywords("office 2007 openxml php")
  9. - "測試結果檔案");
  10. //設定寬度
  11. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
  12. $objPHPExcel->getActiveSheet()- >getColumnDimension('B')->setWidth(80);
  13. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->getColumnDimension('C')-> ;setWidth(80);
  14. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);
  15. $objPHPExcel->getActiveSheet()->getColumnDimension( 'E')->setWidth(80); $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
  16. //設定行高度
  17. //$objPHPExcel->getActiveSheet()->getRowDimension('1' )->setRowHeight(22);
  18. //$objPHPExcel->getActiveSheet()->getRowDimension('2') ->setRowHeight(20);
  19. //設定字體大小粗體
  20. $objPHPExcel ->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
  21. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()- >setBold(true);
  22. //$objPHPExcel->getActiveSheet()- >getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);getActiveSheet()->getStyle('A2:F2' )->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  23. //設定居中水平getActiveSheet()->getStyle( 'A1')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  24. $objPHPExcel->getActiveSheet( >getAlignment()-> setHorizo​​​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  25. $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->Alignment()->Alzole_ccel HORIZONTAL_CENTER);
  26. $objPHPExcel ->getActiveSheet()->getStyle('D')->getAlignment()->setHorizo​​​​ntal(PHPExcel_Style_Alignment::HORIZONLTALex_CEND; >getStyle('F ')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  27. //mergecell
  28. //$objPHPExcel->getActiveSheet()-1:Cege F1') ;
  29. //設定字體大小粗體
  30. //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
  31. //$objPHPExcel ->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
  32. //$objPHPExcel->getActiveSheet()->getStyle(' A2:F2')- >getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  33. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders ()->getAllBs( PHPExcel_Style_Border::BORDER_THIN);?????????
  34. $objPHPExcel->getActiveSheet()->getStyle('A1')- >getAlignment()->setHorizo​​​​ntal(PHPExcel_Style_Alignment()->setHorizo​​​​ntal(PHPExcel_Style_Aligny-CENA)130_PExcel. ;
  35. $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizo​​​​ntal(PHPExcel_Style_Alignment:: HORIZONTAL_CENTER);
  36. $ ('B')->getAlignment()->setHorizo​​ntal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  37. $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);
  38. $objPHPExcel->getActiveSheet()->getStyle('F')->getStyle('F')-> getAlignment()->setHorizo​​​​nimtal( HORIZONTAL_CENTER);
  39. //???cell
  40. $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
  41. / / 設定表頭內容
  42. $objeExkcel (0)
  43. ->setCellValue('A1', iconv("utf-8","gb2312","訂單資料總計時間:'.date('Y-m-d H:i:s')"))
  44. ->setCellValue(' A2', iconv("utf-8","gb2312","訂單ID"))
  45. ->setCellValue('B2', iconv("utf-8","gb2312","下單人") )
  46. ->setCellValue('C2', iconv("utf-8","gb2312","客戶名稱"))
  47. ->setCellValue('D2', iconv("utf-8"," gb2312","下單時間"))
  48. ->setCellValue('E2', iconv("utf-8","gb2312","需求機型"))
  49. ->setCellValue('F2' , iconv("utf-8","gb2312","需求數量"));
  50. // Miscellaneous glyphs, UTF-8
  51. for($i=0;$i $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i][ 'id']));
  52. $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i] ['reid']));
  53. $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i ]['topid']));
  54. //$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i 3), iconv('utf-8','gb2312',toDate( $OrdersData[$i]['typename']))); //?????????common.php??????
  55. $objPHPExcel->getActiveSheet(0)->setCellValue( 'D'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['typename']));
  56. $objPHPExcel->getActiveSheet(0)->setCellValue ('E'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['typelink']));
  57. $objPHPExcel->getActiveSheet(0)-> setCellValue('F'.($i 3), iconv("utf-8","gb2312",$OrdersData[$i]['keyword']));
  58. //$objPHPExcel->getActiveSheet() ->getStyle('A'.($i 3).':F'.($i 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  59. //$objPHPExcel->getActiveSheet ()->getStyle('A'.($i 3).':F'.($i 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  60. //$objPHPExcel->getActiveSheet()->getRowDimension($i 3)->setRowHeight(16);
  61. }
  62. // sheet
  63. $objPHPExcel->getActiveSheet()-> // sheet
  64. $objPHPExcel->getActiveSheet()-> // sheet
  65. $objPHPExcel->getActiveSheet()-> // sheet
  66. $objPHPExcel->getActiveSheet()-> // sheet
  67. $objPHPExcel->getActiveSheet()-> // sheet
  68. $objPHPExcel->getActiveSheet()-> // sheet
  69. $objPHPExcel->getActiveSheet()-> // sheet // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  70. $objPHPExcel->setActiveSheetIndex(0 );
  71. // excel頭參數
  72. header('Content-Type: application/vnd.ms-excel; charset=utf-8');
  73. header('Content-Type: application/force- download');
  74. header('Content-Type: application/octet-stream');
  75. 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') ;
複製程式碼
php, PHPExcel, excel

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板