用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

用PHPExcel将数组输出为excel文件

2014-12-03 作者: php源代码大全举报

[php]代码库

<?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("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 = '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;

    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('A1', 'Hello');

// Rename sheet

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

// 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');

}
//该片段来自于http://yuncode.net


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...