메뉴 건너뛰기

프로그램언어

조회 수 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 문자열관련 함수

    Date2017.03.06 Views15580
    Read More
  2. php로 db 컨트롤 1

    Date2017.03.06 Views15769
    Read More
  3. PHP에서 Excel 파일을 만들 수 있는 PHPExcel

    Date2017.03.06 Views17112
    Read More
  4. PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때...

    Date2017.03.06 Views16610
    Read More
  5. PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기

    Date2017.03.06 Views18264
    Read More
  6. PHP 만년달력 소스

    Date2017.03.06 Views17063
    Read More
  7. text파일에 한줄씩 내용추가하기

    Date2017.03.06 Views17533
    Read More
  8. 지엠 웹에디터 v1.1 (저작권표시없음)|

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

    Date2017.03.06 Views21031
    Read More
  10. ajax refresh 시키기(자동리플래쉬) with php

    Date2017.03.06 Views23185
    Read More
  11. 오류 메시지 출력(alert) 및 페이지 이동(refresh) 관련

    Date2017.03.06 Views18568
    Read More
  12. 메일주소의 골뱅이를 그림처리하기

    Date2017.03.06 Views15508
    Read More
  13. PHP로 엑셀 자료 MySQL에 넣기

    Date2017.03.06 Views17875
    Read More
  14. CodeIgniter에서 PHPExcel 사용하기

    Date2017.03.06 Views20289
    Read More
  15. PHPExcel 클래스를 이용해 Excel 2007~2010 의 xlsx 파일 읽기 (100만 행 까지)

    Date2017.03.06 Views21694
    Read More
  16. phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성

    Date2017.03.06 Views22787
    Read More
  17. 파일업로드

    Date2017.02.19 Views19352
    Read More
  18. PHP에서 데이터를 엑셀(Excel)로 저장

    Date2017.02.19 Views18428
    Read More
  19. [PHP] 게시판 글쓰기와 이미지 파일 DB 저장 및 불러오기 예제

    Date2017.02.19 Views35886
    Read More
  20. array_slice 배열의 일부를 추출

    Date2016.12.23 Views20775
    Read More
Board Pagination Prev 1 ... 6 7 8 9 10 11 12 13 14 15 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved