Home >Backend Development >PHP Tutorial >php uses PHPExcel to export excel code

php uses PHPExcel to export excel code

WBOY
WBOYOriginal
2016-07-25 08:44:20911browse
  1. vendor("PHPExcel176.PHPExcel");
  2. // Create new PHPExcel object
  3. $objPHPExcel = new PHPExcel();
  4. // Set properties
  5. $objPHPExcel->getProperties()->setCreator("ctos")
  6. ->setLastModifiedBy("ctos")
  7. ->setTitle("Office 2007 XLSX Test Document")
  8. ->setSubject("Office 2007 XLSX Test Document")
  9. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  10. ->setKeywords("office 2007 openxml php")
  11. ->setCategory("Test result file");
  12. //set width
  13. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(80);
  14. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(80);
  15. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(80);
  16. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(80);
  17. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(80);
  18. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
  19. //设置行高度
  20. //$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
  21. //$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
  22. //set font size bold
  23. $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
  24. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
  25. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  26. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  27. //设置水平居中
  28. //$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  29. $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  30. $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  31. $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  32. $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  33. //合并cell
  34. //$objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
  35. //set font size bold
  36. //$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
  37. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getFont()->setBold(true);
  38. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  39. //$objPHPExcel->getActiveSheet()->getStyle('A2:F2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);??????????
  40. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  41. $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  42. $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  43. $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  44. $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  45. //???cell
  46. $objPHPExcel->getActiveSheet()->mergeCells('A1:F1');
  47. // set table header content
  48. $objPHPExcel->setActiveSheetIndex(0)
  49. ->setCellValue('A1', iconv("utf-8","gb2312","Order data summary time:'.date('Y-m-d H:i:s')"))
  50. ->setCellValue(' A2', iconv("utf-8","gb2312","Order ID"))
  51. ->setCellValue('B2', iconv("utf-8","gb2312","Order Placer"))
  52. ->setCellValue('C2', iconv("utf-8","gb2312","Customer Name"))
  53. ->setCellValue('D2', iconv("utf-8","gb2312", "Order time"))
  54. ->setCellValue('E2', iconv("utf-8","gb2312","Required model"))
  55. ->setCellValue('F2', iconv("utf -8","gb2312","Required quantity"));
  56. // Miscellaneous glyphs, UTF-8
  57. for($i=0;$i $objPHPExcel ->getActiveSheet(0)->setCellValue('A'.($i+3), iconv("utf-8","gb2312",$OrdersData[$i]['id']));
  58. $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), iconv("utf-8","gb2312",$OrdersData[$i]['reid'])) ;
  59. $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), iconv("utf-8","gb2312",$OrdersData[$i]['topid'] ));
  60. //$objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), iconv('utf-8','gb2312',toDate($OrdersData[$i ]['typename']))); //?????????common.php??????
  61. $objPHPExcel->getActiveSheet(0)->setCellValue('D'.( $i+3), iconv("utf-8","gb2312",$OrdersData[$i]['typename']));
  62. $objPHPExcel->getActiveSheet(0)->setCellValue('E' .($i+3), iconv("utf-8","gb2312",$OrdersData[$i]['typelink']));
  63. $objPHPExcel->getActiveSheet(0)->setCellValue(' F'.($i+3), iconv("utf-8","gb2312",$OrdersData[$i]['keyword']));
  64. //$objPHPExcel->getActiveSheet()-> getStyle('A'.($i+3).':F'.($i+3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  65. //$objPHPExcel-> getActiveSheet()->getStyle('A'.($i+3).':F'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border: :BORDER_THIN);
  66. //$objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
  67. }
  68. // sheet
  69. $objPHPExcel->getActiveSheet()-> ;setTitle( iconv("utf-8","gb2312",'Order Summary Table'));
  70. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  71. $objPHPExcel->setActiveSheetIndex (0);
  72. // excel header parameters
  73. header('Content-Type: application/vnd.ms-excel; charset=utf-8');
  74. header('Content-Type: application/force-download');
  75. header('Content-Type: application/octet-stream');
  76. header('Content-Type: application/download');
  77. //header('Content-type:charset=utf-8);
  78. // header('Content-Transfer-Encoding: utf-8');
  79. header('Content-Disposition: attachment;filename='.iconv("utf-8","gb2312","Order Summary".date('Ymd -His').".xls")); //The date is the file name suffix
  80. //header('Cache-Control: max-age=0');
  81. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ' excel5'); //excel5 is in xls format, excel2007 is in xlsx format
  82. $objWriter->save('php://output');
Copy code

php, PHPExcel, excel


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn