메뉴 건너뛰기

프로그램언어

조회 수 20289 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

CodeIgniter에서 엑셀 파일을 써야 할 일이 있어서..

사용하는 방법을 찾다가 정리를 해 봅니다.


여기서 사용하는 엑셀 사용 클래스는 PHPExcel을 사용하는데..

2012년 6월 19일부로 github로 홈페이지를 이사 했습니다.


그런데..github에서 tag의 최신 버전이.. 1.7.8rc1이더군요..

왠만하면 서비스에서는 rc나 beta는 잘 쓰질 않아서..

여기서는 이전 홈페이지의 가장 마지막 버전인 1.7.7을 사용 했습니다.


사용버전

  • CodeIgniter : 2.1.2
  • PHPExcel : 1.7.7

먼저 다운로드 받은 phpexcel의 압축을 풀어 줍니다.

그리고, 압축을 푼 폴더에서 Classes 폴더의 내용을 application/third_part 폴더에 복사해 넣습니다.



application/libraries/  폴더에 Excel.php 파일을 만들어 줍니다.


<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
require_once APPPATH."/third_party/PHPExcel.php";
 
class Excel extends PHPExcel {
    public function __construct() {
        parent::__construct();
    }
}


설치는 완료 되었습니다.

아래는 간단한 예제입니다.


파일 읽기

// PHPExcel 라이브러리 로드
$this->load->library('excel');
// 엑셀 파일 읽기
$objPHPExcel = PHPExcel_IOFactory::load(APPPATH.'upload/example1.xls');



// 엑셀 내용을 배열로 바꾸기
$sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

echo '<hr />';
echo '<pre>';
var_dump($sheetData);
echo '</pre>';


엑셀 파일 쓰기

// PHPExcel 라이브러리 로드
$this->load->library('excel');
// 워크시트에서 1번째는 활성화
$this->excel->setActiveSheetIndex(0);
// 워크시트 이름 지정
$this->excel->getActiveSheet()->setTitle('테스트 워크시트');
// A1의 내용을 입력 합니다.
$this->excel->getActiveSheet()->setCellValue('A1', '여기에 텍스트 입력');
// A1의 폰트를 변경 합니다.
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
// A1의 글씨를 볼드로 변경합니다.
$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
// A1부터 D1까지 셀을 합칩니다.
$this->excel->getActiveSheet()->mergeCells('A1:D1');
// A1의 컬럼에서 가운데 쓰기를 합니다.
$this->excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
 
$filename='just_some_random_name.xls'; // 엑셀 파일 이름
header('Content-Type: application/vnd.ms-excel'); //mime 타입
header('Content-Disposition: attachment;filename="'.$filename.'"'); // 브라우저에서 받을 파일 이름
header('Cache-Control: max-age=0'); //no cache
            
// Excel5 포맷으로 저장 엑셀 2007 포맷으로 저장하고 싶은 경우 'Excel2007'로 변경합니다.
$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5'); 
// 서버에 파일을 쓰지 않고 바로 다운로드 받습니다.
$objWriter->save('php://output');

  1. PHP 에서의 소켓(Socket) 통신

    Date2014.04.12 Views20395
    Read More
  2. 웹에서 Excel 로 출력하기

    Date2014.03.26 Views20356
    Read More
  3. mysql 에러 구문 표시

    Date2014.02.27 Views20349
    Read More
  4. jquery 이용 아이디 중복체크 실시간

    Date2014.04.12 Views20341
    Read More
  5. [PHP] 서버 운영하시는분 서버 꺼졌을때 메시지 띄우기

    Date2015.04.06 Views20324
    Read More
  6. fopen 파일 열기

    Date2015.04.14 Views20297
    Read More
  7. CodeIgniter에서 PHPExcel 사용하기

    Date2017.03.06 Views20289
    Read More
  8. php 파일 확장자

    Date2014.02.27 Views20234
    Read More
  9. 깨진 한글 체크

    Date2016.12.23 Views20223
    Read More
  10. 파일 업로드 (중복처리)

    Date2017.03.06 Views20184
    Read More
  11. MySQL DB 중복여부 검사하여 없는 것만 추가

    Date2015.04.14 Views20157
    Read More
  12. 파일 이름에서 확장자 추출마스터

    Date2017.03.07 Views20061
    Read More
  13. 정규표현식 매치를 수행 (preg_match)

    Date2016.12.23 Views20051
    Read More
  14. 날짜 일수 차이 계산

    Date2017.03.07 Views19977
    Read More
  15. PHP Mcrypt 라이브러리를 활용한 암호화 시스템

    Date2016.12.22 Views19972
    Read More
  16. implode — 문자열로 배열 원소를 결합

    Date2016.12.23 Views19951
    Read More
  17. explode - 문자열 나눔

    Date2016.12.23 Views19943
    Read More
  18. class_exists 클래스가 정의되었는지 확인

    Date2016.12.23 Views19881
    Read More
  19. 웹페이지 프린트 하기 html 수준

    Date2014.03.26 Views19805
    Read More
  20. php 파일 다운로드 구현

    Date2014.02.27 Views19794
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved