phpExcel---自定义函数
程序员文章站
2022-03-30 15:32:48
...
require_once '../class/c_mysql.class.php';
$db = new c_mysql();
$sql = "select group_name,group_email,member_name,member_email,member_department from email_group;";
$db->connect();
$db->query($sql);
$db->close();
while($row = $db->fetch_row()){
$data[] = $row;
}
$title = array('群组名称','群组邮箱','成员姓名','成员邮箱','成员所在部门');
/*****************函数调用*********************/function exportXlsx($title,$content){
require_once '../PHPExcel/PHPExcel.php';
require_once '../PHPExcel/PHPExcel/Writer/Excel2007.php';
//创建一个excel
$objPHPExcel = new PHPExcel();
//保存excel—2007格式
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
//向excel中插入数据
$letter = range("A","Z");
$row = 2;//从第二行开始插入数据,预留出第一行插入列名
foreach($content as $line){
$i = 0;
foreach($line as $cell){
$objPHPExcel->getActiveSheet()->setCellValue($letter[$i] . $row, $cell);
$i++;
}
$row++;
}
//将标题输入到excel中
foreach($title as $key => $cell){
$objPHPExcel->getActiveSheet()->setCellValue($letter[$key] . '1', $cell);
}
//设置宽width
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(40);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(35);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(100);
//第一行居中,其他行默认
foreach($title as $key => $cell){
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . '1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . '1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . '1')->getFill()->getStartColor()->setARGB('FFC3C3C3');
//设置border的color
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . '1')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . '1')->getBorders()->getAllBorders()->getColor()->setARGB('FF000000');
}
//给其他行填充颜色
$line_count = count($content) + 2;
foreach($title as $key => $cell){
//设置border的color
for($i=2;$igetActiveSheet()->getStyle($letter[$key] . $i)->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle($letter[$key] . $i)->getBorders()->getAllBorders()->getColor()->setARGB('FF000000');
}
}
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");
$filename = '群组信息.xlsx';
$filename = iconv('UTF-8','GB2312',$filename);
header("Content-Disposition:attachment;filename=$filename");
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}
以上就介绍了phpExcel---自定义函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。
上一篇: php 文件类型的判断示例代码