krot

order export

Dec 6th, 2017
103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?
  2. mysql_query("SET NAMES 'utf8'");
  3. mysql_query("SET CHARACTER SET 'utf8'");
  4. mysql_query("SET SESSION collation_connection = 'utf8_general_ci'");
  5.  
  6. $sql="SELECT * FROM `ORDERS`";
  7.     $hQ=mysql_unbuffered_query($sql);
  8.     if($hQ){
  9.         //экспорт
  10.         require_once $_SERVER['DOCUMENT_ROOT']."/PHPExcel/Classes/PHPExcel/IOFactory.php";
  11.         require_once $_SERVER['DOCUMENT_ROOT']."/PHPExcel/Classes/PHPExcel.php";
  12. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  13. $objPHPExcel  = new PHPExcel();
  14. $objPHPExcel->setActiveSheetIndex();  //set first sheet as active
  15. $objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
  16. $objPHPExcel->getActiveSheet()
  17.     ->getPageSetup()
  18.     ->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
  19. //поля
  20. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.39);
  21. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.39);
  22. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.39);
  23. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.39);
  24.  
  25. $sheet=$objPHPExcel->getActiveSheet();
  26.  
  27. $rowPointer=1;
  28. //шапка
  29. $columnPointer=0;
  30. $title=array(
  31. 'ID','Сумма заказа'
  32. );
  33.  
  34. foreach($title as $v){
  35.     $sheet->setCellValueExplicitByColumnAndRow($columnPointer++, $rowPointer, $v);
  36.    
  37. }
  38. $sheet->getStyle("A1:P1")->applyFromArray(
  39.     array(
  40.         'borders' => array(
  41.             'allborders' => array(
  42.                 'style' => PHPExcel_Style_Border::BORDER_THIN,
  43.                 'color' => array('rgb' => '000000')
  44.             )
  45.         ),
  46.          'fill' => array(
  47.             'type' => PHPExcel_Style_Fill::FILL_SOLID,
  48.             'color' => array('rgb' => 'ffcc80')
  49.         )
  50.  
  51.     )
  52. );
  53. $rowPointer++;
  54. while($row=mysql_fetch_assoc($hQ)){
  55.  
  56.    
  57. $columnPointer=0;
  58. //id
  59. $sheet->setCellValueExplicitByColumnAndRow($columnPointer++, $rowPointer, $row['ID']);
  60. //sum
  61. $sheet->setCellValueExplicitByColumnAndRow($columnPointer++, $rowPointer, $row['PRICESUM']);
  62.  
  63. //данные о клиенте
  64. $sheet->setCellValueExplicitByColumnAndRow($columnPointer++, $rowPointer, htmlspecialchars_decode($client_d->name));
  65.  
  66. //данные о заказе
  67. $r=0;
  68. $c=$columnPointer;
  69. foreach($order_d as $v){
  70.     $c=$columnPointer;
  71.     $sheet->setCellValueExplicitByColumnAndRow($c++, $rowPointer+$r, $v->d_brand?$v->d_brand:'--');
  72.     ..
  73.     $r++;
  74. }
  75. //обьединённые ячейки
  76. for($m=0;$m<8;++$m){
  77. $sheet->getStyleByColumnAndRow($m,$rowPointer)->getAlignment()->
  78.                 setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);       
  79. $sheet->mergeCellsByColumnAndRow($m,$rowPointer, $m,$rowPointer+($r-1));
  80. }
  81.  
  82. //раскрасить
  83. $sheet->getStyle("A$rowPointer:P".$rowPointer)->applyFromArray(
  84.     array(
  85.         'borders' => array(
  86.             'top' => array(
  87.                 'style' => PHPExcel_Style_Border::BORDER_THIN,
  88.                 'color' => array('rgb' => '000000'),
  89.                
  90.             )
  91.         ),
  92.        
  93.  
  94.     )
  95. );
  96.  
  97. $rowPointer+=$r-1;
  98. $rowPointer++;
  99. }
  100.  
  101.  
  102.  
  103.     // Redirect output to a client’s web browser (Excel5)
  104. header('Content-Type: application/vnd.ms-excel');
  105. header('Content-Disposition: attachment;filename="order.xls"');
  106. header('Cache-Control: max-age=0');
  107. $objPHPExcel = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  108. $objPHPExcel->save('php://output');        
  109.                
  110. exit;
  111.  
  112. #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  113.        
  114.     }
  115. }
  116. ?>
RAW Paste Data