说明


include ‘PHPExcel.php’;
include ‘PHPExcel/Writer/Excel2007.php’;
//或者include ‘PHPExcel/Writer/Excel5.php’; 用于输出.xls的
创建一个excel
$objPHPExcel = new PHPExcel();
// 输出Excel表格到浏览器下载
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename=”abc.xls”‘);
header(‘Cache-Control: max-age=0’);
// If you’re serving to IE 9, then the following may be needed
header(‘Cache-Control: max-age=1’);
// If you’re serving to IE over SSL, then the following may be needed
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’); // Date in the past
header(‘Last-Modified: ‘ . gmdate(‘D, d M Y H:i:s’) . ‘ GMT’); // always modified
header(‘Cache-Control: cache, must-revalidate’); // HTTP/1.1
header(‘Pragma: public’); // HTTP/1.0
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(‘php://output’);
——————————————————————————————————————–
设置excel的属性:
创建人
$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”);
最后修改人
$objPHPExcel->getProperties()->setLastModifiedBy(“Maarten Balliauw”);
标题
$objPHPExcel->getProperties()->settitle(“Office 2007 XLSX Test Document”);
题目
$objPHPExcel->getProperties()->setSubject(“Office 2007 XLSX Test Document”);
描述
$objPHPExcel->getProperties()->setDescription(“Test document for Office 2007 XLSX, generated using PHP classes.”);
关键字
$objPHPExcel->getProperties()->setKeywords(“office 2007 openxml php”);
种类
$objPHPExcel->getProperties()->setCategory(“Test result file”);
——————————————————————————————————————–
设置当前的sheet
$objPHPExcel->setActiveSheetIndex(0);
设置sheet的name
$objPHPExcel->getActiveSheet()->settitle(‘Simple’);
设置单元格的值
$objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘String’);
$objPHPExcel->getActiveSheet()->setCellValue(‘A2’, 12);
$objPHPExcel->getActiveSheet()->setCellValue(‘A3’, true);
$objPHPExcel->getActiveSheet()->setCellValue(‘C5’, ‘=SUM(C2:C4)’);
$objPHPExcel->getActiveSheet()->setCellValue(‘B8’, ‘=MIN(B2:C5)’);
合并单元格
$objPHPExcel->getActiveSheet()->mergeCells(‘A18:E22’);
分离单元格
$objPHPExcel->getActiveSheet()->unmergeCells(‘A28:B28’);
冻结窗口
$objPHPExcel->getActiveSheet()->freezePane(‘A2’);
保护cell
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells(‘A3:E13’, ‘PHPExcel’);
设置格式
// Set cell number formats
echo date(‘H:i:s’) . ” Set cell number formats\n”;
$objPHPExcel->getActiveSheet()->getstyle(‘E4’)->getNumberFormat()->setFormatCode(PHPExcel_style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicatestyle( $objPHPExcel->getActiveSheet()->getstyle(‘E4’), ‘E5:E13’ );
设置宽width
// Set column widths
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B’)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(12);
// 设置单元格高度
// 所有单元格默认高度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
// 第一行的默认高度
$objPHPExcel->getActiveSheet()->getRowDimension(‘1’)->setRowHeight(30);
设置font
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFont()->setName(‘Candara’);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFont()->setUnderline(PHPExcel_style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFont()->getColor()->setARGB(PHPExcel_style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getstyle(‘E1’)->getFont()->getColor()->setARGB(PHPExcel_style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getstyle(‘D13’)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getstyle(‘E13’)->getFont()->setBold(true);
设置align
$objPHPExcel->getActiveSheet()->getstyle(‘D11’)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getstyle(‘D12’)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getstyle(‘D13’)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_LEFT);
$objPHPExcel->getActiveSheet()->getstyle(‘A18’)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_JUSTIFY);
//垂直居中
$objPHPExcel->getActiveSheet()->getstyle(‘A18’)->getAlignment()->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
设置column的border
$objPHPExcel->getActiveSheet()->getstyle(‘A4’)->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle(‘B4’)->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle(‘C4’)->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle(‘D4’)->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getstyle(‘E4’)->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN);
设置border的color
$objPHPExcel->getActiveSheet()->getstyle(‘D13’)->getBorders()->getLeft()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getstyle(‘D13’)->getBorders()->getTop()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getstyle(‘D13’)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getstyle(‘E13’)->getBorders()->getTop()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getstyle(‘E13’)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300’);
$objPHPExcel->getActiveSheet()->getstyle(‘E13’)->getBorders()->getRight()->getColor()->setARGB(‘FF993300’);
设置填充颜色
$objPHPExcel->getActiveSheet()->getstyle(‘A1’)->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getstyle(‘A1’)->getFill()->getStartColor()->setARGB(‘FF808080’);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getstyle(‘B1’)->getFill()->getStartColor()->setARGB(‘FF808080’);
加图片
/*实例化插入图片类*/
$objDrawing = new PHPExcel_Worksheet_Drawing();
/*设置图片路径 切记:只能是本地图片*/
$objDrawing->setPath($img_val);
/*设置图片高度*/
$objDrawing->setWidth(200);
$img_height[] = $objDrawing->getHeight();
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates($img_k[$j].$i);
/*设置图片所在单元格的格式*/
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY(10);
$objDrawing->setRotation(0);
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection(50);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
导出Excel表格例子
<?php
$datas = array(
array(‘王城’, ‘男’, ’18’, ‘1997-03-13’, ‘18948348924’),
array(‘李飞虹’, ‘男’, ’21’, ‘1994-06-13’, ‘159481838924’),
array(‘王芸’, ‘女’, ’18’, ‘1997-03-13’, ‘18648313924’),
array(‘郭瑞’, ‘男’, ’17’, ‘1998-04-13’, ‘15543248924’),
array(‘李晓霞’, ‘女’, ’19’, ‘1996-06-13’, ‘18748348924’),
);
include ‘PHPExcel.php’;
include ‘PHPExcel/Writer/Excel2007.php’;
//或者include ‘PHPExcel/Writer/Excel5.php’; 用于输出.xls的
// 创建一个excel
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator(“Phpmarker”)->setLastModifiedBy(“Phpmarker”)->settitle(“Phpmarker”)->setSubject(“Phpmarker”)->setDescription(“Phpmarker”)->setKeywords(“Phpmarker”)->setCategory(“Phpmarker”);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue(‘A1’, ‘名字’)->setCellValue(‘B1’, ‘性别’)->setCellValue(‘C1’, ‘年龄’)->setCellValue(‘D1’, ‘出生日期’)->setCellValue(‘E1’, ‘电话号码’);
// Rename worksheet
$objPHPExcel->getActiveSheet()->settitle(‘Phpmarker-‘ . date(‘Y-m-d’));
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
$objPHPExcel->getActiveSheet()->freezePane(‘A2’);
$i = 2;
foreach($datas as $data){
$objPHPExcel->getActiveSheet()->setCellValue(‘A’ . $i, $data[0])->getstyle(‘A’.$i)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->setCellValue(‘B’ . $i, $data[1]);
$objPHPExcel->getActiveSheet()->setCellValue(‘C’ . $i, $data[2]);
$objPHPExcel->getActiveSheet()->setCellValueExplicit(‘D’. $i, $data[3],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle(‘D’ . $i)->getNumberFormat()->setFormatCode(“@”);
// 设置文本格式
$objPHPExcel->getActiveSheet()->setCellValueExplicit(‘E’. $i, $data[4],PHPExcel_Cell_DataType::TYPE_STRING);
$objPHPExcel->getActiveSheet()->getstyle(‘E’ . $i)->getAlignment()->setWrapText(true);
$i ;
}
$objActSheet = $objPHPExcel->getActiveSheet();
// 设置CELL填充颜色
$cell_fill = array(
‘A1’,
‘B1’,
‘C1’,
‘D1’,
‘E1’,
);
foreach($cell_fill as $cell_fill_val){
$cellstyle = $objActSheet->getstyle($cell_fill_val);
// background
// $cellstyle->getFill()->setFillType(PHPExcel_style_Fill::FILL_SOLID)->getStartColor()->setARGB(‘fafa00’);
// set align
$cellstyle->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_LEFT)->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
// font
$cellstyle->getFont()->setSize(12)->setBold(true);
// border
$cellstyle->getBorders()->getTop()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB(‘FFFF0000’);
$cellstyle->getBorders()->getBottom()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB(‘FFFF0000’);
$cellstyle->getBorders()->getLeft()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB(‘FFFF0000’);
$cellstyle->getBorders()->getRight()->setBorderstyle(PHPExcel_style_Border::BORDER_THIN)->getColor()->setARGB(‘FFFF0000’);
}
$objPHPExcel->getActiveSheet()->getRowDimension(‘1’)->setRowHeight(30);
$objActSheet->getColumnDimension(‘A’)->setWidth(18.5);
$objActSheet->getColumnDimension(‘B’)->setWidth(23.5);
$objActSheet->getColumnDimension(‘C’)->setWidth(12);
$objActSheet->getColumnDimension(‘D’)->setWidth(12);
$objActSheet->getColumnDimension(‘E’)->setWidth(12);
$filename = ‘2015030423’;
ob_end_clean();//清除缓冲区,避免乱码
header(‘Content-Type: application/vnd.ms-excel’);
header(‘Content-Disposition: attachment;filename=”‘.$filename.’.xls”‘);
header(‘Cache-Control: max-age=0’);
// If you’re serving to IE 9, then the following may be needed
header(‘Cache-Control: max-age=1’);
// If you’re serving to IE over SSL, then the following may be needed
header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’); // Date in the past
header(‘Last-Modified: ‘ . gmdate(‘D, d M Y H:i:s’) . ‘ GMT’); // always modified
header(‘Cache-Control: cache, must-revalidate’); // HTTP/1.1
header(‘Pragma: public’); // HTTP/1.0
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(‘php://output’);
?>

使用


if (!empty($arrDownloadInfo)) {
            // halt($arrDownloadInfo);
            $arrDownloadData = array();
            foreach ($arrDownloadInfo as $k => $v) {
                $arrTmp = [];
                $arrTmp[‘name’] = $v[‘name’];
                $arrTmp[‘sex’] = $v[‘sex’];
                $arrTmp[‘ctype’] = $v[‘ctype’];
                $arrTmp[‘cnumner’] = $v[‘cnumner’];
                $arrTmp[‘birthday’] = $v[‘birthday’];
                $arrTmp[‘country’] = $v[‘country’];
                $arrTmp[‘marry’] = $v[‘marry’];
                $arrTmp[‘native_place’] = $v[‘native_place’];
                $arrTmp[‘house_national’] = $v[‘house_national’];
                $arrTmp[‘mianmao’] = $v[‘mianmao’];
                $arrTmp[‘nation’] = $v[‘nation’];
                $arrTmp[‘vocation’] = $v[‘vocation’];
                $arrTmp[‘edu’] = $v[‘edu’];
                $arrTmp[‘special_group’] = $v[‘special_group’];
                $arrTmp[‘wait’] = $v[‘wait’];
                $arrTmp[‘mobile’] = $v[‘person_concat’][‘mobile’];
                $arrTmp[’email’] = $v[‘person_concat’][’email’];
                $arrTmp[‘econtact’] = $v[‘person_concat’][‘econtact’];
                $arrTmp[‘econtact_tel’] = $v[‘person_concat’][‘econtact_tel’];
                $arrTmp[‘bank’] = $v[‘person_concat’][‘bank’];
                $arrTmp[‘bankuser’] = $v[‘person_concat’][‘bankuser’];
                $arrTmp[‘place’] = $v[‘person_concat’][‘place’];
                $arrTmp[‘detai_place’] = $v[‘person_concat’][‘detai_place’];
                $arrTmp[‘location’] = $v[‘person_concat’][‘location’];
                $arrTmp[‘detai_location’] = $v[‘person_concat’][‘detai_location’];
                $arrTmp[‘address’] = $v[‘person_concat’][‘address’];
                $arrTmp[‘detai_address’] = $v[‘person_concat’][‘detai_address’];
                $arrTmp[‘qq’] = $v[‘person_concat’][‘qq’];
                $arrTmp[‘weixin’] = $v[‘person_concat’][‘weixin’];
                $arrTmp[‘nature’] = $v[‘person_post’][‘nature’];
                $arrTmp[‘entry_time’] = $v[‘person_post’][‘entry_time’];
                $arrTmp[‘job_number’] = $v[‘person_post’][‘job_number’];
                $arrTmp[‘probation’] = $v[‘person_post’][‘probation’];
                $arrTmp[‘correction_time’] = $v[‘person_post’][‘correction_time’];
                $arrTmp[‘sj_correction_time’] = $v[‘person_post’][‘sj_correction_time’];
                $arrTmp[‘job_status’] = $v[‘person_post’][‘job_status’];
                $arrTmp[‘quit_time’] = $v[‘person_post’][‘quit_time’];
                $arrTmp[‘quit_why’] = $v[‘person_post’][‘quit_why’];
                $arrTmp[‘sj_quit_time’] = $v[‘person_post’][‘sj_quit_time’];
                $arrTmp[‘is_jshebao’] = $v[‘person_post’][‘is_jshebao’];
                $arrTmp[‘start_shebao’] = $v[‘person_post’][‘start_shebao’];
                $arrTmp[‘is_shebao’] = $v[‘person_post’][‘is_shebao’];
                $arrTmp[‘stop_shebao’] = $v[‘person_post’][‘stop_shebao’];
                $arrTmp[‘is_jgjj’] = $v[‘person_post’][‘is_jgjj’];
                $arrTmp[‘start_gjj’] = $v[‘person_post’][‘start_gjj’];
                $arrTmp[‘is_gjj’] = $v[‘person_post’][‘is_gjj’];
                $arrTmp[‘stop_gjj’] = $v[‘person_post’][‘stop_gjj’];
                $arrTmp[‘dept_cn’] = $v[‘person_post’][‘dept_cn’];
                $arrTmp[‘gw_cn’] = $v[‘person_post’][‘gw_cn’];
                $arrTmp[‘zhiwu’] = $v[‘person_post’][‘zhiwu’];
                $arrTmp[‘contract_code’] = $v[‘person_post’][‘contract_code’];
                $arrTmp[‘contract_start’] = $v[‘person_post’][‘contract_start’];
                $arrTmp[‘contract_end’] = $v[‘person_post’][‘contract_end’];
                $arrTmp[‘t_wages’] = $v[‘person_post’][‘t_wages’];
                $arrTmp[‘c_wages’] = $v[‘person_post’][‘c_wages’];
                $arrTmp[‘addtime’] = $v[‘addtime’];
                $arrDownloadData[] = $arrTmp;
            }
            //生成Excel
            $objPHPExcel = new PHPExcel();
            //表格创建人
            $objPHPExcel->getProperties()->setCreator(“secondar”);
            //标题
            $objPHPExcel->getProperties()->settitle(“导出数据”);
            //设置表格首行标题
            $objPHPExcel->getActiveSheet()->setCellValue(‘A1’, ‘测试表头’);
            //合并第一行
            $objPHPExcel->getActiveSheet()->mergeCells(‘A1:BC1’);
            //设置第标题高度,第一行
            $objPHPExcel->getActiveSheet()->getRowDimension(‘1’)->setRowHeight(30);
            $objPHPExcel->getActiveSheet()->getstyle(‘A1’)->getAlignment()->setVertical(PHPExcel_style_Alignment::VERTICAL_CENTER);
            //设置居中
            // $objPHPExcel->getActiveSheet()->getstyle(‘A1’)->getAlignment()->setHorizontal(PHPExcel_style_Alignment::HORIZONTAL_CENTER);
            //设置单元格的值
            //设置表头
            $arrExcel= [‘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’];
            $arrData = [‘姓名’,’性别’,’证件类型’,’证件号码’,’出生日期’,’国籍’,’婚姻状况’,’籍贯’,’户籍性质’,’政治面貌’,’民族’,’职业’,’学历’,’特殊人群’,’状态’,’手机号’,’邮箱’,’紧急联系人’,’紧急联系人号码’,’开户银行’,’开户行账号’,’地省/市/区’,’地详情’,’所在地省/市/区’,’户籍所在地详情’,’联系地址省/市/区’,’联系地址详情’,’qq号’,’微信号’,’人员性质’,’任职受雇日期’,’工号’,’试用期’,’预计转正时间’,’实际转正时间’,’在职状态’,’预计离职时间’,’离职原因’,’实际离职时间’,’缴纳社保’,’始缴社保时间’,’是否停缴社保’,’停缴社保时间’,’缴公积金’,’始缴公积金时间’,’停缴公积金’,’停缴公积金时间’,’部门’,’岗位’,’职务’,’合同编码’,’合同起始时间’,’合同到期时间’,’试用期工资’,’转正工资’,’添加时间’];
            $arrFor[“newi”]=-1;
            //循环设置表头文字
            $i=0;
            $di=0;
            $arrTable = [];
            while(true){
                if($arrFor[‘newi’]>-1){
                    $str = ‘$str = $arrExcel[$arrFor[“newi”]].$arrExcel[$i];’;
                    eval($str);
                }else{
                    $str = ‘$str = $arrExcel[$i];’;
                    eval($str);
                }
                $i++;
                $objPHPExcel->getActiveSheet()->setCellValue($str.’2′, $arrData[$di]);
                $arrTable[] = $str;
                $di++;
                if($i>25){
                    $i=0;
                    $arrFor[“newi”]++;
                }
                if($arrFor[“newi”]>-1){
                    $sum = $arrFor[“newi”]+1;
                    $sum = $sum*26;
                    $sum = $sum+$i;
                    if($sum>=count($arrData)){
                        break;
                    }
                }
            }
            // halt($arrTable);
            $i = 3;
            foreach($arrDownloadData as $k => $v){
                // halt($v);
                $i1=0;
                // echo count($arrTable);
                // echo “—“;
                // echo count($arrData);
                // echo “—“;
                // echo count($v);
                // exit;
                foreach($v as $vk => $vv){
                    $objPHPExcel->getActiveSheet()->setCellValue($arrTable[$i1].$i, $vv);
                    // echo $arrTable[$i1].$i.'<br>’;
                    // echo $vk.'<br>’;
                    $i1++;
                }
                // exit;
                $i++;
            }
            // halt($arrTable);
            //
            // exit;
            ob_end_clean();
            header(‘pragma:public’);
            header(‘Cache-Control: max-age=0’);
            header(‘Cache-Control: max-age=1’);
            header(‘Expires: Mon, 26 Jul 1997 05:00:00 GMT’);
            header(‘Last-Modified: ‘ . gmdate(‘D, d M Y H:i:s’) . ‘ GMT’);
            header(‘Cache-Control: cache, must-revalidate’);
            header(‘Content-type:application/vnd.ms-excel;charset=utf-8;name=”‘.”测试导出” .’.xls”‘);
            header(“Content-Disposition:attachment;filename=”.time().”.xls”);//attachment新窗口打印inline本窗口打印
            $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
            $objWriter->save(‘php://output’);

 

 

说点什么
支持Markdown语法
好耶,沙发还空着ヾ(≧▽≦*)o
Loading...