PHP導出Excel表
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019/3/16
* Time: 9:41
*/
//利用excel導出插件PHPExcel
// 引入phpexcel核心類文件
require_once ‘PHPExcel.php‘;
// 實例化excel類
$objPHPExcel = new PHPExcel();
// 操作第一個工作表
$objPHPExcel->setActiveSheetIndex(0);
// 設置sheet名
$objPHPExcel->getActiveSheet()->setTitle(‘xx列表‘);
// 設置表格寬度
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(50);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(5);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(20);
// 列名表頭文字加粗
$objPHPExcel->getActiveSheet()->getStyle(‘A1:J1‘)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘A1:J1‘)->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 列名賦值
$objPHPExcel->getActiveSheet()->setCellValue(‘A1‘, ‘編號‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘B1‘, ‘姓名‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘C1‘, ‘電話‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘D1‘, ‘擅長‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘E1‘, ‘創建日期‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘F1‘, ‘審核‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘G1‘, ‘審核時間‘);
// 數據起始行
$row_num = 2;
// 向每行單元格插入數據
//foreach ($res as $value) {
// 設置所有垂直居中
$objPHPExcel->getActiveSheet()->getStyle(‘A‘ . $row_num . ‘:‘ . ‘J‘ . $row_num)->getAlignment()
->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 設置價格為數字格式
$objPHPExcel->getActiveSheet()->getStyle(‘D‘ . $row_num)->getNumberFormat()
->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
// 居中
$objPHPExcel->getActiveSheet()->getStyle(‘E‘ . $row_num . ‘:‘ . ‘H‘ . $row_num)->getAlignment()
->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 設置單元格數值
$objPHPExcel->getActiveSheet()->setCellValue(‘A‘ . $row_num, 50);
$objPHPExcel->getActiveSheet()->setCellValue(‘B‘ . $row_num, ‘nihao‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘C‘ . $row_num, ‘sssss‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘D‘ . $row_num, ‘sssss‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘E‘ . $row_num, ‘ssssss‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘F‘ . $row_num, ‘ssssssssss‘);
$objPHPExcel->getActiveSheet()->setCellValue(‘G‘ . $row_num, ‘ssssssss‘);
$row_num++;
//}
$outputFileName = ‘teacher_‘ . time() . ‘.xls‘;
$xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header(‘Content-Disposition:attachment;filename="‘ . $outputFileName . ‘"‘);
header("Content-Transfer-Encoding: binary");
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: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save("php://output");
//$xlsWriter->save("php://output");
//echo file_get_contents($outputFileName);
PHP導出Excel表