What is PhpSpreadsheet? how to use?

慕斯
Release: 2023-03-10 14:54:02
Original
3417 people have browsed it

The previous article introduced you to "What is Ajax?" working principle? How to use Ajax to complete a GET request", this article continues to introduce to you what is PhpSpreadsheet? how to use? (Code attached) has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What is PhpSpreadsheet? how to use?

PhpSpreadsheet introduction

##1. What is PhpSpreadsheet

  • [info] PhpSpreadsheet is a library written in pure PHP that provides a set of classes that allow you to read and write different spreadsheet file formats

  • PhpSpreadsheet provides a rich API interface, which can set many cell and document properties, including styles, pictures, dates, functions and many other applications. In short, PhpSpreadsheet can do whatever kind of Excel table you want

●PHP developed using PhpSpreadsheet requires version 7.1 or higher


●PhpSpreadsheet supports chain operations.


2. File formats supported by PhpSpreadsheet


Formats                                                                                                                                Office Open XML (.xIsx) Excel 2007 and above

BIFF 8 (.xIs) Excel 97 and above

BIFF 5 (.xIs) Excel 95

SpreadsheetML (. xm) Excel 2003

Gnumeric

HTML

SYLK

CSV

PDF

3. PhpSpreadsheet official website

##●htps://phpspreadsheet.readthedocs.io

##4. PhpSpreadsheet installation

●composer require phpoffice/phpspreadsheet

We start to learn how to use PHPspreadsheet:

First we automatically load composer The file is loaded in, and this file can call all composer plug-ins. Then we reference this file, that is, give an alias, and then we instantiate the spreadsheet object;

Let’s take the code as an example:

Automatically loaded file (autoload.php), as long as we introduce this file, it will automatically introduce all files under composer

<?php
//autoload.php @egenerated by Composer
require_once_DIR__ . &#39; /composer/autoload_real. php&#39;;
return ComposerAutoloaderInitb97eea8e7b2e6edebfeef718a4689bd::getLoader();
Copy after login

Get the workbook:

●getActiveSheet

<?php
#载入composer自动加载文件
require &#39;vendor/autoload.php&#39; ;
#给类文件的命名空间起个别名
use PhpOffice\PhpSpreadsheet\Spreadsheet;
#实例化Spreadsheet 对象
$spreadsheet = new Spreadsheet();
#获取活动工作薄
ssheet = $spreadsheet->getActiveSheet();
Copy after login

3. Get cells

●Two ways to get cells


getCell

getCellByColumnAndRow

  • <?php 
    #载入composer自动加载文件
    require &#39;vendor/autoload . php&#39;;
    #给类文件的命名空间起个别名
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    #实例化Spreadsheet对象
    $spreadsheet = new Spreadsheet();
    #获取活动工作薄
    ssheet = Sspreadsheet->getActiveSheet(); 
    #获取单元格
    $cell = $sheet->getcell(&#39;A1&#39;);
    $ce1l = ssheet->getCel1ByColumnAndRow(1.1);
    Copy after login

    4. Set cell
  • ●setValue

  • Parameter: cell value
<?php
#载入composer自动加载文件
require &#39;vendor/autoload.php&#39;;
#给类文件的命名空间起个别名
use PhpOffice\PhpSpreadsheet\Spreadsheet;
#实例化Spreadsheet 对象
$spreadsheet = new Spreadsheet();
#获取活动工作薄
$sheet■$spreadsheet->getActiveSheet(); 
t获取单元格
$ce1lA = $sheet->getCe1l(&#39;A1&#39;);
#设置单元格值
$cellA->setValue(&#39;lili&#39;);
#获取单元格
$ce11B = $sheet->getCe11ByColumnAndRow(1,2);
设置单元格值
$cellB->setVa1ue(&#39;baby&#39;);
#获取设置单元格,链式操作
$sheet->getCell(&#39;A3&#39;)->setValue(&#39;axl&#39;);
$sheet->getCellByColumnAndRow(1,4)->setValue(&#39;boy&#39;);
Copy after login

5. Get the cell value

getValueGet the cell value

getCoordinate Gets the cell coordinates

  • <?php
    #载入composer 自动加载文件
    require &#39;vendor/autoload. php&#39;;
    #给类文件的命名空间起个别名
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    #实例化Spreadsheet 对象
    $spreadsheet = new Spreadsheet();
    #获取活动工作薄
    $sheet = $spreadsheet->getActiveSheet();
    #获取单元格
    $cellA = $sheet->getCell(&#39;A1&#39;);
    #设置单元格值
    $cel1A->setValue(&#39;baby&#39;);
    echo &#39;值: &#39;,$cellA->getValue(),PHP_EOL;
    echo&#39;坐标:&#39;,$cel1A->getCoordinate();
    Copy after login

    Recommended learning: "

    PHP Video Tutorial"
  • The above is the detailed content of What is PhpSpreadsheet? how to use?. 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