欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP导出Excel表

程序员文章站 2022-08-31 23:06:41
setActiveSheetIndex(0); // 设置sheet名 $objPHPExcel->getActiveSheet()->setTitle('xx列表'); // 设置表格宽度 $objPHPExcel->getActiveSheet()->getColumnDimension('A'... ......
<?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('a')->setwidth(10);
$objphpexcel->getactivesheet()->getcolumndimension('b')->setwidth(20);
$objphpexcel->getactivesheet()->getcolumndimension('c')->setwidth(15);
$objphpexcel->getactivesheet()->getcolumndimension('d')->setwidth(50);
$objphpexcel->getactivesheet()->getcolumndimension('e')->setwidth(20);
$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);