Home Backend Development PHP Tutorial Sharing how to use phpExcel class

Sharing how to use phpExcel class

Jul 25, 2016 am 08:56 AM

  1. include 'PHPExcel.php';
  2. include 'PHPExcel/Writer/Excel2007.php';
  3. //or include 'PHPExcel/Writer/Excel5.php'; for outputting .xls
  4. Create an excel
  5. $objPHPExcel = new PHPExcel();
  6. Save excel—2007 format
  7. $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  8. //or $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); non-2007 format
  9. $objWriter- >save( "xxx.xlsx");
Copy the code

2 and output it directly to the browser

  1. $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

  2. header("Pragma: public");
  3. header("Expires: 0″);
  4. header("Cache-Control:must -revalidate, post-check=0, pre-check=0″);
  5. header("Content-Type:application/force-download");
  6. header("Content-Type:application/vnd.ms-execl") ;
  7. header("Content-Type:application/octet-stream"); ​​
  8. header("Content-Type:application/download");;
  9. header('Content-Disposition:attachment;filename="resume.xls"' );
  10. header("Content-Transfer-Encoding:binary");
  11. $objWriter->save('php://output');

  12. 3, set excel attributes:

  13. Created by
  14. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
  15. Last modified by
  16. $objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
  17. Title
  18. $objPHPExcel->getProperties()->setTitle("office 2007 XLSX Test Document");
  19. Title
  20. $objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
  21. Description
  22. $objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
  23. Keywords
  24. $objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
  25. Category
  26. $objPHPExcel->getProperties()->setCategory("Test result file");

Copy code

4, other

  1. Set the current sheet
  2. $objPHPExcel->setActiveSheetIndex(0);
  3. Set the name of the sheet
  4. $objPHPExcel->getActiveSheet()->setTitle('Simple');
  5. Set the value of the cell
  6. $objPHPExcel->getActiveSheet()->setCellValue('A1′, 'String');
  7. $objPHPExcel->getActiveSheet()->setCellValue('A2′, 12);
  8. $objPHPExcel-> getActiveSheet()->setCellValue('A3′, true);
  9. $objPHPExcel->getActiveSheet()->setCellValue('C5′, '=SUM(C2:C4)');
  10. $objPHPExcel-> getActiveSheet()->setCellValue('B8′, '=MIN(B2:C5)');
  11. Merge cells
  12. $objPHPExcel->getActiveSheet()->mergeCells('A18:E22′);
  13. Separate Cell
  14. $objPHPExcel->getActiveSheet()->unmergeCells('A28:B28′);
  15. Protect cell
  16. $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); / / Needs to be set to true in order to enable any worksheet protection!
  17. $objPHPExcel->getActiveSheet()->protectCells('A3:E13′, 'PHPExcel');
  18. Set format
  19. // Set cell number formats
  20. echo date('H:i:s') . " Set cell number formatsn";
  21. $objPHPExcel->getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat ::FORMAT_CURRENCY_EUR_SIMPLE);
  22. $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4′), 'E5:E13′ );
  23. Set the width
  24. / / Set column widths
  25. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
  26. $objPHPExcel->getActiveSheet()->getColumnDimension('D')-> ;setWidth(12);
  27. Set font
  28. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setName('Candara');
  29. $objPHPExcel->getActiveSheet ()->getStyle('B1′)->getFont()->setSize(20);
  30. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()- >setBold(true);
  31. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
  32. $objPHPExcel->getActiveSheet( )->getStyle('B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  33. $objPHPExcel->getActiveSheet()->getStyle('E1′ )->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
  34. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getFont()-> ;setBold(true);
  35. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getFont()->setBold(true);
  36. Set align
  37. $objPHPExcel->getActiveSheet() ->getStyle('D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  38. $objPHPExcel->getActiveSheet()->getStyle('D12′)->getAlignment() ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  39. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  40. $objPHPExcel- >getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
  41. //Vertical centering
  42. $objPHPExcel->getActiveSheet()->getStyle( 'A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  43. Set the column's border
  44. $objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders() ->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  45. $objPHPExcel->getActiveSheet()->getStyle('B4′)->getBorders()->getTop()-> setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  46. $objPHPExcel->getActiveSheet()->getStyle('C4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  47. $objPHPExcel->getActiveSheet()->getStyle('D4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  48. $objPHPExcel->getActiveSheet()- >getStyle('E4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
  49. Set the border color
  50. $objPHPExcel->getActiveSheet()->getStyle( 'D13′)->getBorders()->getLeft()->getColor()->setARGB('FF993300′);
  51. $objPHPExcel->getActiveSheet()->getStyle('D13′) ->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  52. $objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders ()->getBottom()->getColor()->setARGB('FF993300′);
  53. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);
  54. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);
  55. $objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor()->setARGB('FF993300′);
  56. 设置填充颜色
  57. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  58. $objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′);
  59. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
  60. $objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->getStartColor()->setARGB('FF808080′);
  61. 加图片 //bbs.it-home.org
  62. $objDrawing = new PHPExcel_Worksheet_Drawing();
  63. $objDrawing->setName('Logo');
  64. $objDrawing->setDescription('Logo');
  65. $objDrawing->setPath('./images/officelogo.jpg');
  66. $objDrawing->setHeight(36);
  67. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  68. $objDrawing = new PHPExcel_Worksheet_Drawing();
  69. $objDrawing->setName('Paid');
  70. $objDrawing->setDescription('Paid');
  71. $objDrawing->setPath('./images/paid.png');
  72. $objDrawing->setCoordinates('B15′);
  73. $objDrawing->setOffsetX(110);
  74. $objDrawing->setRotation(25);
  75. $objDrawing->getShadow()->setVisible(true);
  76. $objDrawing->getShadow()->setDirection(45);
  77. $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
  78. 在默认sheet后,创建一个worksheet
  79. echo date('H:i:s') . " Create new Worksheet objectn";
  80. $objPHPExcel->createSheet();
  81. $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
  82. $objWriter-save('php://output');
复制代码


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles