码迷,mamicode.com
首页 > Web开发 > 详细

PHP导出生成Excel文件

时间:2017-05-25 11:54:17      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:max-age   exce   width   osi   font   for   obj   cells   xpl   

PHP导出生成Excel首先需要下载安装office扩展

可以自行下载,也可以通过composer下载安装

个人比较建议后者

代码很简单  不废话直接上代码


/**
* @param $file_name String 文件名称
* @param $title String 标题
* @param $order String 中文的表格序以逗号方式连接
* @param $data array 数据集
* @param $data_order String 数据集合key以逗号的方式连接
*/
function export($file_name,$title,$order,$data,$data_order) {

$objPHPExcel = new PHPExcel();
$letter = array();
for($j = 65;$j < 91;$j++) {
$letter[] = chr($j);
}
/**
* setCreator 作者
* setTitle 标题
* setSubject 主题
* setDescription 备注
* setKeywords 关键字
* setCategory 类别
*/
$objPHPExcel->getProperties()->setCreator("alading")
->setLastModifiedBy("alading")
->setTitle("Office 2007 XLSX")
->setSubject("Office 2007 XLSX")
->setDescription("Office 2007 XLSX")
->setKeywords("office 2007")
->setCategory("alading file");

$order_arr = explode(‘,‘,$order);

foreach ($order_arr as $key => $value) {
// 设置宽
$objPHPExcel->getActiveSheet()->getColumnDimension($letter[$key])->setWidth(20);
// 设置水平居中
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle($letter[$key])->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
}

// 设置行高度
$objPHPExcel->getActiveSheet()->getRowDimension(‘1‘)->setRowHeight(22);
$objPHPExcel->getActiveSheet()->getRowDimension(‘2‘)->setRowHeight(20);
$data_order = explode(‘,‘,$data_order);
//设置最后的样式
end($data_order);
$end_key = ‘:‘ . $letter[key($data_order)];

// 字体和样式
$objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
$objPHPExcel->getActiveSheet()->getStyle(‘A2‘.$end_key.‘2‘)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘A1‘)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘A2‘.$end_key.‘2‘)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘A2‘.$end_key.‘2‘)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

// 合并
$objPHPExcel->getActiveSheet()->mergeCells(‘A1‘.$end_key.‘1‘);

// 表头
$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1‘, $title);
foreach ($order_arr as $k => $v) {
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue($letter[$k].‘2‘, $v);
}

for ($i = 0, $len = count($data); $i < $len; $i++) {
foreach ($data_order as $da => $vo) {
$objPHPExcel->getActiveSheet(0)->setCellValue($letter[$da]. ($i + 3), $data[$i][$vo]);
}
$objPHPExcel->getActiveSheet()->getStyle(‘A‘ . ($i + 3) . $end_key . ($i + 3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘A‘ . ($i + 3) . $end_key . ($i + 3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getRowDimension($i + 3)->setRowHeight(16);
}
// 重命名表名称
$objPHPExcel->getActiveSheet()->setTitle($title);
// 表索引设置为第一个表
$objPHPExcel->setActiveSheetIndex(0);
// 输出
header(‘Content-Type: application/vnd.ms-excel‘);
header(‘Content-Disposition: attachment;filename="‘ . $file_name . ‘.xls"‘);
header(‘Cache-Control: max-age=0‘);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);

$objWriter->save(‘php://output‘);
}

PHP导出生成Excel文件

标签:max-age   exce   width   osi   font   for   obj   cells   xpl   

原文地址:http://www.cnblogs.com/wlsphper/p/6902330.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!