PHPExcel 설치

PHPExcel 설치
PHPExcel 시작하기

PHPExcel.png

1 다운로드[ | ]

2 설치[ | ]

[root@zetawiki vendor]# ll PHPExcel_1.8.0_doc.zip 
-rw-r--r-- 1 root root 5410110 Aug 20 15:45 PHPExcel_1.8.0_doc.zip
  • 하위폴더 만들고 압축해제
[root@zetawiki vendor]# mkdir PHPExcel
[root@zetawiki vendor]# unzip PHPExcel_1.8.0_doc.zip -dPHPExcel
Archive:  PHPExcel_1.8.0_doc.zip
  inflating: PHPExcel/changelog.txt  
  inflating: PHPExcel/install.txt    
  inflating: PHPExcel/license.txt    
... (생략)
  inflating: PHPExcel/Examples/templates/36writeLineChart1.xlsx  
  inflating: PHPExcel/Examples/XMLReader.php  
  inflating: PHPExcel/Examples/XMLTest.xml

3 쉘 테스트[ | ]

[root@zetawiki vendor]# cd /var/www/html
[root@zetawiki html]# mkdir -p ex/phpexcel
[root@zetawiki html]# cd ex/phpexcel
[root@zetawiki phpexcel]# pwd
/var/www/html/ex/phpexcel
[root@zetawiki phpexcel]# vi phpexcel1.php
<?php
include 'vendor/PHPExcel/Classes/PHPExcel.php';
function column_char($i) { return chr( 65 + $i ); }
 
// 자료 생성
$headers = array('ID','부서ID','이름','이메일','나이');
$rows = array(
	array(1, 1, '한놈', 'maarten@example.com', 24),
	array(2, 1, '두시기', 'paul@example.com', 30),
	array(3, 2, '석삼', 'bill.a@example.com', 29),
	array(4, 3, '석삼', 'bill.g@example.com', 28),
);
$data = array_merge(array($headers), $rows);
 
// 스타일 지정
$widths = array(6, 8, 8, 30, 6);
$header_bgcolor = 'FFABCDEF';
 
// 엑셀 생성
$last_char = column_char( count($headers) - 1 );
 
$excel = new PHPExcel();
$excel->setActiveSheetIndex(0)->getStyle( "A1:${last_char}1" )->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB($header_bgcolor);
$excel->setActiveSheetIndex(0)->getStyle( "A:$last_char" )->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setWrapText(true);
foreach($widths as $i => $w) $excel->setActiveSheetIndex(0)->getColumnDimension( column_char($i) )->setWidth($w);
$excel->getActiveSheet()->fromArray($data,NULL,'A1');
$writer = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$writer->save('test.xlsx');
echo "Done...\n";
  • 쉘에서 실행하고 test.xlsx이 생성되었는지 확인
[root@zetawiki phpexcel]# php phpexcel1.php
Done...
[root@zetawiki phpexcel]# ll test.xlsx
-rw-r--r-- 1 root root 6571 Aug 31 22:16 test.xlsx
  • test.xlsx 파일을 내려받아 내용 확인
  • 다음의 웹 테스트를 위해 test.xlsx 삭제
[root@zetawiki phpexcel]# rm -f test.xlsx

4 웹 테스트 (오류)[ | ]

[root@zetawiki phpexcel]# cat /var/log/httpd/error_log | grep phpexcel1
[Sat Jun 20 21:23:44 2015] [error] [client 127.0.0.1] PHP Fatal error:  Uncaught exception 'PHPExcel_Writer_Exception' with message 'Could not close zip file test.xlsx.' in /usr/share/php/vendor/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php:348\nStack trace:\n#0 /var/www/html/ex/phpexcel/phpexcel1.php(28): PHPExcel_Writer_Excel2007->save('test.xlsx')\n#1 {main}\n  thrown in /usr/share/php/vendor/PHPExcel/Classes/PHPExcel/Writer/Excel2007.php on line 348

5 오류 해결 (권한 조정)[ | ]

httpd[2]에게 해당 폴더의 쓰기 권한이 없기 때문에 오류가 발생한다.

[root@zetawiki ~]# ps -ef | grep httpd | grep -v grep
root      1750     1  0 Jun13 ?        00:00:00 /usr/sbin/httpd
apache    6874  1750  0 Jun19 ?        00:03:56 /usr/sbin/httpd
apache   18185  1750  0 08:29 ?        00:00:36 /usr/sbin/httpd
apache   18391  1750  0 08:48 ?        00:00:33 /usr/sbin/httpd
apache   18410  1750  0 08:49 ?        00:00:34 /usr/sbin/httpd
apache   18578  1750  0 09:09 ?        00:00:31 /usr/sbin/httpd
apache   18654  1750  0 09:11 ?        00:00:34 /usr/sbin/httpd
apache   20304  1750  0 11:41 ?        00:00:22 /usr/sbin/httpd
apache   20819  1750  0 12:12 ?        00:00:20 /usr/sbin/httpd
apache   22521  1750  0 14:48 ?        00:00:07 /usr/sbin/httpd
apache   24517  1750  0 Jun18 ?        00:04:58 /usr/sbin/httpd
apache   25934  1750  0 Jun20 ?        00:02:20 /usr/sbin/httpd
apache   29616  1750  0 Jun18 ?        00:04:32 /usr/sbin/httpd
→ 최초 구동은 root가 했지만, 실제로 사용자의 요청을 받아 PHP 명령어를 수행하는 계정은 apache이다.
[root@zetawiki phpexcel]# ll /var/www/html/ex/phpexcel/ -d
drwxr-xr-x 3 root root 4096 Aug 31 22:20 /var/www/html/ex/phpexcel/
→ phpexcel 폴더의 퍼미션이 755이고, owner가 root이므로 apache 계정이 파일을 쓸 수 없는 상태이다.
[root@zetawiki phpexcel]# chown apache:apache /var/www/html/ex/phpexcel/
[root@zetawiki phpexcel]# ll /var/www/html/ex/phpexcel/ -d
drwxr-xr-x 3 apache apache 4096 Aug 31 22:20 /var/www/html/ex/phpexcel/
→ phpexcel 폴더의 소유자를 apache로 바꾸었다.

6 실행예시 (성공)[ | ]

Done...
→ 오류 없이 정상적으로 수행되었다.
  • 파일 생성 확인
[root@zetawiki phpexcel]# ll test.xlsx
-rw-r--r-- 1 apache apache 6570 Aug 31 22:33 test.xlsx

7 같이 보기[ | ]

8 주석[ | ]

  1. 폴더가 없으면 만들어서 넣자.
  2. 보다 정확하게는 httpd를 실행한 리눅스 계정
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}