메뉴 건너뛰기

프로그램언어

조회 수 20291 추천 수 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. $_FILES

    Date2016.12.23 Views23847
    Read More
  2. $_SERVER 함수

    Date2016.12.23 Views23943
    Read More
  3. $_SERVER 환경변수

    Date2016.09.21 Views33237
    Read More
  4. $_SERVER변수

    Date2014.02.27 Views24446
    Read More
  5. 13자리 timestamp 생성하기

    Date2020.09.28 Views654
    Read More
  6. addslashes — 문자열을 슬래시로 인용

    Date2016.12.23 Views23083
    Read More
  7. addslashes 함수의 필요성

    Date2015.04.14 Views24250
    Read More
  8. ajax refresh 시키기(자동리플래쉬) with php

    Date2017.03.06 Views23186
    Read More
  9. Ajax로 구연한 실시간 서버시간출력

    Date2017.03.06 Views21032
    Read More
  10. AJAX로 해당 페이지에서 COOKIE 사용하기

    Date2021.03.26 Views359
    Read More
  11. AJAX를 활용하여 JSON 댓글 처리하기 (PHP)

    Date2018.07.04 Views8456
    Read More
  12. array (배열)

    Date2015.04.14 Views24904
    Read More
  13. array_key_exists 배열에서 key가 존재하는지 확인

    Date2016.12.23 Views22206
    Read More
  14. array_push 배열 끝에 하나 이상의 요소를 추가

    Date2016.12.23 Views21604
    Read More
  15. array_slice 배열의 일부를 추출

    Date2016.12.23 Views20775
    Read More
  16. base64 인코딩/디코딩 함수의 특징

    Date2018.02.09 Views13080
    Read More
  17. call_user_func 사용자가 정의한 함수를 호출하여 실행고자 할 때 사용

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

    Date2016.12.23 Views19881
    Read More
  19. Class를 이용한 DB Connection 소스 (Oracle, MyS

    Date2014.02.27 Views30504
    Read More
  20. CodeIgniter - DB오류체크, 디버깅 여부 설정

    Date2021.03.29 Views496
    Read More
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved