This article mainly introduces you to the relevant information about the common excel export method in php. The method introduced in the article will have a certain reference for everyone to learn or use php The value of learning, friends who need to learn PHP how to export Excel, please follow the editor to learn together.
1. Common export methods
There are many methods to export excel online. To export excel in crm or oa system, As is common, everyone who has done this function knows that its main operation is actually to cycle through the data list, and then add data to fixed cells one by one. As long as you do it once, you only need to copy the relevant code and modify it, and the export function in other places will be completed.
But this will have two problems:
1. When the list data field is modified, a large amount of code needs to be modified, which is difficult to maintain and uncomfortable to change. ;
2. When the export function appears multiple times, a large number of repetitive and redundant codes need to be copied in multiple places, which looks uncomfortable;
Therefore, it is necessary to unify one export excel When using this method, you only need to pass in the data header, header field name, data list, and data table name to export to excel.
2. Use the general export method
As shown in the figure below, as long as 4 parameters are passed in, the export can be completed , use and maintenance become much simpler.
Okay, the goal has been clarified, the next step is to implement the code.
Obviously, the biggest difficulty in realizing this function is to automatically calculate the coordinate value of each field in excel (such as A1) based on the index of a single piece of data and the order of header fields. , B3).
Then let’s analyze the cell coordinates of excel. Starting from A1, increase the value of numbers vertically and letters horizontally. When the horizontal letters change to Z, the next letters are AA, then AB, ..., ZZ, ..., AAA...
In this case, we know that the vertical coordinate is simple and can be calculated based on the index value of each piece of data. The difficult thing is the horizontal coordinate , how to calculate? After careful analysis of the horizontal coordinates, we can find that it is a hexadecimal-like alphanumeric system. If A is regarded as 0, then Z represents 25. But this kind of number is different from our common hexadecimal, octal, etc., because when Z is carried, the next number is not BA, but AA. In common base systems, for example, if the decimal system carries 9, it becomes 10, not 00; if the hexadecimal system carries 0xF, it becomes 0x10, not 0x00.
Therefore, you can refer to the algorithm of base conversion, and then change it to get the method of calculating the horizontal coordinates of excel (decimal to pseudo-26 base):
//AAA转换 public static function toAAA($dec) { if ($dec < 0) return ''; $y = $dec % 26; $x = floor($dec / 26); return self::toAAA($x - 1) . chr($y + 65); }
Finally, attach the complete code
The framework is Yii2 and the excel export component is moonlandsoft /yii2-phpexcel;
Other similar
//导出xls public static function exportXls($array) { set_time_limit(0); include(Url::to('@vendor/moonland/phpexcel/PHPExcel.php')); include(Url::to('@vendor/moonland/phpexcel/PHPExcel/Writer/Excel2007.php')); $titles = $array['titles']; $fields = $array['fields']; $list = $array['list']; $name = $array['name']; $count = count($titles); $keys = [];//A=>chr(65) foreach ($titles as $k => $v) { $keys[] = self::toAAA($k); } $objPHPExcel = new \PHPExcel(); $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel); $objPHPExcel->setActiveSheetIndex(0); $activeSheet = $objPHPExcel->getActiveSheet(); $activeSheet->setTitle($name); $activeSheet->getStyle("A1:{$keys[$count-1]}1")->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $activeSheet->mergeCells("A1:{$keys[$count-1]}1"); $activeSheet->setCellValue('A1', $name); //设置title,样式 foreach ($titles as $key => $title) { $activeSheet->setCellValue($keys[$key] . '2', $title); $activeSheet->getColumnDimension($keys[$key])->setWidth(20); $activeSheet->getRowDimension(($key + 1))->setRowHeight(18); } $i = 3; foreach ($list as &$item) { foreach ($keys as $k => $v) { $val = isset($item[$fields[$k]]) ? $item[$fields[$k]] . ' ' : ' '; $activeSheet->setCellValue($v . $i, $val); } $i++; } $fileName = $name . "_" . date('Y_m_d_His') . '.xlsx'; header("Cache-Control: public"); header("Pragma: public"); header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:attachment;filename=" . iconv("utf-8", "GB2312//TRANSLIT", $fileName)); header('Content-Type:APPLICATION/OCTET-STREAM'); ob_clean(); ob_start(); $objWriter->save('php://output'); ob_end_flush(); } //AAA转换 public static function toAAA($dec) { if ($dec < 0) return ''; $y = $dec % 26; $x = floor($dec / 26); return self::toAAA($x - 1) . chr($y + 65); }
## 3. Export Result example
Related recommendations:
How to save pictures in EXCEL with PHP
Native implementation method of PHP exporting and downloading excel
PHP Export EXCEL Quick Development Guide
The above is the detailed content of Example of common excel export method in php. For more information, please follow other related articles on the PHP Chinese website!