


Introduction to how PHPExcel is replaced by PHP_XLSXWriter (picture)
PHPExcel is an open source framework for processing Excel and CVS files. Unfortunately, PHPExcel officially no longer maintains this project. The official team has started a new project on github called PhpSpreadsheet. So this article mainly introduces you to examples of how to use PHP_XLSXWriter instead of PHPExcel. Friends in need can refer to it.
Preface
This article mainly introduces to you the method of using PHP_XLSXWriter instead of PHPExcel, and shares it for your reference and study. I won’t say anything below. Let’s talk more, let’s take a look at the detailed introduction:
What’s the difference between the two?
PHPExcel is a process for Excel, An open source framework for CVS files, based on Microsoft's OpenXML standard and PHP language. You can use it to read and write spreadsheets in different formats. This is also the most versatile Excel processing tool for PHP so far, but it has a very fatal shortcoming: it takes up a lot of memory and is almost tiring for large batches of table data. Love it or not, the processing speed is very slow, but it is very rich in functions and has many APIs, so when exporting Excel tables in complex formats, you often have to use it, which is really a love-hate relationship.
Unfortunately, PHPExcel officially no longer maintains this project. The official team has started a new project on github called PhpSpreadsheet. The new project uses a lot of new PHP features, such as Namespace , PSR standard, and its performance is much higher than PHPExcel. However, the project is still in development status (2017-07-12). The minimum stable version has not yet been released. It is estimated that there will be more bugs, so it is not recommended. Use, the picture below is the project migration instructions:
Compared with PHPExcel, PHP_XLSXWriter is a small but powerful Excel reading and writing plug-in. It does not have as many advanced features as PHPExcel. Operations such as freezing table headers are not available, but its export speed is very fast, which is very suitable for export requirements where the amount of data is particularly large and the report format is not very complex. The following picture is the official speed and memory test:
How to use PHP_XLSXWriter?
Download
This is the github address of PHP_XLSXWriter. Of course, you can also download it locally. You can click to download Download it. After decompression, you can see that it has only one core file: xlswriter.class.php. The examples directory is the code sample directory. There are many examples in it for you to refer to.
Use
For most daily reporting needs, PHP_XLSXWriter is competent. Let's use an example to get familiar with the use of API.
Suppose we want to export the report in the picture below and download it through the browser:
Code implementation:
//writer 类 $writer = new XLSXWriter(); //文件名 $filename = "example.xlsx"; //设置 header,用于浏览器下载 header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"'); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Pragma: public'); //导出的数据 $string = array ( 0 => array ( 'payc_bill_time' => '2017-07-12 16:40:44', 'payt_received_date' => '2017-07-12', 'ci_name' => '租金', 'payt_num' => 'YRZB(2012)A0047', 'payt_scsr_name' => '李巧红', 'payt_received' => '300.00', 'paytd_type' => '现金', 'emp_name' => '郑振标', ), 1 => array ( 'payc_bill_time' => '2017-07-12 16:39:55', 'payt_received_date' => '2017-07-12', 'ci_name' => '租金', 'payt_num' => 'YRZB(2012)A0046', 'payt_scsr_name' => '22222', 'payt_received' => '45.00', 'paytd_type' => '现金', 'emp_name' => '郑振标', ) ); //每列的标题头 $title = array ( 0 => '开单时间', 1 => '收款时间', 2 => '开票项目', 3 => '票据编号', 4 => '客户名称', 5 => '实收金额', 6 => '收款方式', 7 => '收款人', ); //工作簿名称 $sheet1 = 'sheet1'; //对每列指定数据类型,对应单元格的数据类型 foreach ($title as $key => $item){ $col_style[] = $key ==5 ? 'price': 'string'; } //设置列格式,suppress_row: 去掉会多出一行数据;widths: 指定每列宽度 $writer->writeSheetHeader($sheet1, $col_style, ['suppress_row'=>true,'widths'=>[20,20,20,20,20,20,20,20]] ); //写入第二行的数据,顺便指定样式 $writer->writeSheetRow($sheet1, ['xxx财务报表'], ['height'=>32,'font-size'=>20,'font-style'=>'bold','halign'=>'center','valign'=>'center']); /*设置标题头,指定样式*/ $styles1 = array( 'font'=>'宋体','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee', 'halign'=>'center', 'border'=>'left,right,top,bottom'); $writer->writeSheetRow($sheet1, $title,$styles1); // 最后是数据,foreach写入 foreach ($data as $value) { foreach ($value as $item) { $temp[] = $item;} $rows[] = $temp; unset($temp); } $styles2 = ['height'=>16]; foreach($rows as $row){ $writer->writeSheetRow($sheet1, $row,$styles2); } //合并单元格,第一行的大标题需要合并单元格 $writer->markMergedCell($sheet1, $start_row=0, $start_col=0, $end_row=0, $end_col=7); //输出文档 $writer->writeToStdOut(); exit(0);
The above Each line of code is commented. If you don’t understand it, you can check out the code samples in the example folder and the documentation on the official website, but the documentation is relatively short;
Guide to jumping into pits :
I have also stepped on some pitfalls during use. Here is a list, I hope it will be helpful to you:
File name and class The name does not correspond to
. This is not actually a problem when using require or require_once, but when using automatic loading, the two do not correspond. It cannot be identified. You may want to introduce the xlsxwriter.class.php file into your project and add a namespace so that it can be automatically loaded. What you need to do at this time is to change the file name to the class name XLSXWriter.class.php (introduced into TP here). For example, if I put it in the Component directory, then add the namespace Component; to the file. At this time, There is also a Zip class in the file that has not introduced a namespace, and you need to add use ZipArchive;
After these are completed, you can use it elsewhere in the project:
use Component; $writer = new XLSXWriter();
How to set columns Format?
Different columns may need to be displayed in different formats. The default ones are text formats, but sometimes they need to be displayed as numeric columns, which is more convenient for using functions in excel, such as the table above. The amount column in must have two decimal points, thousandths, and numeric format.
Look at the code above. The number format is actually set in the writeSheetHeader method. The column with type price is the amount column. If you need other formats, the commonly used formats are listed on the homepage of the official website.
Can I set the value of a certain cell individually?
This has not been implemented yet. Currently, data is written row by row, and such fine granularity is not supported. However, a compromise is to write null into the cells that do not need to be filled;
Summarize
The above is the detailed content of Introduction to how PHPExcel is replaced by PHP_XLSXWriter (picture). 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

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

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

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,

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

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

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.
