/* 사용방법 */
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";
}
}
?>