$hook['pre_controller'][] = array( 'class' => 'ExcelWriter', 'function' => 'index', 'filename' => 'ExcelWriter.php', 'filepath' => 'hooks' );5. /system/application/hooks/ 디렉토리에 ExcelWriter.php 파일을 생성하고 아래와 같이 입력해주세요
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class ExcelWriter
{
/**
* includes the directory application\my_classes\Classes in your includes directory
*
*/
function index()
{
//includes the directory application\my_classes\Classes\
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/excelWriter/Classes/');
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/excelWriter/Classes/PHPExcel/');
ini_set('include_path', ini_get('include_path').PATH_SEPARATOR.BASEPATH.'application/excelWriter/Classes/PHPExcel/Writer');
}
}
?>
로더를 생성하고 클래스를 로드해서 쓰려고 하다보니 패스가 세개가 됐네요;;
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class ExcelWriterLoader {
function load($class, $options = null){
require_once($class.'.php');
$classname = $class;
if(is_null($options)){
return new $classname();
}else{
return new $classname($options);
}
}
}
?>
// Create new PHPExcel object
echo date('H:i:s') . " Create new PHPExcel object\n";
$this->load->library("excelwriterloader");
$exObj = $this->excelwriterloader->load("PHPExcel");
// Set properties
echo date('H:i:s') . " Set properties\n";
$exObj->getProperties()->setCreator("BazZ");
$exObj->getProperties()->setLastModifiedBy("BazZ");
$exObj->getProperties()->setTitle("TestExcel");
$exObj->getProperties()->setSubject("");
// Set row height
$exObj->getActiveSheet()->getRowDimension(1)->setRowHeight(50);
$exObj->getActiveSheet()->getRowDimension(2)->setRowHeight(25);
// Set column width
$exObj->getActiveSheet()->getColumnDimension('A')->setWidth(5);
$exObj->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$exObj->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$exObj->getActiveSheet()->getColumnDimension('D')->setWidth(40);
$exObj->getActiveSheet()->getColumnDimension('E')->setWidth(15);
$exObj->getActiveSheet()->getColumnDimension('F')->setWidth(15);
$exObj->getActiveSheet()->getColumnDimension('G')->setWidth(30);
$exObj->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$exObj->getActiveSheet()->getColumnDimension('I')->setWidth(20);
$exObj->getActiveSheet()->getColumnDimension('J')->setWidth(20);
$exObj->getActiveSheet()->getColumnDimension('K')->setWidth(20);
$exObj->getActiveSheet()->getColumnDimension('L')->setWidth(20);
$exObj->getActiveSheet()->getColumnDimension('M')->setWidth(20);
$exObj->getActiveSheet()->getColumnDimension('N')->setWidth(20);
//Merge cells (warning: the row index is 0-based)
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(0,1,13,1);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(0,2,13,2);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(0,3,0,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(1,3,1,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(2,3,3,3);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(2,4,2,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(3,4,3,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(4,3,4,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(5,3,5,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(6,3,6,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(7,3,9,3);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(7,4,7,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(8,4,9,4);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(10,3,10,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(11,3,11,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(12,3,12,5);
$exObj->getActiveSheet()->mergeCellsByColumnAndRow(13,3,13,5);
//Modify cell's style
$exObj->getActiveSheet()->getStyle('A1')->applyFromArray(
array(
'font' => array(
'name' => 'Times New Roman',
'bold' => true,
'italic' => false,
'size' => 20
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap' => true
)
)
);
$exObj->getActiveSheet()->getStyle('A2')->applyFromArray(
array(
'font' => array(
'name' => 'Times New Roman',
'bold' => true,
'italic' => false,
'size' => 14
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap' => true
)
)
);
$exObj->getActiveSheet()->duplicateStyleArray(
array(
'font' => array(
'name' => 'Times New Roman',
'bold' => true,
'italic' => false,
'size' => 12
),
'borders' => array(
'top' => array('style' => PHPExcel_Style_Border::BORDER_DOUBLE),
'bottom' => array('style' => PHPExcel_Style_Border::BORDER_DOUBLE),
'left' => array('style' => PHPExcel_Style_Border::BORDER_DOUBLE),
'right' => array('style' => PHPExcel_Style_Border::BORDER_DOUBLE)
),
'alignment' => array(
'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
'wrap' => true
)
),
'A3:N5'
);
// Add some data
echo date('H:i:s') . " Add some data\n";
$exObj->setActiveSheetIndex(0);
$exObj->getActiveSheet()->SetCellValue('A1', 'Try PHPExcel with CodeIgniter');
$exObj->getActiveSheet()->SetCellValue('A2',"Subtitle here");
$exObj->getActiveSheet()->SetCellValue('A3',"No.");
$exObj->getActiveSheet()->SetCellValue('B3',"Name");
$exObj->getActiveSheet()->SetCellValue('C3',"Number");
$exObj->getActiveSheet()->SetCellValue('C4',"Code");
$exObj->getActiveSheet()->SetCellValue('D4',"Register");
$exObj->getActiveSheet()->SetCellValue('E3',"Space (M2)");
$exObj->getActiveSheet()->SetCellValue('F3',"Year");
$exObj->getActiveSheet()->SetCellValue('G3',"Location");
// Rename sheet
echo date('H:i:s') . " Rename sheet\n";
$exObj->getActiveSheet()->setTitle('Try PHPExcel with CodeIgniter');
// Save Excel 2003 file
echo date('H:i:s') . " Write to Excel2003 format\n";
$objWriter = $this->excelwriterloader->load("PHPExcel_Writer_Excel5", $exObj);
//$objWriter = new PHPExcel_Writer_Excel5($exObj);
$file_name = "/files/excel/data_".date('YmdHis', time()).".xls";
$objWriter->save($_SERVER['DOCUMENT_ROOT'].$file_name);
아마.. 잘 될거라 생각합니다;한글화된 팁 감사합니다. ^^
2007은 포맷이 조금 틀릴겁니다.
// 파일 다운로드 (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="IP_LIST.xlsx"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
// < Excel 2003
//$objWriter = $this->excelwriterloader->load("PHPExcel_Writer_Excel5", $exObj);
//$file_path = "/files/excel/";
//$file_name = "name_".date('YmdHis', time()).".xls";
// > Excel 2007
$objWriter = $this->excelwriterloader->load("PHPExcel_Writer_Excel2007", $exObj);
$file_path = "/files/excel/";
$file_name = "name_".date('YmdHis', time()).".xlsx";
입니다..2) Downloaded PHPExcel 1.7.6
3) Created “pxl” folder to libraries (/application/libraries/pxl)
4) Copied PHPExcel classes to this folder, both PHPExcel.php and PHPExcel-folder (/application/libraries/pxl/PHPExcel.php & /application/libraries/pxl/PHPExcel/)
5) Created pxl.php to libraries folder with this data:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** PHPExcel */
class Pxl
{
function __construct()
{
require_once APPPATH.'/libraries/pxl/PHPExcel.php';
require_once APPPATH.'/libraries/pxl/PHPExcel/IOFactory.php';
}
} 6) Added function excel1 to welcome.php controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
}
function index()
{
$this->load->view('welcome_message');
}
function excel1()
{
$this->load->library("pxl");
// $this->load->library(“pxl/PHPExcel/iofactory”);
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setTitle("title")
->setDescription("description");
// Assign cell values
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue("A1", "cell value here 111");
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setCellValue("A1", "cell value here 222");
// Save it as an excel 2003 file
$objWriter = IOFactory::createWriter($objPHPExcel, "Excel5");
$objWriter->save("reports/nameoffile.xls");
$this->index(); //CALL DISPLAY
}
} 7) Tried to call function as http://localhost/index.php/welcome/excel1