Home  >  Article  >  Backend Development  >  phpexcel rapid development guide (good)

phpexcel rapid development guide (good)

WBOY
WBOYOriginal
2016-07-25 09:04:20938browse
  1. include("./class/class.php"); // Contains the basic header file of class

  2. include("./class/phpexcel/PHPExcel.php" ); // Generate the basic class definition of excel (note the case of the file name)

  3. // If you directly output the excel file, you must include this file

  4. include("./class/phpexcel /PHPExcel/IOFactory.php");

  5. // Create a phpexcel object, which contains the output content and format

  6. $m_objPHPExcel = new PHPExcel();

  7. < p>// Template file, in order to separate the format and content, the specific content of the output file is implemented in the template file
  8. // The template file operates the object $m_objPHPExcel
  9. include("./include/excel.php");< ;/p>
  10. //Output file type, excel or pdf

  11. $m_exportType = "excel";

  12. $m_strOutputExcelFileName = date('Y-m-j_H_i_s')." .xls"; // Output EXCEL file name

  13. $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // Output PDF file name

  14. // PHPExcel_IOFactory, output excel

  15. //require_once dirname(__FILE__).'/Classes/PHPExcel/IOFactory.php';

  16. // If you need to output EXCEL format

  17. if($m_exportType=="excel"){
  18. $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'Excel5');

  19. // Output $m_strOutputExcelFileName directly from the browser

  20. header("Pragma: public");
  21. header("Expires : 0");
  22. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  23. header("Content-Type:application/force-download");
  24. header( "Content-Type: application/vnd.ms-excel;");
  25. header("Content-Type:application/octet-stream");
  26. header("Content-Type:application/download");
  27. header(" Content-Disposition:attachment;filename=".$m_strOutputExcelFileName);
  28. header("Content-Transfer-Encoding:binary");
  29. $objWriter->save("php://output");
  30. }
  31. //If you need to output PDF format

  32. if($m_exportType=="pdf"){
  33. $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'PDF');
  34. $objWriter->setSheetIndex (0);

  35. header("Pragma: public");

  36. header("Expires: 0");
  37. header("Cache-Control:must-revalidate, post-check=0 , pre-check=0");
  38. header("Content-Type:application/force-download");
  39. header("Content-Type: application/pdf");
  40. header("Content-Type:application/octet -stream");
  41. header("Content-Type:application/download");
  42. header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName);
  43. header("Content-Transfer-Encoding:binary") ;
  44. $objWriter->save("php://output");
  45. }
  46. ?>

Copy code

2. Template file content (with common operations attached)

  1. global $m_objPHPExcel; // Defined by external file

  2. // Set basic properties

  3. $m_objPHPExcel->getProperties() ->setCreator("Sun Star Data Center")
  4. ->setLastModifiedBy("Sun Star Data Center")
  5. ->setTitle("Microsoft Office Excel Document")
  6. ->setSubject("Test Data Report -- From Sunstar Data Center")
  7. ->setDescription("LD Test Data Report, Generate by Sunstar Data Center")
  8. ->setKeywords("sunstar ld report")
  9. ->setCategory("Test result file");

  10. //Create multiple workbooks

  11. $sheet1 = $m_objPHPExcel->createSheet();
  12. $sheet2 = $m_objPHPExcel->createSheet();

  13. < ;p>// You can operate the corresponding workbook by operating the index
  14. // Just set the workbook index to be operated as the current active workbook, such as
  15. // $m_objPHPExcel->setActiveSheetIndex(0);
  16. //Set the first workbook as the active workbook

  17. $m_objPHPExcel->setActiveSheetIndex(0);

  18. //Set the name of the active workbook

  19. // If it is Chinese, you must use the iconv function to convert the encoding
  20. $m_objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'Test Workbook'));

  21. //Set the default font and size

  22. $m_objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体'));
  23. $m_objPHPExcel->getDefaultStyle()->getFont()->setSize(10);

  24. // Set the width of a column

  25. $m_objPHPExcel->getActiveSheet()-> getColumnDimension('A')->setWidth(15);

  26. // Set the height of a row

  27. $m_objPHPExcel->getActiveSheet()->getRowDimension('6')-> ;setRowHeight(30);

  28. // Merge cells

  29. $m_objPHPExcel->getActiveSheet()->mergeCells('A1:P1');

  30. < p>// Define a style, bold, centered
  31. $styleArray1 = array(
  32. 'font' => array(
  33. 'bold' => true,
  34. 'color'=>array(
  35. 'argb' = > '00000000',
  36. ),
  37. ),

  38. 'alignment' => array(

  39. 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
  40. ),
  41. );< /p>
  42. //Apply style to cell A1

  43. $m_objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1);

  44. //Set cell style (black font)

  45. $m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color ::COLOR_BLACK); // Black

  46. // Set cell format (background)

  47. $m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFill( )->getStartColor()->setARGB('00ff99cc'); // Set the background to light pink

  48. // Set cell format (number format)

  49. $m_objPHPExcel-> ;getActiveSheet()->getStyle('F1')->getNumberFormat()->setFormatCode('0.000');

  50. // Write content to a specific cell

  51. $m_objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');

  52. // Set cell style (centered)

  53. $m_objPHPExcel->getActiveSheet( )->getStyle('H5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

  54. // Put a picture into the cell and add the data picture Place it in cell J1

  55. $objDrawing = new PHPExcel_Worksheet_Drawing();
  56. $objDrawing->setName('Logo');
  57. $objDrawing->setDescription('Logo');
  58. $objDrawing->setPath(" ../logo.jpg"); // Image path can only be a relative path
  59. $objDrawing->setWidth(400); // Image width
  60. $objDrawing->setHeight(123); // Image height
  61. $objDrawing->setCoordinates('J1');//Cell
  62. $objDrawing->setWorksheet($m_objPHPExcel->getActiveSheet());

  63. // Set A5 cell content and add hyperlinks

  64. $m_objPHPExcel->getActiveSheet()->setCellValue('A5', iconv('gbk', 'utf-8', 'Hyperlink jbxue.com'));
  65. $m_objPHPExcel-> ;getActiveSheet()->getCell('A5')->getHyperlink()->setUrl('http://bbs.it-home.org/');
  66. ?>

Copy code

3. Generate static files on the server side Compared with direct generation, the main difference between these two methods is the generation format. The template files are exactly the same. The following is a modified version based on the above example. Pay attention to the difference from the above example.

  1. // Contains the basic header file of class
  2. include("./class/class.php");

  3. // Generate the basic class definition of excel (note the case of the file name)

  4. include("./class/phpexcel/PHPExcel.php");
  5. // Contains files written in Excel5 format. If you need to generate excel2007 files, include the corresponding Writer can
  6. include("./class/phpexcel/PHPExcel/Writer/Excel5.php");
  7. // Includes writing PDF format files
  8. include("./class/phpexcel/PHPExcel/Writer/PDF.php") ;

  9. // Create a phpexcel object, which contains the output content and format

  10. $m_objPHPExcel = new PHPExcel();

  11. // Template file, in order To achieve separation of format and content, the specific content of the output file is implemented in the template file

  12. // The template file operates the object $m_objPHPExcel
  13. include("./include/excel.php");

  14. $m_exportType = "pdf";

  15. $m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // Output EXCEL File name

  16. $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // Output PDF file name

  17. // Output file saving path, this path must be writable

  18. $ m_strOutputPath = "./output/";

  19. // If you need to output EXCEL format

  20. if($m_exportType=="excel"){
  21. $objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel);
  22. $objWriter->save($m_strOutputPath.$m_strOutputExcelFileName);
  23. }

  24. // If you need to output PDF format

  25. if($m_exportType=="pdf"){
  26. $objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel);
  27. $objWriter->save($m_strOutputPath.$m_strOutputPdfFileName);
  28. }
  29. ?>

Copy code


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