PHP export Excel PHP using Phpexcel export Excel file

Source: Internet
Author: User
  1. Load Phpexcel class

  2. Require './phpexcel/phpexcel.php ';

  3. Create an instance of an Excel object

  4. $objPHPExcel = new Phpexcel ();

  5. Set document basic Properties

  6. $objProps = $objPHPExcel->getproperties ();
  7. $objProps->setcreator ("Lao Mao");
  8. $objProps->setlastmodifiedby ("Lao Mao");
  9. $objProps->settitle ("Office XLS Test Document");
  10. $objProps->setsubject ("Office XLS Test Document, Demo");
  11. $objProps->setdescription ("Test document, generated by Phpexcel.");
  12. $objProps->setkeywords ("Office Excel Phpexcel");
  13. $objProps->setcategory ("Test");

  14. Sets the current sheet index for subsequent content operations.

  15. It is generally only necessary to display a call when multiple sheet are used.
  16. By default, Phpexcel automatically creates the first sheet to be set sheetindex=0
  17. $objPHPExcel->setactivesheetindex (0);

  18. Sets the name of the currently active sheet

  19. $objActSheet = $objPHPExcel->getactivesheet ();
  20. $objActSheet->settitle (' Test sheet ');

  21. Set cell contents

  22. The data here can be read from the database and then processed in a loop
  23. by bbs.it-home.org
  24. $objPHPExcel->getactivesheet ()->setcellvalue (' A1 ', ' A1 ');
  25. $objPHPExcel->getactivesheet ()->setcellvalue (' A2 ', ' A2 ');
  26. $objPHPExcel->getactivesheet ()->setcellvalue (' A3 ', ' A3 ');
  27. $objPHPExcel->getactivesheet ()->setcellvalue (' A4 ', ' A4 ');
  28. $objPHPExcel->getactivesheet ()->setcellvalue (' A5 ', ' A5 ');

  29. $objPHPExcel->getactivesheet ()->setcellvalue (' B1 ', ' B1 ');

  30. $objPHPExcel->getactivesheet ()->setcellvalue (' B2 ', ' B2 ');
  31. $objPHPExcel->getactivesheet ()->setcellvalue (' B3 ', ' B3 ');
  32. $objPHPExcel->getactivesheet ()->setcellvalue (' B4 ', ' b4 ');
  33. $objPHPExcel->getactivesheet ()->setcellvalue (' B5 ', ' B5 ');

  34. $objPHPExcel->getactivesheet ()->setcellvalue (' C1 ', ' C1 ');

  35. $objPHPExcel->getactivesheet ()->setcellvalue (' C2 ', ' C2 ');
  36. $objPHPExcel->getactivesheet ()->setcellvalue (' C3 ', ' C3 ');
  37. $objPHPExcel->getactivesheet ()->setcellvalue (' C4 ', ' C4 ');
  38. $objPHPExcel->getactivesheet ()->setcellvalue (' C5 ', ' C5 ');

  39. Output document

  40. $objWriter = new Phpexcel_writer_excel5 ($objPHPExcel);

  41. Headers header information is set and output to browser

  42. Header (' Content-type:application/vnd.ms-excel ');
  43. Header ("content-disposition:attachment; Filename=demo.xls ");
  44. Header (' cache-control:max-age=0 ');
  45. $objWriter->save (' php://output ');

  46. Save to a location

  47. $objWriter->save (DirName (__file__). '/demo.xls ');

Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.