PHP Export EXCEL Quick Development Guide
php excel has a proprietary development document. For detailed operations, please refer to the php excel development document. This article only optimizes and integrates its use to facilitate new projects. Rapid development. If you are interested, you can also look at the php excel development documentation.
There are also two ways for phpexcel to generate files, one is direct output, and the other is to generate static files.
Direct output:
The main file is (the file in the same directory of the class directory):
<?php include("./class/class.php"); // 包含class的基本头文件 include("./class/phpexcel/PHPExcel.php"); // 生成excel的基本类定义(注意文件名的大小写) // 如果直接输出excel文件,则要包含此文件 include("./class/phpexcel/PHPExcel/IOFactory.php"); // 创建phpexcel对象,此对象包含输出的内容及格式 $m_objPHPExcel = new PHPExcel(); // 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中 // 模板文件将对象$m_objPHPExcel进行操作 include("./include/excel.php"); // 输出文件的类型,excel或pdf $m_exportType = "excel"; $m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // 输出EXCEL文件名 $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // 输出PDF文件名 // PHPExcel_IOFactory, 输出excel //require_once dirname(__FILE__).'/Classes/PHPExcel/IOFactory.php'; // 如果需要输出EXCEL格式 if($m_exportType=="excel"){ $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'Excel5'); // 从浏览器直接输出$m_strOutputExcelFileName header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type: application/vnd.ms-excel;"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Content-Disposition:attachment;filename=".$m_strOutputExcelFileName); header("Content-Transfer-Encoding:binary"); $objWriter->save("php://output"); } // 如果需要输出PDF格式 if($m_exportType=="pdf"){ $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, 'PDF'); $objWriter->setSheetIndex(0); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type: application/pdf"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName); header("Content-Transfer-Encoding:binary"); $objWriter->save("php://output"); } ?>
Template file content (with additional common operations)
<?php global $m_objPHPExcel; // 由外部文件定义 // 设置基本属性 $m_objPHPExcel->getProperties()->setCreator("Sun Star Data Center") ->setLastModifiedBy("Sun Star Data Center") ->setTitle("Microsoft Office Excel Document") ->setSubject("Test Data Report -- From Sunstar Data Center") ->setDescription("LD Test Data Report, Generate by Sunstar Data Center") ->setKeywords("sunstar ld report") ->setCategory("Test result file"); // 创建多个工作薄 $sheet1 = $m_objPHPExcel->createSheet(); $sheet2 = $m_objPHPExcel->createSheet(); // 通过操作索引即可操作对应的工作薄 // 只需设置要操作的工作簿索引为当前活动工作簿,如 // $m_objPHPExcel->setActiveSheetIndex(0); // 设置第一个工作簿为活动工作簿 $m_objPHPExcel->setActiveSheetIndex(0); // 设置活动工作簿名称 // 如果是中文一定要使用iconv函数转换编码 $m_objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', '测试工作簿')); // 设置默认字体和大小 $m_objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体')); $m_objPHPExcel->getDefaultStyle()->getFont()->setSize(10); // 设置一列的宽度 $m_objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15); // 设置一行的高度 $m_objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30); // 合并单元格 $m_objPHPExcel->getActiveSheet()->mergeCells('A1:P1'); // 定义一个样式,加粗,居中 $styleArray1 = array( 'font' => array( 'bold' => true, 'color'=>array( 'argb' => '00000000', ), ), 'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, ), ); // 将样式应用于A1单元格 $m_objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1); // 设置单元格样式(黑色字体) $m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色 // 设置单元格格式(背景) $m_objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 将背景设置为浅粉色 // 设置单元格格式(数字格式) $m_objPHPExcel->getActiveSheet()->getStyle('F1')->getNumberFormat()->setFormatCode('0.000'); // 给特定单元格中写入内容 $m_objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby'); // 设置单元格样式(居中) $m_objPHPExcel->getActiveSheet()->getStyle('H5')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); // 给单元格中放入图片, 将数据图片放在J1单元格内 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath("../logo.jpg"); // 图片路径,只能是相对路径 $objDrawing->setWidth(400); // 图片宽度 $objDrawing->setHeight(123); // 图片高度 $objDrawing->setCoordinates('J1'); $objDrawing->setWorksheet($m_objPHPExcel->getActiveSheet()); // 设置A5单元格内容并增加超链接 $m_objPHPExcel->getActiveSheet()->setCellValue('A5', iconv('gbk', 'utf-8', '超链接keiyi.com')); $m_objPHPExcel->getActiveSheet()->getCell('A5')->getHyperlink()->setUrl('http://www.keiyi.com/'); ?>
Generate static files on the server side
Compared with direct generation, the main difference between these two methods is the generation format. The template file is completely The same, the following is a modified version based on the above example, pay attention to the difference from the above example.
<?php // 包含class的基本头文件 include("./class/class.php"); // 生成excel的基本类定义(注意文件名的大小写) include("./class/phpexcel/PHPExcel.php"); // 包含写Excel5格式的文件,如果需要生成excel2007的文件,包含对应的Writer即可 include("./class/phpexcel/PHPExcel/Writer/Excel5.php"); // 包含写PDF格式文件 include("./class/phpexcel/PHPExcel/Writer/PDF.php"); // 创建phpexcel对象,此对象包含输出的内容及格式 $m_objPHPExcel = new PHPExcel(); // 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中 // 模板文件将对象$m_objPHPExcel进行操作 include("./include/excel.php"); // 输出文件的类型,excel或pdf $m_exportType = "pdf"; $m_strOutputExcelFileName = date('Y-m-j_H_i_s').".xls"; // 输出EXCEL文件名 $m_strOutputPdfFileName = date('Y-m-j_H_i_s').".pdf"; // 输出PDF文件名 // 输出文件保存路径,此路径必须可写 $m_strOutputPath = "./output/"; // 如果需要输出EXCEL格式 if($m_exportType=="excel"){ $objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel); $objWriter->save($m_strOutputPath.$m_strOutputExcelFileName); } // 如果需要输出PDF格式 if($m_exportType=="pdf"){ $objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel); $objWriter->save($m_strOutputPath.$m_strOutputPdfFileName); } ?>
The above is all the contents of the PHP Export EXCEL Rapid Development Guide. If you are interested, you can search for more on the PHP Chinese website~
##Related recommendations:
Introduction to the method of freezing and locking table headers in PHPExcel
Example of how phpexcel imports excel to process big data code
Use PHPExcel to upload data in batches
Introduction to the use process of PHPExcel
The above is the detailed content of PHP Export EXCEL Quick Development Guide. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

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.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
