PHP

38. PHP PhpSpreadsheet 설치하기 & 예제 코드

drizzle0925 2021. 9. 9. 18:27
728x90

PhpSpreadsheet

PHPExcel이 deprecated 되어 PHP 7.2 이상에서 지원이 안되므로 PHPSpreadsheet를 설치합니다.

 

 

PhpSpreadsheet composer 없이 다운로드 가능한 사이트

https://php-download.com/package/phpoffice/phpspreadsheet

 

▷ Download the PHP library phpoffice/phpspreadsheet +++ One click!

Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.

php-download.com

 

 

PhpSpreadsheet github

https://github.com/PHPOffice/PhpSpreadsheet

 

GitHub - PHPOffice/PhpSpreadsheet: A pure PHP library for reading and writing spreadsheet files

A pure PHP library for reading and writing spreadsheet files - GitHub - PHPOffice/PhpSpreadsheet: A pure PHP library for reading and writing spreadsheet files

github.com

 

 

PhpSpreadsheet 설치 안내 가이드

https://phpspreadsheet.readthedocs.io/en/latest/

 

Welcome to PhpSpreadsheet's documentation - PhpSpreadsheet Documentation

Welcome to PhpSpreadsheet's documentation PhpSpreadsheet is a library written in pure PHP and offers a set of classes that allow you to read and write various spreadsheet file formats such as Excel and LibreOffice Calc. File formats supported Format Readin

phpspreadsheet.readthedocs.io


composer를 이용하여 project에 PhpSpreadsheet를 설치

 

 

설치 명령어

composer require phpoffice/phpspreadsheet

 

 

 

PDF 파일은 수정할 예정이 없기 때문에 mpdf/mpdf, dompdf/dompdf, tecnickcom/tcpdf, jpgraph/jpgraph는 설치를 건너뜁니다.

 

 

설치되었습니다.


예제 1

Hello world 엑셀로 다운로드하기

 

 

<?php

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');

$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');

 

 

위 파일을 실행시켰더니 Permission 에러가 발생했습니다. (권한 문제)

 

 

. 폴더의 권한을 757로 변경했더니 정상적으로 다운로드되었습니다.

(폴더의 소유자 그룹(chown)을 변경하던가 폴더의 접근(chmod) 권한을 변경한다)

 

 

결과물


예제 2

기본 데이터 출력하기

 

<?php 
require_once $_SERVER['DOCUMENT_ROOT'].'/vendor/autoload.php';

$datas = array(
    array('name' => '김철수', 'tel' => '010-1234-5678', 'age' => '24'),
    array('name' => '박영희', 'tel' => '010-5678-1234', 'age' => '22')
);

$cells = array(
    'A' => array(14, 'name', '신청자명'),
    'B' => array(16, 'tel',  '전화번호'),
    'C' => array(10, 'age', '나이')
);

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

foreach ($cells as $key => $val) {
    $cellName = $key.'1';

    $sheet->getColumnDimension($key)->setWidth($val[0]);
    $sheet->getRowDimension('1')->setRowHeight(25);
    $sheet->setCellValue($cellName, $val[2]);
    $sheet->getStyle($cellName)->getFont()->setBold(true);
    $sheet->getStyle($cellName)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
    $sheet->getStyle($cellName)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
}

for ($i = 2; $row = array_shift($datas); $i++) {
    foreach ($cells as $key => $val) {
        $sheet->setCellValue($key.$i, $row[$val[1]]);
    }
}

$filename = 'excel';

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment; filename="'.$filename.'.xlsx"');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
?>

 

결과물

728x90