PHPExcel一些测试

<?php
/**
 * Created by PhpStorm.
 * User: Mary
 * Date: 2017/6/25
 * Time: 9:53
 */
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

//创建Excel对象
$objPHPExcel = new PHPExcel();
//获取当前活动的sheet
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
    array(
        array('', 2010, 2011, 2012),
        array('Q1', 12, 15, 21),
        array('Q2', 56, 73, 86),
        array('Q3', 52, 61, 69),
        array('Q4', 30, 32, 0),
    )
);
//设置注释
$objPHPExcel->getActiveSheet()->getComment('B3')->setAuthor('大头');
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment('B3')->getText()->createTextRun('PHPExcel:');

$objCommentRichText->getFont()->setBold(true);

$objPHPExcel->getActiveSheet()->getComment('B3')->getText()->createTextRun("\r\n");

$objPHPExcel->getActiveSheet()->getComment('B3')->getText()->createTextRun('显示注释');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
//添加一张图片在worksheet中
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('测试添加图片');
$objDrawing->setDescription('Terms and conditions');
$objDrawing->setPath('./Examples/images/officelogo.jpg');
$objDrawing->setCoordinates('B14');
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

//添加一个富文本
$objRichText = new PHPExcel_RichText();
$objRichText->createText('This invoice is ');

$objPayable = $objRichText->createTextRun('payable within thirty days after the end of the month');
$objPayable->getFont()->setBold(true);
$objPayable->getFont()->setItalic(true);
$objPayable->getFont()->setColor( new PHPExcel_Style_Color( PHPExcel_Style_Color::COLOR_DARKGREEN ) );

$objRichText->createText(', unless specified otherwise on the invoice.');

$objPHPExcel->getActiveSheet()->getCell('A8')->setValue($objRichText);


if (file_exists("C:\Users\Mary\Desktop\demo_1.xlsx")) {
    unlink("C:\Users\Mary\Desktop\demo_1.xlsx");
}
$objWriter->save("C:\Users\Mary\Desktop\demo_1.xlsx");

建议查文档,这些都是差文档的到的。

0 条评论
发表一条评论