当前位置: 首页 > 工具软件 > PHPExcel > 使用案例 >

PHPExcel的使用(一)

夹谷鸿福
2023-12-01
<b>使用PHPExcel生产Excel文档</b><hr/>
<?php
//错误提示级别
error_reporting(E_All);
//引入PHPExcel类
require_once"Classes/PHPExcel.php";
//创建PHPExcel对象
$objPHPExcel = new PHPExcel();
//设置活动表单
$objPHPExcel->setActiveSheetIndex(0);
//合并单元格
$objPHPExcel->getActiveSheet()->mergeCells('A1:D2');
//设定渐变背景颜色,双色(灰)
$objPHPExcel->getActiveSheet()->getStyle('A1:D2')->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_GRADIENT_LINEAR,
            'rotation'=>90,
            'startcolo'=>array(
                //渐变开始时的颜色
                'rgb'=>'DCDCDC'
            ),
            'endcolor'=>array(
                'rgb'=>'FFFFFF'
            )
        )
    )
);
//设置字体大小
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16);
//设定A1栏显示PHPExcel测试表
$objPHPExcel->getSheet()->getCellValue('A1','PHPExcel 测试表');
//设定字体颜色
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()
->getColor->setARGB(PHPExcel_StyleColor::COLOR_BLUE);
//设定背景颜色 单色
$objPHPExcel->getActiveSheet()->getStyle('A3:D3')
->applyFromArray(
    array(
        'fill'=>array(
            'type'=>PHPExcel_Style_File::FILL_SOLID,
            'color'=>array('rgb'=>'D1EEEE')
        )
    )
);
//设定栏位值
$objPHPExcel->getSheet()->setCellValue('A3','t1');
$objPHPExcel->getSheet()->setCellValue('B3','t2');
$objPHPExcel->getSheet()->setCellValue('C3','t3');
$objPHPExcel->getSheet()->setCellValue('D3','t4');
//设定栏位宽度(自动)
$objPHPExcel->getSheet()->getColumnDimension('A')
->setAutoSize(true);
//设定第一个表为默认表
$objPHPExcel->getActiveSheetIndex(0);
//汇出成2007
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,
'Excel 2007');
$objWriter->save('17.xlsx');
//汇出成2003
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel 5');
$objWriter->save('17.xlsx');
?>
 类似资料: