PHP export excelphp use phpexcel to export excel files

Source: Internet
Author: User
PHP export excelphp use phpexcel to export excel files

  1. // Load the PHPExcel class

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

  3. // Create an excel Object instance

  4. $ ObjPHPExcel = new PHPExcel ();

  5. // Set the basic attributes of the document

  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. // Set the current sheet index for subsequent content operations.

  15. // Display the call only when multiple sheets are used.
  16. // By default, PHPExcel will automatically create the first sheet and set SheetIndex = 0
  17. $ ObjPHPExcel-> setActiveSheetIndex (0 );

  18. // Set the name of the current active sheet

  19. $ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
  20. $ ObjActSheet-> setTitle ('test Sheet ');

  21. // Set the cell content

  22. // The data here can be read from the database and processed cyclically.
  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. // Set the header information and output it to the 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 ');

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.