PHPExcel导出实例

首先到https://github.com/PHPOffice/PHPExcel下载PHPExcel,自行用git下载。

/**
* @param $data array 具体数据
*
* @param $title array 第一行的数据
*
* @param $filename string 文件名称
*
*/

public function exportexcel($data = array(), $title = array(), $filename = ”)
{
require_once MODULE_ROOT . ‘/model/Classes/PHPExcel.php’;
require_once MODULE_ROOT . ‘/model/Classes/PHPExcel/IOFactory.php’;
require_once MODULE_ROOT . ‘/model/Classes/PHPExcel/Writer/Excel2007.php’;
ob_end_clean();

if(empty($filename)) $filename = time();
if(!is_array($title)) return false;
$header_arr = array(‘A’,’B’,’C’,’D’,’E’,’F’,’G’,’H’,’I’,’J’,’K’,’L’,’M’, ‘N’,’O’,’P’,’Q’,’R’,’S’,’T’,’U’,’V’,’W’,’X’,’Y’,’Z’);

$objPHPExcel = new PHPExcel(); //初始化PHPExcel()

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);//设置保存版本格式
$filename = $filename.’.xlsx’;

$objActSheet = $objPHPExcel->getActiveSheet();
foreach ($title as $k => $v) {
$objActSheet->setCellValue($header_arr[$k].’1′, $v);
}
$startRow = 2;
foreach ($data as $row) {
$e=0;
foreach ($row as $key => $value){
$objActSheet->setCellValue($header_arr[$e].$startRow, $value);
$e++;
}
$startRow++;
}
header(“Pragma: public”);
header(“Expires: 0”);
header(“Cache-Control:must-revalidate, post-check=0, pre-check=0”);
header(“Content-Type:application/force-download”);
header(“Content-Type:application/vnd.ms-execl”);
header(“Content-Type:application/octet-stream”);
header(“Content-Type:application/download”);;
header(‘Content-Disposition:attachment;filename=’.$filename.”);
header(“Content-Transfer-Encoding:binary”);
$objWriter->save(‘php://output’);
}

此条目发表在PHP分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注