phpExcel常用方法详解【附有php导出excel加超级链接】

发表于4年前(-- :)   阅读() | 评论() 0人收藏此文章, 我要收藏
赞0 http://www.codeplex.com/PHPExcel http://www.phpexcel.net 开发包Tests目录有详细使用实例
支持中文,注意文件编码,文件保存为utf-
//Add a hyperlink to the sheet 添加链接
$objPHPExcel->getActiveSheet()->setCellValue(‘E26′, ‘www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setUrl(‘http://www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setTooltip(‘Navigate to website’);
$objPHPExcel->getActiveSheet()->getStyle(‘E26′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

写excel

//Include class
require_once(‘Classes/PHPExcel.php’);
require_once(‘Classes/PHPExcel/Writer/Excel2007.php’);
$objPHPExcel = new PHPExcel(); //Set properties 设置文件属性
$objPHPExcel->getProperties()->setCreator(“Maarten Balliauw”);
$objPHPExcel->getProperties()->setLastModifiedBy(“Maarten Balliauw”);
$objPHPExcel->getProperties()->setTitle(“Office XLSX Test Document”);
$objPHPExcel->getProperties()->setSubject(“Office XLSX Test Document”);
$objPHPExcel->getProperties()->setDescription(“Test document for Office XLSX, generated using PHP classes.”);
$objPHPExcel->getProperties()->setKeywords(“office openxml php”);
$objPHPExcel->getProperties()->setCategory(“Test result file”); //Add some data 添加数据
$objPHPExcel->setActiveSheetIndex();
$objPHPExcel->getActiveSheet()->setCellValue(‘A1′, ‘Hello’);//可以指定位置
$objPHPExcel->getActiveSheet()->setCellValue(‘A2′, true);
$objPHPExcel->getActiveSheet()->setCellValue(‘A3′, false);
$objPHPExcel->getActiveSheet()->setCellValue(‘B2′, ‘world!’);
$objPHPExcel->getActiveSheet()->setCellValue(‘B3′, );
$objPHPExcel->getActiveSheet()->setCellValue(‘C1′, ‘Hello’);
$objPHPExcel->getActiveSheet()->setCellValue(‘D2′, ‘world!’);
//循环
for($i = ;$i<;$i++) {
$objPHPExcel->getActiveSheet()->setCellValue(‘A’ . $i, $i);
$objPHPExcel->getActiveSheet()->setCellValue(‘B’ . $i, ‘Test value’);
}
//日期格式化
$objPHPExcel->getActiveSheet()->setCellValue(‘D1′, time());
$objPHPExcel->getActiveSheet()->getStyle(‘D1′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_DATE_YYYYMMDDSLASH); //Add comment 添加注释
$objPHPExcel->getActiveSheet()->getComment(‘E11′)->setAuthor(‘PHPExcel’);
$objCommentRichText = $objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(‘PHPExcel:’);
$objCommentRichText->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(“\r\n”);
$objPHPExcel->getActiveSheet()->getComment(‘E11′)->getText()->createTextRun(‘Total amount on the current invoice, excluding VAT.’); //Add rich-text string 添加文字 可设置样式
$objRichText = new PHPExcel_RichText( $objPHPExcel->getActiveSheet()->getCell(‘A18′) );
$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.’); //Merge cells 合并分离单元格
$objPHPExcel->getActiveSheet()->mergeCells(‘A18:E22′);
$objPHPExcel->getActiveSheet()->unmergeCells(‘A18:E22′); //Protect cells 保护单元格
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);//Needs to be set to true in order to enable any worksheet protection!
$objPHPExcel->getActiveSheet()->protectCells(‘A3:E13′, ‘PHPExcel’); //Set cell number formats 数字格式化
$objPHPExcel->getActiveSheet()->getStyle(‘E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle(‘E4′), ‘E5:E13′ ); //Set column widths 设置列宽度
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B’)->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setWidth(); //Set fonts 设置字体
$objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setName(‘Candara’);
$objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setSize();
$objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getStyle(‘B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE); //Set alignments 设置对齐
$objPHPExcel->getActiveSheet()->getStyle(‘D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle(‘A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
$objPHPExcel->getActiveSheet()->getStyle(‘A18′)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle(‘A3′)->getAlignment()->setWrapText(true); //Set column borders 设置列边框
$objPHPExcel->getActiveSheet()->getStyle(‘A4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘A10′)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘E10′)->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
$objPHPExcel->getActiveSheet()->getStyle(‘E13′)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK); //Set border colors 设置边框颜色
$objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getLeft()->getColor()->setARGB(‘FF993300′);
$objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getTop()->getColor()->setARGB(‘FF993300′);
$objPHPExcel->getActiveSheet()->getStyle(‘D13′)->getBorders()->getBottom()->getColor()->setARGB(‘FF993300′);
$objPHPExcel->getActiveSheet()->getStyle(‘E13′)->getBorders()->getRight()->getColor()->setARGB(‘FF993300′); //Set fills 设置填充
$objPHPExcel->getActiveSheet()->getStyle(‘A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle(‘A1′)->getFill()->getStartColor()->setARGB(‘FF808080′); //Add a hyperlink to the sheet 添加链接
$objPHPExcel->getActiveSheet()->setCellValue(‘E26′, ‘www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setUrl(‘http://www.phpexcel.net’);
$objPHPExcel->getActiveSheet()->getCell(‘E26′)->getHyperlink()->setTooltip(‘Navigate to website’);
$objPHPExcel->getActiveSheet()->getStyle(‘E26′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); //Add a drawing to the worksheet 添加图片
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(‘Logo’);
$objDrawing->setDescription(‘Logo’);
$objDrawing->setPath(‘./images/officelogo.jpg’);
$objDrawing->setHeight();
$objDrawing->setCoordinates(‘B15′);
$objDrawing->setOffsetX();
$objDrawing->setRotation();
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection();
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); //Play around with inserting and removing rows and columns
$objPHPExcel->getActiveSheet()->insertNewRowBefore(, );
$objPHPExcel->getActiveSheet()->removeRow(, );
$objPHPExcel->getActiveSheet()->insertNewColumnBefore(‘E’, );
$objPHPExcel->getActiveSheet()->removeColumn(‘E’, ); //Add conditional formatting
$objConditional1 = new PHPExcel_Style_Conditional();
$objConditional1->setConditionType(PHPExcel_Style_Conditional::CONDITION_CELLIS);
$objConditional1->setOperatorType(PHPExcel_Style_Conditional::OPERATOR_LESSTHAN);
$objConditional1->setCondition(’′);
$objConditional1->getStyle()->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
$objConditional1->getStyle()->getFont()->setBold(true); //Set autofilter 自动过滤
$objPHPExcel->getActiveSheet()->setAutoFilter(‘A1:C9′); //Hide “Phone” and “fax” column 隐藏列
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C’)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D’)->setVisible(false); //Set document security 设置文档安全
$objPHPExcel->getSecurity()->setLockWindows(true);
$objPHPExcel->getSecurity()->setLockStructure(true);
$objPHPExcel->getSecurity()->setWorkbookPassword(“PHPExcel”); //Set sheet security 设置工作表安全
$objPHPExcel->getActiveSheet()->getProtection()->setPassword(‘PHPExcel’);
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);// This should be enabled in order to enable any of the following!
$objPHPExcel->getActiveSheet()->getProtection()->setSort(true);
$objPHPExcel->getActiveSheet()->getProtection()->setInsertRows(true);
$objPHPExcel->getActiveSheet()->getProtection()->setFormatCells(true); //Calculated data 计算
echo ‘Value of B14 [=COUNT(B2:B12)]: ‘ . $objPHPExcel->getActiveSheet()->getCell(‘B14′)->getCalculatedValue() . “\r\n”; //Set outline levels
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setOutlineLevel();
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setVisible(false);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E’)->setCollapsed(true); //Freeze panes
$objPHPExcel->getActiveSheet()->freezePane(‘A2′); //Rows to repeat at top
$objPHPExcel->getActiveSheet()->getPageSetup()->setRowsToRepeatAtTopByStartAndEnd(, ); //Set data validation 验证输入值
$objValidation = $objPHPExcel->getActiveSheet()->getCell(‘B3′)->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );
$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorTitle(‘Input error’);
$objValidation->setError(‘Number is not allowed!’);
$objValidation->setPromptTitle(‘Allowed input’);
$objValidation->setPrompt(‘Only numbers between and are allowed.’);
$objValidation->setFormula1();
$objValidation->setFormula2();
$objPHPExcel->getActiveSheet()->getCell(‘B3′)->setDataValidation($objValidation); //Create a new worksheet, after the default sheet 创建新的工作标签
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(); //Set header and footer. When no different headers for odd/even are used, odd header is assumed. 页眉页脚
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(‘&C&HPlease treat this document as confidential!’);
$objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter(‘&L&B’ . $objPHPExcel->getProperties()->getTitle() . ‘&RPage &P of &N’); //Set page orientation and size 方向大小
$objPHPExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_LANDSCAPE);
$objPHPExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4); //Rename sheet 重命名工作表标签
$objPHPExcel->getActiveSheet()->setTitle(‘Simple’); //Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(); //Save Excel 2007 file 保存
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save(str_replace(‘.php’, ‘.xlsx’, __FILE__)); //Save Excel 5 file 保存
require_once(‘Classes/PHPExcel/Writer/Excel5.php’);
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objWriter->save(str_replace(‘.php’, ‘.xls’, __FILE__)); //1.6.2新版保存
require_once(‘Classes/PHPExcel/IOFactory.php’);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel2007′);
$objWriter->save(str_replace(‘.php’, ‘.xls’, __FILE__)); 读excel
//Include class
require_once(‘Classes/PHPExcel/Reader/Excel2007.php’);
$objReader = new PHPExcel_Reader_Excel2007; $objPHPExcel = $objReader->load(“05featuredemo.xlsx”); 读写csv require_once(“05featuredemo.inc.php”);
require_once(‘Classes/PHPExcel/Writer/CSV.php’);
require_once(‘Classes/PHPExcel/Reader/CSV.php’);
require_once(‘Classes/PHPExcel/Writer/Excel2007.php’); //Write to CSV format 写
$objWriter = new PHPExcel_Writer_CSV($objPHPExcel);
$objWriter->setDelimiter(‘;’);
$objWriter->setEnclosure(”);
$objWriter->setLineEnding(“\r\n”);
$objWriter->setSheetIndex();
$objWriter->save(str_replace(‘.php’, ‘.csv’, __FILE__)); //Read from CSV format 读
$objReader = new PHPExcel_Reader_CSV();
$objReader->setDelimiter(‘;’);
$objReader->setEnclosure(”);
$objReader->setLineEnding(“\r\n”);
$objReader->setSheetIndex();
$objPHPExcelFromCSV = $objReader->load(str_replace(‘.php’, ‘.csv’, __FILE__)); //Write to Excel2007 format
$objWriter2007 = new PHPExcel_Writer_Excel2007($objPHPExcelFromCSV);
$objWriter2007->save(str_replace(‘.php’, ‘.xlsx’, __FILE__)); 写html require_once(“05featuredemo.inc.php”);
require_once(‘Classes/PHPExcel/Writer/HTML.php’); //Write to HTML format
$objWriter = new PHPExcel_Writer_HTML($objPHPExcel);
$objWriter->setSheetIndex();
$objWriter->save(str_replace(‘.php’, ‘.htm’, __FILE__)); 写pdf require_once(“05featuredemo.inc.php”);
require_once(‘Classes/PHPExcel/IOFactory.php’); //Write to PDF format
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘PDF’);
$objWriter->setSheetIndex();
$objWriter->save(str_replace(‘.php’, ‘.pdf’, __FILE__));
//Echo memory peak usage
echo date(‘H:i:s’) . ” Peak memory usage: ” . (memory_get_peak_usage(true) / / ) . ” MB\r\n”;
 

phpExcel常用方法详解【附有php导出excel加超级链接】的更多相关文章

  1. phpExcel导出excel加超级链接的实例代码[转]

    phpexcel实现的导出excel文件的代码,且可以在excel文件中加入超级链接. 说明:PHPExcel的开发包Tests目录有详细使用实例.以下代码支持中文,注意文件编码,文件保存为utf-8 ...

  2. $.ajax()常用方法详解(推荐)

    AJAX 是一种与服务器交换数据的技术,可以在补充在整个页面的情况下更新网页的一部分.接下来通过本文给大家介绍ajax一些常用方法,大家有需要可以一起学习. 1.url: 要求为String类型的参数 ...

  3. 【Java】HashMap源码分析——常用方法详解

    上一篇介绍了HashMap的基本概念,这一篇着重介绍HasHMap中的一些常用方法:put()get()**resize()** 首先介绍resize()这个方法,在我看来这是HashMap中一个非常 ...

  4. 定时器详解和应用、js加载阻塞、css加载阻塞

    1.setTimeout().setInterval()详解和应用 1.1 详解: setTimeout.setInterval执行时机 1.2 存在问题: setInterval重复定时器可能存在的 ...

  5. .NET DLL 保护措施详解(非混淆加密加壳)

    为什么要保护DLL,我就不多说了,各人有各人的理由.总的来说,就是不想核心逻辑泄露及授权验证被破解两大方面的因素.市面上的混淆加密工具对.NET源码保护的效果天差地别,很多网上下到的混淆工具破解版对. ...

  6. JavaScript语法基础:数组的常用方法详解

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 数组的定义 之前学习的数据类型,只能存储一个值(字符串为一个值).如果我 ...

  7. StringUtils工具类常用方法详解

    StringUtils 常用方法 1.isEmpty(String str) 是否为空,空格字符为false2.isNotEmpty(String str) 是否为非空,空格字符为true3.isBl ...

  8. Java-Method类常用方法详解

    一.Method类的定义Method类位于 java.lang.reflect 包中,主要用于在程序运行状态中,动态地获取方法信息二.Method类的常用方法  1.getAnnotatedRetur ...

  9. 【python基础】os.path模块常用方法详解

    os.path模块 主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. 更多的方法可以去查看官方文档:http://docs.python.org/library/os.path. ...

随机推荐

  1. python基础-协程函数、递归、模块、包等内容

    1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...

  2. IO流--File--properties

    package com.songyan.properties; /** * properties * 是hashtable的子类具备map集合的特点 * 里面存储的键值对都是String而且不需要指定 ...

  3. 一个简单的WeakList的实现

    有的时候,我们会使用到WeakList,它和传统的List不同的是,保存的是对象的弱应用,在WeakList中存储的对象会被GC回收,在一些和UI相关的编程的地方会用到它(弱事件,窗体的通知订阅等). ...

  4. gitk图形界面中文乱码情况

    当打开gitk图形界面时,文件中的中文部分乱码了,这大部分是因为编码格式的问题,为了跟上时代的脚步,本人建议都是用utf-8编码. 为了方便,我将全局配置为utf-8编码: git config -- ...

  5. 请用心练完这16个webpack小例子

    16个demo,webpack+react搭配使用首先教大家2个新技能 1.按照正常github地址情况下,你的github本身不能访问目录. 例如要访问vue-demo下的vueCpu文件夹:htt ...

  6. Oracle数据库冷备份与恢复(救命稻草)

    说明,只要是同样系统,同样数据库版本,是可以做冷备恢复.冷备份数据必须是数据库不在open状态下.以oracle11gR2为例. 一.冷备份与冷恢复 具体步骤如下. 1. 复制旧的数据库文件 (1) ...

  7. 【mybatis】mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wait timeout exceeded; try restarting transaction

    今天使用mybatis和jpa的过程中,发现这样一个问题: mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wai ...

  8. mac 下安装 mysql (蛋疼)

    mac先安装mydql步骤 1.首先从官网上下载 mac版本的mysql:我的是mysql-5.7.20-macos10.12-x86_64.dmg 2.正常安装: 3.在系统偏好设置的最底下.1:是 ...

  9. oracle 10g函数大全--聚合函数

    AVG([distinct|all]x) [功能]统计数据表选中行x列的平均值. [参数]all表示对所有的值求平均值,distinct只对不同的值求平均值,默认为all 如果有参数distinct或 ...

  10. sqls

    ALTER TABLE `shh_data`.`topic_floor` ADD COLUMN `updated_date` DATETIME NULL AFTER `publish_date`,AD ...