Home Backend Development PHP Problem How to convert data to Excel format in php

How to convert data to Excel format in php

Apr 19, 2023 am 11:39 AM

In web development, it is often necessary to export data to Excel format so that users can manage and use it more conveniently. As a web development language, PHP also provides very convenient functions to export Excel. The method of exporting arrays to Excel is very practical and can quickly convert data in the website into Excel format.

1. Excel export class

PHP provides many excel export classes, among which the more common and practical ones are PHPExcel and PhpSpreadsheet. PHPExcel is an earlier open source library and has stopped updating in the latest version, so we use PhpSpreadsheet for demonstration here.

2. Demo demonstration

<?php
require_once &#39;vendor/autoload.php&#39;; // 引入类文件

use PhpOffice\PhpSpreadsheet\Spreadsheet; // 导入类名称
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; // 导入类名称

// 数据处理
$data = array(
    array(&#39;John&#39;, &#39;Doe&#39;, 28, &#39;Male&#39;),
    array(&#39;Lisa&#39;, &#39;Smith&#39;, 34, &#39;Female&#39;),
    array(&#39;Mark&#39;, &#39;Johnson&#39;, 42, &#39;Male&#39;)
);

// 创建对象
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// 写入数据
$sheet->setCellValue('A1', 'First Name');
$sheet->setCellValue('B1', 'Last Name');
$sheet->setCellValue('C1', 'Age');
$sheet->setCellValue('D1', 'Gender');

$row = 2;

foreach ($data as $rowdata) {
    $sheet->setCellValue('A' . $row, $rowdata[0]);
    $sheet->setCellValue('B' . $row, $rowdata[1]);
    $sheet->setCellValue('C' . $row, $rowdata[2]);
    $sheet->setCellValue('D' . $row, $rowdata[3]);
    $row++;
}

// 导出Excel
$filename = "demo.xlsx";
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="' . $filename . '"');
header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;
Copy after login

The above code demonstrates converting the data in a two-dimensional array $data into Excel format and exporting it. The specific process is as follows:

  1. Introduce the PhpSpreadsheet class library, and import the Spreadsheet and Xlsx classes;
  2. Define the data $data that needs to be exported;
  3. Create the Spreadsheet object and obtain Current worksheet;
  4. Write data into cells;
  5. Export Excel through Xlsx class.

Through the above code demonstration, we can draw the following conclusions:

  1. The use of PhpSpreadsheet is very simple, you only need to perform data operations in the conventional way of Excel operation;
  2. Use the header to return the exported file to the browser for download;
  3. You can use PHPExcel and PhpSpreadsheet to process more complex Excel styles.

3. Summary

Using PHP to convert arrays to Excel is a very practical function that allows us to manage and export data more quickly. In UI design, we usually use the simplest and most flexible way to present data, and then use PHP to optimize the Excel format. This not only saves the time and cost of the Web front-end, but also allows engineers to focus more on data processing. During the development process, you need to select and master the corresponding Excel export class library according to the actual situation in order to better handle various data scenarios.

The above is the detailed content of How to convert data to Excel format in php. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1255
24