2017 © Pedro Peláez
 

library php-excel

方便快捷的 Excel 导出导入类

image

purocean/php-excel

方便快捷的 Excel 导出导入类

  • Saturday, January 13, 2018
  • by purocean
  • Repository
  • 1 Watchers
  • 0 Stars
  • 430 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 12 Versions
  • 2 % Grown

The README.md

php-excel

方便快捷的 Excel 导出导入类,就俩静态方法, (*1)

安装

composer require purocean/php-excel, (*2)

简单上手

导出

use YExcel\Excel;

// 从数据库之类得到数据,数组或可遍历的生成器,支持使用闭包灵活配置每一个单元格
$data = [
    ["a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10"],
    ["b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "b10"],
    ["c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "c10"],
    [
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d1");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d2");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d3");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d4");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d5");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d6");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d7");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d8");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d9");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        },
        function ($sheet, $colNum, $rowNum) {
            $sheet->setCellValueByColumnAndRow($colNum, $rowNum, "d10");
            $fill = $sheet->getStyleByColumnAndRow($colNum, $rowNum)->getFill();
            $fill->setFillType('solid');
            $fill->getStartColor()->setARGB('FFFFEB18');
        }
    ],
];

$template = 'template.xlsx'; // 模板文件,如果不提供则新建一个 xlsx 文件

// 给浏览器发送下载头
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename=sites_export.xlsx');
header('Cache-Control: max-age=0');

// 默认跳过第一行表头
// 更多使用请查看 src\Excel 注释
Excel::put('php://output', $data, ['tplFile' => $template]);

// 多个 sheet 导出
Excel::put('php://output', [1 => $data], [multiSheet => [1 => 'test11']]);

导入

use YExcel\Excel;

// 默认跳过第一行表头
// 更多使用请查看 src\Excel 注释
foreach (Excel::get('file.xlsx') as $row) {
    var_dump($row);
    // 写入数据库之类
}

测试

cd test
php test.php

The Versions

13/01 2018

dev-master

9999999-dev https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

15/11 2017

v1.2.1

1.2.1.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

15/11 2017

v1.2.0

1.2.0.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

04/09 2017

v1.1.1

1.1.1.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

01/08 2017

v1.1.0

1.1.0.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

15/05 2017

v1.0.0

1.0.0.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

11/05 2017

v0.2.0

0.2.0.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

19/04 2017

v0.1.1

0.1.1.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

19/04 2017

v0.1.0

0.1.0.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

22/03 2017

v0.0.3

0.0.3.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

01/03 2017

v0.0.2

0.0.2.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office

04/02 2017

v0.0.1

0.0.1.0 https://github.com/purocean/php-excel

方便快捷的 Excel 导出导入类,就俩静态方法

  Sources   Download

MIT

The Requires

 

php excel xls xlsx office