首页  >  文章  >  后端开发  >  PHPExcel代码

PHPExcel代码

WBOY
WBOY原创
2016-07-25 08:42:13919浏览

php导出excel的

[PHP]代码

  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","订单数据汇总 时间:'.date('Y-m-d H:i:s')"))
  50. ->setCellValue('A2', iconv("utf-8","gb2312","订单ID"))
  51. ->setCellValue('B2', iconv("utf-8","gb2312","下单人"))
  52. ->setCellValue('C2', iconv("utf-8","gb2312","客户名称"))
  53. ->setCellValue('D2', iconv("utf-8","gb2312","下单时间"))
  54. ->setCellValue('E2', iconv("utf-8","gb2312","需求机型"))
  55. ->setCellValue('F2', iconv("utf-8","gb2312","需求数量"));
  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",'订单汇总表'));
  70. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  71. $objPHPExcel->setActiveSheetIndex(0);
  72. // excel头参数
  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","订单汇总".date('Ymd-His').".xls")); //日期为文件名后缀
  80. //header('Cache-Control: max-age=0');
  81. $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'excel5'); //excel5为xls格式,excel2007为xlsx格式
  82. $objWriter->save('php://output');
复制代码
PHPExcel


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn