Home > Backend Development > PHP Tutorial > Detailed explanation of PHP data export knowledge points

Detailed explanation of PHP data export knowledge points

小云云
Release: 2023-03-20 11:06:01
Original
1439 people have browsed it

Recently I am working on a backend management project, and there is usually a need to export data to excel in the backend. After previous searches, it is usually recommended to use php excel. I often use laravel, and there is also a very useful corresponding package for php excel.

It is very easy to use at first, but when the data that needs to be exported reaches tens of thousands, it will directly cause the problem of insufficient memory.

Then I found several solutions.

Front-end solution

PHP cooperates with SheetJS/js-xlsx to export a large amount of Excel data

The benefits of this solution are not required Additional interfaces, but dependent on front-end developers.

Export to csv

This solution is faster and fully back-end implemented. The disadvantage is that the csv format has relatively high requirements on the export form, and the requirement is pure Data cannot exist in rich text form such as pictures.

The following mainly introduces how to export csv

Introduction to php official documentation


<?php

$list = array (
  array(&#39;aaa&#39;, &#39;bbb&#39;, &#39;ccc&#39;, &#39;dddd&#39;),
  array(&#39;123&#39;, &#39;456&#39;, &#39;789&#39;),
  array(&#39;"aaa"&#39;, &#39;"bbb"&#39;)
);

$fp = fopen(&#39;file.csv&#39;, &#39;w&#39;);

foreach ($list as $fields) {
  fputcsv($fp, $fields);
}

fclose($fp);
?>
Copy after login

Export complete example


<?php

$name = &#39;test&#39;;
header ( "Content-type:application/vnd.ms-excel" );
header ( "Content-Disposition:filename=".$name.".csv" );
header (&#39;Cache-Control: max-age=0&#39;);

//打开PHP文件句柄,php://output 表示直接输出到浏览器
$fp = fopen(&#39;php://output&#39;, &#39;a&#39;);  

// 写入BOM头,防止乱码
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); 

// 生成的测试数据
function test()
{
  for ($i=0; $i < 150000; $i++) {
    yield [&#39;name&#39;, $i, &#39;男&#39;];
  }
}

// 表头
$headers = [&#39;名字&#39;, &#39;年龄&#39;, &#39;性别&#39;];

fputcsv($fp, $headers);

foreach (test() as $value) {
  fputcsv($fp, $value);
}

fclose($fp);
?>
Copy after login

Using it with chunk in laravel can easily and quickly export all data.

Related recommendations:

Sharing tips on how to export data to excel using Python

Speculative: How to use PHP to export data Export to Excel table (graphic explanation)

php exports data to execl file format

The above is the detailed content of Detailed explanation of PHP data export knowledge points. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template