Simple use of PhpSpreadsheet

 

Since PHPExcel no longer maintained, PhpSpreadsheet is the next version of PHPExcel. PhpSpreadsheet is a library written in pure PHP, and the introduction of namespaces, PSR norms. Here a brief introduction of import and export functions under PhpSpreadsheet.

1, the installation

  • Use composer Installation:
composer require phpoffice/phpspreadsheet
  • GitHub Download:

   https://github.com/PHPOffice/PhpSpreadsheet

 

2, excel file export

/ * * 
 * Excel file export 
 * / 
function Export () 
{ 
    require_once __DIR__ '/vendor/autoload.php'. ; 
 
    $ Data = [ 
        [ 'TITLE1' => '111', 'title2' => '222'], 
        [ 'TITLE1' => '111', 'title2' => '222'], 
        [ 'TITLE1' => '111', 'title2' => '222' ] 
    ]; 
    $ title = [ 'first row header ',' second row header ' ]; 
 
    // the Create new new Spreadsheet Object 
    $ Spreadsheet = new new \ PhpOffice \ PhpSpreadsheet \ Spreadsheet ();
    $sheet = $spreadsheet->getActiveSheet (); 
 
    // method using setCellValueByColumnAndRow 
    // Header 
    // cell contents 
    the foreach ( $ title  AS  $ Key => $ value ) {
         // cell contents written 
        $ Sheet -> setCellValueByColumnAndRow ( $ Key + . 1,. 1, $ value ); 
    } 
    $ row = 2; // from the second line 
    the foreach ( $ Data  AS  $ Item ) {
         $ column =. 1 ;
         the foreach ( $ Item  AS  $ value ) {
             //Cell contents written 
            $ Sheet -> setCellValueByColumnAndRow ( $ column , $ Row , $ value );
             $ column ++ ; 
        } 
        $ Row ++ ; 
    } 
 
    // Second method, using setCellValue 
    // Header 
    // cell contents 
    titCol $ = 'A' ;
     the foreach ( $ title  AS  $ Key => $ value ) {
         // cell contents written 
        $ Sheet -> setCellValue ( $ titCol '. 1',. $ value );
         $ titCol ++ ;
    }
    Row $ = 2; // second line from 
    the foreach ( $ Data  AS  $ Item ) {
         $ DataCol = 'A' ;
         the foreach ( $ Item  AS  $ value ) {
             // cell contents written 
            $ Sheet -> setCellValue ( DataCol $ . $ Row , $ value );
             $ DataCol ++ ; 
        } 
        $ Row ++ ; 
    } 
 
    // the Redirect to Output A apos Web Browser Client (Xlsx) 
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="01simple.xlsx"');
    header('Cache-Control: max-age=0');
    // If you're serving to IE 9, then the following may be needed
    header('Cache-Control: max-age=1');
 
    // If you're serving to IE over SSL, then the following may be needed
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
    header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
    header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
    header('Pragma: public'); // HTTP/1.0
 
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('php://output');
    exit;
}

result:

 

3, save the file to your local excel

/ * * 
 * Excel file saved locally 
 * / 
function Save () 
{ 
    require_once __DIR__ '/vendor/autoload.php'. ; 
 
    $ Data = [ 
        [ 'TITLE1' => '111', 'title2' => '222' ], 
        [ 'TITLE1' => '111', 'title2' => '222'], 
        [ 'TITLE1' => '111', 'title2' => '222' ] 
    ]; 
    $ title = [ 'first row header ',' second row header ' ]; 
 
    // the Create new new Spreadsheet Object 
    $ Spreadsheet = new new \ PhpOffice \ PhpSpreadsheet \ Spreadsheet ();
    $sheet = $spreadsheet->getActiveSheet (); 
 
    // Header 
    // cell contents 
    $ titCol = 'A' ;
     the foreach ( $ title  AS  $ Key => $ value ) {
         // cell contents written 
        $ Sheet -> setCellValue ( $ titCol . '. 1', $ value );
         $ titCol ++ ; 
    } 
    $ row = 2; // second line from 
    the foreach ( $ Data  AS  $ Item ) {
         $ DataCol = 'A' ;
         the foreach ( $ Item  AS  $ value) {
            // 单元格内容写入
            $sheet->setCellValue($dataCol . $row, $value);
            $dataCol++;
        }
        $row++;
    }
 
    // Save
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
    $writer->save('01simple.xlsx');
}

 

4, read the contents of the file excel

/**
 * 读取excel文件内容
 */
function read()
{
    require_once __DIR__ . '/vendor/autoload.php';
    $inputFileName = dirname(__FILE__) . '/01simple.xlsx';
    $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
    // 方法二
    $sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
    return $sheetData;
}

result:

 

possible problems:

1、Fatal error: Uncaught Error: Class 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found

This is because there is no automatic load. You can load the file manually introduced.

require_once __DIR__ . '/vendor/autoload.php';

or:

require_once __DIR__ . '/vendor/phpoffice/phpspreadsheet/src/Bootstrap.php';

 

2、Fatal error: Interface 'Psr\SimpleCache\CacheInterface' not found

This is because there is no psr documents, the lack of simple-cache module. If a composer is installed, then automatically generated. If not, you can manually download.

GitHub Download: https://github.com/php-fig/simple-cache/releases

 

Guess you like

Origin www.cnblogs.com/woods1815/p/11372007.html