欢迎您,零度科技专业海外服务器租用
公司电话: 24小时电话: 

当前位置:首页>网站建设教程>PHP教程>PHP 开源小项目

目录导航

PHPExcel 类库的使用技巧

台湾游戏服务器 添加于 2011-09-30  
PHP 开源小项目

PHP导出Excel选择PHPExcel的朋友相信是非常之多的,下面给大家简单的介绍一下关于PHPExcel的一些基本的函数和使用方法,具体见下面的代码:

/ ** Error reporting */
error_reporting(E_ALL);
/ ** PHPExcel */
require_once'../Classes/PHPExcel.php';
$objPHPExcel= newPHPExcel();
// 设置<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CE%C4%BC%FE">文件</SPAN>属性
$objPHPExcel->getProperties()->setCreator("C1G")
->setLastModifiedBy("C1G")
->setTitle("phpexcel Test Document")
->setSubject("phpexcel Test Document")
->setDescription("Test document for phpexcel, generated using PHP classes.")
->setKeywords("office 2007 openxml php c1gstudio")
->setCategory("Test");
//设置当前活动的sheet
$objPHPExcel->setActiveSheetIndex(0);
//设置sheet名字
$objPHPExcel->getActiveSheet()->setTitle('phpexcel demo');
//设置默认行高
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15);
//由PHPExcel根据传入内容自动判断单元格内容类型
$objPHPExcel->getActiveSheet()->setCellValue('A1', "Firstname");
$objPHPExcel->getActiveSheet()->setCellValue('B1', "Lastname");
$objPHPExcel->getActiveSheet()->setCellValue('C1', "Phone");
$objPHPExcel->getActiveSheet()->setCellValue('D1', "Fax");
$objPHPExcel->getActiveSheet()->setCellValue('E1', "Address");
$objPHPExcel->getActiveSheet()->setCellValue('F1', "ZIP");
$objPHPExcel->getActiveSheet()->setCellValue('G1', "DATE");
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(0, 8, 'firstname');
$objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow(1, 8, 'lastname');
// utf8格式字符
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A15', 'Miscellaneous glyphs')
->setCellValue('A16', 'éàèù&acirc;ê&icirc;&ocirc;&ucirc;&euml;&iuml;ü&yuml;&auml;&ouml;ü&ccedil;')
->setCellValue('A17', 'phpexcel演示');
$objPHPExcel->getActiveSheet()->setCellValue('A9', "502");
$objPHPExcel->getActiveSheet()->setCellValue('B9', "99");
$objPHPExcel->getActiveSheet()->setCellValue('C9', "=SUM(A9:B9)");
//设置列宽
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
// 设置行高
$objPHPExcel->getActiveSheet()->getRowDimension('9')->setRowHeight(20);
// 加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
// <SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D6%D0%CE%C4">中文</SPAN>
$objPHPExcel->getActiveSheet()->setCellValue('A2', "小风");
$objPHPExcel->getActiveSheet()->setCellValue('B2', "wang");
// 设置单元格格式
$objPHPExcel->getActiveSheet()->getCell('C2')->setValueExplicit('861391327543258', PHPExcel_Cell_DataType::TYPE_NUMERIC);
//设置为文本格式
$objPHPExcel->getActiveSheet()->getCell('C2')->setValueExplicit(‘010123213213321’, PHPExcel_Cell_DataType::TYPE_STRING);
// 日期
$objPHPExcel->getActiveSheet()->setCellValue('G2', '2008-12-31');
$objPHPExcel->getActiveSheet()->getStyle('G2')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
// 时间戳
$time= gmmktime(0,0,0,12,31,2008); // int(1230681600)
$objPHPExcel->getActiveSheet()->setCellValue('G3', PHPExcel_Shared_Date::PHPToExcel($time));
$objPHPExcel->getActiveSheet()->getStyle('G3')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH);
// url
$objPHPExcel->getActiveSheet()->setCellValue('G11', 'blog.c1gstudio.com');
$objPHPExcel->getActiveSheet()->getCell('G11')->getHyperlink()->setUrl('http://blog.c1gstudio.com');
// 另一个sheet
$objPHPExcel->getActiveSheet()->setCellValue('G12', 'sheetb');
$objPHPExcel->getActiveSheet()->getCell('G12')->getHyperlink()->setUrl("sheet://'sheetb'!A1");
// 水平居上
$objPHPExcel->getActiveSheet()->getStyle('A9:B9')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_TOP);
// 单元格换行
$objPHPExcel->getActiveSheet()->getStyle('G2:G3')->getAlignment()->setWrapText(true);
// 合并
$objPHPExcel->getActiveSheet()->mergeCells('A18:E22');
// 隐藏D列
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setVisible(false);
//
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setOutlineLevel(1);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setOutlineLevel(1);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setCollapsed(true);
// 固定第一行
$objPHPExcel->getActiveSheet()->freezePane('A2');
// 保护工作表
$objPHPExcel->getActiveSheet()->getProtection()->setPassword('PHPExcel');
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true);
//设置边框
$sharedStyle1= newPHPExcel_Style();
$sharedStyle1->applyFromArray(
array('borders'=> array(
'left'=> array('style'=> PHPExcel_Style_Border::BORDER_MEDIUM)
)
));
$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1, "B1:B10");
// 创建一个新的工作表
$objWorksheet1= $objPHPExcel->createSheet();
$objWorksheet1->setTitle('sheetb');
$objPHPExcel->setActiveSheetIndex(1);
// 创建一个图片
$gdImage= @imagecreatetruecolor(200, 20) ordie('Cannot Initialize new GD image stream');
$textColor= imagecolorallocate($gdImage, 255, 255, 255);
imagestring($gdImage, 1, 5, 5, 'Created with PHPExcel (c1gstudio.com)', $textColor);
// 把创建的图片添加到工作表
$objDrawing= newPHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Sample image');
$objDrawing->setDescription('Sample image');
$objDrawing->setImageResource($gdImage);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_JPEG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(36);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
$objPHPExcel->setActiveSheetIndex(0);
// 保存
$objWriter= PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('testexcel'.time().'.xls');
/ *
//在<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%E4%AF%C0%C0%C6%F7">浏览器</SPAN>导出
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="testexcel.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
*/ / ** * $Log$ */

零度科技专业提供香港服务器美国服务器等海外服务器租用,程序开发,插件制作等业务,详情请咨询零度科技在线客服。

原文来自零度科技:http://www.ldisp.com/a/php-project/2011/1086.shtml

上一篇:phpmailer所有对象和属性 中文
下一篇:phpmyadmin如何设置不用登陆

 
了解零度?

零度科技主营全球服务器租用,如果您有服务器租用需求请联系

海外服务器租用

推荐香港、美国、韩国。提供站群服务器、高防服务器租用

海外特价服务器

关注零度特价服务器频道,了解全球特价服务器。

574

业务咨询

    技术服务

      特价活动
        全站搜索
        热门搜索:
        会员
        0通知
        客服
        0特价
        搜索
        TOP
        香港高防服务器,远程桌面,韩国服务器,nginx,反向代理