Home > Backend Development > PHP Tutorial > PHP export excel php uses phpexcel to export excel files

PHP export excel php uses phpexcel to export excel files

WBOY
Release: 2016-07-25 08:54:43
Original
980 people have browsed it
  1. //Load the PHPExcel class

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

  3. //Create an excel object instance

  4. $objPHPExcel = new PHPExcel();

  5. //Set the basic properties 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. //Generally, display calls are only needed when using multiple sheets.
  16. //By default, PHPExcel will automatically create the first sheet with 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 cell content

  22. //The data here can Read from the database and then perform loop processing
  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 certain location

  47. $objWriter->save(dirname(__FILE__) . '/demo.xls');< /p>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template