Home >php教程 >PHP源码 >将数组输出为excel文件

将数组输出为excel文件

PHP中文网
PHP中文网Original
2016-05-25 17:01:491663browse

将数组输出为excel文件

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

/** PHPExcel */
include FCPATH.&#39;vendor/Excel/excel/PHPExcel.php&#39;;
include FCPATH.&#39;vendor/Excel/excel/PHPExcel/Writer/Excel5.php&#39;;


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

$objPHPExcel = new PHPExcel();

// Set properties

$objPHPExcel->getProperties()->setCreator("Michael");
$objPHPExcel->getProperties()->setLastModifiedBy("MOT");
$objPHPExcel->getProperties()->setTitle("36lean email list");
$objPHPExcel->getProperties()->setSubject("Emails");
$objPHPExcel->getProperties()->setDescription("Array to Excel");

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

    $objPHPExcel->getActiveSheet()->SetCellValue( $basic.&#39;1&#39;, $c);
    
    $basic ++ ;
}

$max_length = count( $datalist) - 1;

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

    $basic = 65;

    var_dump( $datalist[$count]);

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

        $basic = chr( $basic);

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

        $basic = ord( $basic) + 1;
    }

    $basic = 65;
}


//$objPHPExcel->getActiveSheet()->SetCellValue(&#39;A1&#39;, &#39;Hello&#39;);

// Rename sheet

$objPHPExcel->getActiveSheet()->setTitle(&#39;Simple&#39;);

        
// Save Excel 2007 file
$fhandle = opendir(&#39;data/download/&#39;);

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

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

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

}

 以上就是将数组输出为excel文件的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn