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

php--phpexcel类生成excel文档

程序员文章站 2022-04-09 21:43:44
...
<span>shi yong phpexcel sheng cheng excel wen dang</span>
<hr>

<?php
//显示所有错误警告
error_reporting(E_ALL);
//引用,注意请自行下载PHPExcel-1.8类,并按照此路径放置在工程内
require_once 'PHPExcel-1.8/Classes/PHPExcel.php';
//实例化
$obj_phpexcel = new PHPExcel();
//设置活动页索引
$obj_phpexcel->setActiveSheetIndex(0);
//合并单元格
$obj_phpexcel->getActiveSheet()->mergeCells('A1:E2');
//单元格设置格式,粗体,居中对齐,边框,颜色
$obj_phpexcel->getActiveSheet()->getStyle('A1:E2')->applyFromArray(
    array(
        'font'=>array('bold'=>true),
        'alignment'=>array('horizontal'=>PHPExcel_Style_Alignment::HORIZONTAL_CENTER),
        'borders'=>array(
            'top'=> array('style'=>PHPExcel_Style_Border::BORDER_THIN),
            'fill'=> array('type'=>PHPExcel_Style_Fill::FILL_GRADIENT_LINEAR),
            'rotation'=> 90,
            'startcolor'=> array('rgb'=>'dcdcdc'),
            'endcolor'=> array('rgb'=>'ffffff')
        )
    )
);
//设置字号18px
$obj_phpexcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
//填入内容
$obj_phpexcel->getActiveSheet()->setCellValue('A1','I Love PHP, Do you like it?');
//获取当前页A1格的字体,设置颜色为绿色
$obj_phpexcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_GREEN);
//设置单元格为填充,颜色
$obj_phpexcel->getActiveSheet()->getStyle('A3:E3')->applyFromArray(
  array(
      'fill'=> array('type'=>PHPExcel_Style_Fill::FILL_SOLID, 'color'=> array('rgb'=>'efefef')),
  )
);
//设置单元格居中对齐
$obj_phpexcel->getActiveSheet()->getStyle('A3:E3')->applyFromArray(
    array(
        'alignment'=> array('horizontal'=>PHPExcel_Style_Alignment::HORIZONTAL_CENTER)
    )
);
//填入内容
$obj_phpexcel->getActiveSheet()->setCellValue('A3','虾');

$obj_phpexcel->getActiveSheet()->setCellValue('B3','米');

$obj_phpexcel->getActiveSheet()->setCellValue('C3','大');

$obj_phpexcel->getActiveSheet()->setCellValue('D3','王');

$obj_phpexcel->getActiveSheet()->setCellValue('E3','!');
//设置单元格列为自动大小
$obj_phpexcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
//再次设定当前页索引
$obj_phpexcel->setActiveSheetIndex(0);
//生成excel2007文档,特别注意'Excel2007'中间不能有空格
$obj_writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel2007');
//保存文档
$obj_writer->save('excel01.xlsx');
//生成excel5文档
$obj_writer = PHPExcel_IOFactory::createWriter($obj_phpexcel,'Excel5');
//保存文档
$obj_writer->save('excel02.xls');

?>


 

相关标签: php php excel