TIP게시판

제목 CI 2.1.0에서 헬퍼로 사용가능한 엑셀출력 (기존 플러그인)
글쓴이 양승현 작성시각 2015/09/04 16:11:11
댓글 : 1 추천 : 0 스크랩 : 0 조회수 : 11363   RSS
/* 사용방법 */
1. autoload.php 파일에 heler에 로그추가
$autoload['helper'] = array('url', 'file', 'form',"여기에 추가");

2. 아래의 소스를 application/helpers 폴더아래 excel_helper.php로 파일을 생성

3. 실 소스에 사용시 아래와 같습니다.
...
...
디비쿼리
....
$query = $this->db->get();
to_excel($query, "엑셀파일명");

4. excel_helper.php 파일내용은 아래와 같습니다.

5. $query->field_data(); 라인에서 에러가 난다면 이전글을 확인하시고 패치 하시면 잘 됩니다.
시스템 파일을 바꾸기 싫다면 $query->field_data(); 앞에 @ 하나 붙여주시면 필드명 없이 출력이 잘 되실겁니다.

 
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
* Excel library for Code Igniter applications
* Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
*/

function to_excel($query, $filename='exceloutput')
{
     $headers = ''; // just creating the var for field headers to append to below
     $data = ''; // just creating the var for field data to append to below
     $obj = & get_instance();

     $fields = $query->field_data();
     if ($query->num_rows() == 0) {
          echo '<p>The table appears to have no data.</p>';
     } else {
          foreach ($fields as $field) {
             $headers .= $field->name . "\t";
          }

          foreach ($query->result() as $row) {
               $line = '';
               foreach($row as $value) {
                 $value = iconv('UTF-8','EUC-KR',$value);
                
                    if ((!isset($value)) OR ($value == "")) {
                         $value = "\t";
                    } else {
                         $value = str_replace('"', '""', $value);
                         $value = '"' . $value . '"' . "\t";
                    }
                    $line .= $value;
               }
               $data .= trim($line)."\n";
          }
          
          $data = str_replace("\r","",$data);
                         
          header("Content-type: application/vnd.ms-excel" ); 
          header("Content-Disposition: attachment; filename=$filename.xls");

          echo "$headers\n$data";  
     }
}
?>
 다음글 복수 파일 업로드 (멀티 파일 업로드) (2)
 이전글 CI 2.1.0 버전업후 field_data() 문제 (1)

댓글

한대승(불의회상) / 2015/09/07 11:13:19 / 추천 0
좋은 정보 감사 합니다.