以下是本人在近期开发一个简历的excel导出功能时写过的一些PhpSpreadSheet代码,供大家参考:
安装PhpSpreadsheet,代码开始部分做如下声明:
header('Content-Type:text/html; charset=utf-8');
require 'PhpSpreadsheet-master/vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
use PhpOffice\PhpSpreadsheet\Style\Fill;
代码如下:
//设置excel属性
$objActSheet = $excel->getActiveSheet();
//设置当前的sheet
$excel->setActiveSheetIndex(0);
//设置sheet的name
$objActSheet->setTitle($sheetname);
//根据有生成的excel多少列,$letter长度要大于等于这个值
$letter1 = array('A','B','C','D','E','F');
//设置表头
$objActSheet->setCellValue("A1","编号");
$objActSheet->setCellValue("B1","地区");
$objActSheet->setCellValue("C1","企业名称");
$objActSheet->setCellValue("D1","岗位名称");
$objActSheet->setCellValue("E1","招聘人数");
$objActSheet->setCellValue("F1","联系电话");
$excel->getDefaultStyle()->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$excel->getDefaultStyle()->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//设置表头
for($i = 0;$i < count($letter1);$i++)
{
for($j = 1; $j <=1; $j++)
{
//设置表头字体样式
$objActSheet->getStyle("$letter1[$i]$j")->getFont()->setName('微软雅黑');
//设置表头字体大小
$objActSheet->getStyle("$letter1[$i]$j")->getFont()->setSize(12);
//设置表头字体是否加粗
$objActSheet->getStyle("$letter1[$i]$j")->getFont()->setBold(true);
//设置表头文字垂直居中
$objActSheet->getStyle("$letter1[$i]$j")->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
//设置文字上下居中
$objActSheet->getStyle($letter1[$i])->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
//设置表头外的文字垂直居中
$excel->setActiveSheetIndex(0)->getStyle($letter1[$i])->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
}
}
//设置各列的宽度
$objActSheet->getColumnDimension($letter1[0])->setWidth(5);//编号
$objActSheet->getColumnDimension($letter1[1])->setWidth(20);//地区
$objActSheet->getColumnDimension($letter1[2])->setWidth(50);//企业名称
$objActSheet->getColumnDimension($letter1[3])->setWidth(50);//岗位名称
$objActSheet->getColumnDimension($letter1[4])->setWidth(16);//招聘人数
$objActSheet->getColumnDimension($letter1[5])->setWidth(16);//联系电话
//设置边框
$style_array = array(
'borders' => array(
'allborders' => array(
'style' => Border::BORDER_THIN
)
) );
$objActSheet->getStyle('A1:F1')->applyFromArray($style_array);
代码如下(示例)
$j = 2;
for ($i = 0;$i < count($data);$i++,$j++)
{
$objActSheet->setCellValue("A".$j,$data[$i]['编号']);
$objActSheet->setCellValue("B".$j,$data[$i]['地区']);
$objActSheet->setCellValue("C".$j,$data[$i]['企业名称']);
$objActSheet->setCellValue("D".$j,$data[$i]['岗位名称']);
$objActSheet->setCellValue("E".$j,$data[$i]['招聘人数']);
$objActSheet->setCellValue("F".$j,$data[$i]['联系电话']);
}
该处使用的url网络请求的数据。
代码如下(示例):
ob_end_clean();
ob_start();
header('Content-Type: application/vnd.ms-excel');
//下载的excel文件名称,为Excel5,后缀为xls,不过影响似乎不大
header('Content-Disposition: attachment;filename="' . $savefile . '.xlsx"');
header('Cache-Control: max-age=0');
// 用户下载excel
$objWriter = new Xlsx($excel);
$objWriter->save('php://output');