<?php
/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
include FCPATH.'vendor/Excel/excel/PHPExcel.php';
include FCPATH.'vendor/Excel/excel/PHPExcel/Writer/Excel5.php';


function download(  $column , $datalist) {
// Create new PHPExcel object

$objPHPExcel = new PHPExcel();

// Set properties

$objPHPExcel->getProperties()->setCreator("MOT");
$objPHPExcel->getProperties()->setLastModifiedBy("MOT");
$objPHPExcel->getProperties()->setTitle("PHP Array To Excel");
$objPHPExcel->getProperties()->setSubject("ExcelFile");
$objPHPExcel->getProperties()->setDescription("PHP Array to Excel");

// Add some data
$objPHPExcel->setActiveSheetIndex(0);
$basic = 'A';
foreach ($column as $c) {

    $objPHPExcel->getActiveSheet()->SetCellValue( $basic.'1', $c);

    $basic ++ ;
}

$max_length = count( $datalist) - 1;

for ($count = 0; $count <= $max_length; $count++) { 

    $basic = 65;
    $num_basic = 65;



    foreach ($datalist[$count] as $key => $d) {

        $basic = $num_basic;

        $basic = big_chr( $basic);

        $d = str_replace('=', '_', $d);

        $objPHPExcel->getActiveSheet()->SetCellValue( $basic.($count+2) , $d);

        $num_basic ++;

    }

    $basic = 65;
}


//$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello');

// Rename sheet

$objPHPExcel->getActiveSheet()->setTitle('list');


// Save Excel 2007 file
$fhandle = opendir('data/download/');

while($file = readdir( $fhandle))
{
    if( is_file( 'data/download/'.$file))
    {
        unlink( 'data/download/'.$file);
    }
}

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$filename = 'data/download/'.md5(time()).'.xls';
$objWriter->save($filename);

return base_url( $filename);
//header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
//header('Cache-Control: max-age=0');

}


function big_chr( $number) {

    if( $number < 91)
        return chr( $number);

    $number = $number - 65;

    if( $number / 26 >= 1)
    {
       $size = intval( $number / 26 );

       $prefix = chr( 64 + $size );

       return $prefix . chr( 65 + $number % 26 );
    }
} 

这个我写在ci的helpers下面作为辅助函数,代码直接存为/applications/helpers/export_excel_helper.php就可以了


mot
1.2k 声望16 粉丝