PHPExcel export excel_PHP tutorial
如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把<span>$yourStr从utf</span>-8转换成了gb2312: <span>$yourStr</span> = mb_convert_encoding("gb2312", "UTF-8", <span>$yourStr</span><span>); 总结 php导出Excel php导入Excel PhpExcel使用说明 PhpExcel使用手册 </span>1:第一推荐无比风骚的PHPExcel,官方网站: http:<span>//</span><span>www.codeplex.com/PHPExcel</span> <span>导入导出都成,可以导出office2007格式,同时兼容2003。 下载下来的包中有文档和例子,大家可以自行研究。 </span><? <span>//</span><span>设置PHPExcel类库的include path </span> <span>set_include_path</span>('.'. PATH_SEPARATOR .'D:\workspace\biznaligy_eh\dev_src\includes\PHPExcel' . PATH_SEPARATOR . <span>get_include_path</span><span>()); </span><span>/*</span><span>* * 以下是使用示例,对于以 //// 开头的行是不同的可选方式,请根据实际需要 * 打开对应行的注释。 * 如果使用 Excel5 ,输出的内容应该是GBK编码。 </span><span>*/</span> <span>require_once</span> 'PHPExcel.php'<span>; </span><span>require_once</span> 'PHPExcel/Writer/Excel5.php'; <span>//</span><span> 用于其他低版本xls </span> <span>require_once</span> 'PHPExcel/Writer/Excel2007.php'; <span>//</span><span> 用于 excel-2007 格式 // 创建一个处理对象实例 </span> <span>$objExcel</span> = <span>new</span><span> PHPExcel(); </span><span>//</span><span> 创建文件格式写入对象实例, uncomment </span> <span>$objWriter</span> = <span>new</span> PHPExcel_Writer_Excel5(<span>$objExcel</span>); <span>//</span><span> 用于其他版本格式 //or //$objWriter = new PHPExcel_Writer_Excel2007($objExcel); // 用于 2007 格式 //$objWriter->setOffice2003Compatibility(true); //设置文档基本属性 </span> <span>$objProps</span> = <span>$objExcel</span>-><span>getProperties(); </span><span>$objProps</span>->setCreator("Zeal Li"<span>); </span><span>$objProps</span>->setLastModifiedBy("Zeal Li"<span>); </span><span>$objProps</span>->setTitle("Office XLS Test Document"<span>); </span><span>$objProps</span>->setSubject("Office XLS Test Document, Demo"<span>); </span><span>$objProps</span>->setDescription("Test document, generated by PHPExcel."<span>); </span><span>$objProps</span>->setKeywords("office excel PHPExcel"<span>); </span><span>$objProps</span>->setCategory("Test"<span>); </span><span>//</span><span>设置当前的sheet索引,用于后续的内容操作。 //一般只有在使用多个sheet的时候才需要显示调用。 //缺省情况下,PHPExcel会自动创建第一个sheet被设置SheetIndex=0 </span> <span>$objExcel</span>->setActiveSheetIndex(0<span>); </span><span>$objActSheet</span> = <span>$objExcel</span>-><span>getActiveSheet(); </span><span>//</span><span>设置当前活动sheet的名称 </span> <span>$objActSheet</span>->setTitle('测试Sheet'<span>); </span><span>//</span><span>设置单元格内容 由PHPExcel根据传入内容自动判断单元格内容类型 </span> <span>$objActSheet</span>->setCellValue('A1', '字符串内容'); <span>//</span><span> 字符串内容 </span> <span>$objActSheet</span>->setCellValue('A2', 26); <span>//</span><span> 数值 </span> <span>$objActSheet</span>->setCellValue('A3', <span>true</span>); <span>//</span><span> 布尔值 </span> <span>$objActSheet</span>->setCellValue('A4', '=SUM(A2:A2)'); <span>//</span><span> 公式 //显式指定内容类型 </span> <span>$objActSheet</span>->setCellValueExplicit('A5','8757584',PHPExcel_Cell_DataType::<span>TYPE_STRING); </span><span>//</span><span>合并单元格 </span> <span>$objActSheet</span>->mergeCells('B1:C22'<span>); </span><span>//</span><span>分离单元格 </span> <span>$objActSheet</span>->unmergeCells('B1:C22'<span>); </span><span>//</span><span>设置宽度 </span> <span>$objActSheet</span>->getColumnDimension('B')->setAutoSize(<span>true</span><span>); </span><span>$objActSheet</span>->getColumnDimension('A')->setWidth(30<span>); </span><span>//</span><span>设置单元格内容的数字格式。 //如果使用了 PHPExcel_Writer_Excel5 来生成内容的话, //这里需要注意,在 PHPExcel_Style_NumberFormat 类的 const 变量定义的 //各种自定义格式化方式中,其它类型都可以正常使用,但当setFormatCode //为 FORMAT_NUMBER 的时候,实际出来的效果被没有把格式设置为"0"。需要 //修改 PHPExcel_Writer_Excel5_Format 类源代码中的 getXf($style) 方法, //在 if ($this->_BIFF_version == 0x0500) { (第363行附近)前面增加一 //行代码: //if($ifmt === '0') $ifmt = 1; //设置格式为PHPExcel_Style_NumberFormat::FORMAT_NUMBER,避免某些大数字 //被使用科学记数方式显示,配合下面的 setAutoSize 方法可以让每一行的内容 //都按原始内容全部显示出来。</span> <span>$objStyleA5</span> = <span>$objActSheet</span> ->getStyle('A5'<span>); </span><span>$objStyleA5</span> ->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::<span>FORMAT_NUMBER); </span><span>//</span><span>设置字体 </span> <span>$objFontA5</span> = <span>$objStyleA5</span>-><span>getFont(); </span><span>$objFontA5</span>->setName('Courier New'<span>); </span><span>$objFontA5</span>->setSize(10<span>); </span><span>$objFontA5</span>->setBold(<span>true</span><span>); </span><span>$objFontA5</span>->setUnderline(PHPExcel_Style_Font::<span>UNDERLINE_SINGLE); </span><span>$objFontA5</span> ->getColor()->setARGB('FFFF0000'<span>) ; </span><span>$objFontA5</span> ->getColor()->setARGB( PHPExcel_Style_Color::<span>COLOR_WHITE); </span><span>//</span><span> $ objFontA5 ->getFont()->setColor(PHPExcel_Style_Color::COLOR_RED); //设置对齐方式 </span> <span>$objAlignA5</span> = <span>$objStyleA5</span>-><span>getAlignment(); </span><span>$objAlignA5</span>->setHorizontal(PHPExcel_Style_Alignment::<span>HORIZONTAL_RIGHT); </span><span>$objAlignA5</span>->setVertical(PHPExcel_Style_Alignment::<span>VERTICAL_CENTER); </span><span>//</span><span>设置边框 </span> <span>$objBorderA5</span> = <span>$objStyleA5</span>-><span>getBorders(); </span><span>$objBorderA5</span>->getTop()->setBorderStyle(PHPExcel_Style_Border::<span>BORDER_THIN); </span><span>$objBorderA5</span>->getTop()->getColor()->setARGB('FFFF0000') ; <span>//</span><span> 边框color </span> <span>$objBorderA5</span>->getBottom()->setBorderStyle(PHPExcel_Style_Border::<span>BORDER_THIN); </span><span>$objBorderA5</span>->getLeft()->setBorderStyle(PHPExcel_Style_Border::<span>BORDER_THIN); </span><span>$objBorderA5</span>->getRight()->setBorderStyle(PHPExcel_Style_Border::<span>BORDER_THIN); </span><span>//</span><span>设置CELL填充颜色 </span> <span>$objFillA5</span> = <span>$objStyleA5</span>-><span>getFill(); </span><span>$objFillA5</span>->setFillType(PHPExcel_Style_Fill::<span>FILL_SOLID); </span><span>$objFillA5</span>->getStartColor()->setARGB('FFEEEEEE'<span>); </span><span>//</span><span>从指定的单元格复制样式信息. </span> <span>$objActSheet</span>->duplicateStyle(<span>$objStyleA5</span>, 'B1:C22'<span>); </span><span>//</span><span>添加图片 </span> <span>$objDrawing</span> = <span>new</span><span> PHPExcel_Worksheet_Drawing(); </span><span>$objDrawing</span>->setName('ZealImg'<span>); </span><span>$objDrawing</span>->setDescription('Image inserted by Zeal'<span>); </span><span>$objDrawing</span>->setPath('./zeali.net.logo.gif'<span>); </span><span>$objDrawing</span>->setHeight(36<span>); </span><span>$objDrawing</span>->setCoordinates('C23'<span>); </span><span>$objDrawing</span>->setOffsetX(10<span>); </span><span>$objDrawing</span>->setRotation(15<span>); </span><span>$objDrawing</span>->getShadow()->setVisible(<span>true</span><span>); </span><span>$objDrawing</span>->getShadow()->setDirection(36<span>); </span><span>$objDrawing</span>->setWorksheet(<span>$objActSheet</span><span>); </span><span>//</span><span>添加一个新的worksheet </span> <span>$objExcel</span>-><span>createSheet(); </span><span>$objExcel</span>->getSheet(1)->setTitle('测试2'<span>); </span><span>//</span><span>保护单元格 </span> <span>$objExcel</span>->getSheet(1)->getProtection()->setSheet(<span>true</span><span>); </span><span>$objExcel</span>->getSheet(1)->protectCells('A1:C22', 'PHPExcel'<span>); </span><span>//</span><span>显示网格线: </span> <span>$objPHPExcel</span>->getActiveSheet()->setShowGridlines(<span>true</span><span>); </span><span>//</span><span>显示隐藏列</span> <span>$objPHPExcel</span>->getActiveSheet()->getColumnDimension('C')->setVisible(<span>true</span><span>); </span><span>$objPHPExcel</span>->getActiveSheet()->getColumnDimension('D')->setVisible(<span>false</span><span>); </span><span>//</span><span>显示隐藏行</span> <span>$objPHPExcel</span>->getActiveSheet()->getRowDimension('10')->setVisible(<span>false</span><span>); </span><span>//</span><span>默认列宽</span> <span>$objPHPExcel</span>->getActiveSheet()->getDefaultColumnDimension()->setWidth(12<span>); </span><span>//</span><span>默认行宽</span> <span>$objPHPExcel</span>->getActiveSheet()->getDefaultRowDimension()->setRowHeight(15<span>); </span><span>//</span><span>worksheet 默认style 设置 (和默认不同的需单独设置)</span> <span>$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()->getFont()->setName('Arial'<span>); </span><span>$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()->getFont()->setSize(8<span>); </span><span>$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()-><span>getAlignment(); </span><span>$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::<span>HORIZONTAL_CENTER); </span><span>$objPHPExcel</span>->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::<span>VERTICAL_CENTER); </span><span>//</span><span>输出内容 </span> <span>$outputFileName</span> = "output.xls"<span>; </span><span>//</span><span>到文件 </span><span> //</span><span>//$objWriter->save($outputFileName); //or //到浏览器 </span> <span>header</span>("Content-Type: application/force-download"<span>); </span><span>header</span>("Content-Type: application/octet-stream"<span>); </span><span>header</span>("Content-Type: application/download"<span>); </span><span>header</span>('Content-Disposition:inline;filename="'.<span>$outputFileName</span>.'"'<span>); </span><span>header</span>("Content-Transfer-Encoding: binary"<span>); </span><span>header</span>("Last-Modified: " . <span>gmdate</span>("D, d M Y H:i:s") . " GMT"<span>); </span><span>header</span>("Cache-Control: must-revalidate, post-check=0, pre-check=0"<span>); </span><span>header</span>("Pragma: no-cache"<span>); </span><span>$objWriter</span>->save('php://output'<span>); </span>?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Complete Guide: How to Process Excel Files Using PHP Extension PHPExcel Introduction: Excel files are often used as a common format for data storage and exchange when processing large amounts of data and statistical analysis. Using the PHP extension PHPExcel, we can easily read, write and modify Excel files to effectively process Excel data. This article will introduce how to use the PHP extension PHPExcel to process Excel files and provide code examples. 1. Install PHPExc

With the advent of the digital age, data has become the most important part of our daily lives and work, and Excel files have become one of the important tools for data processing. I believe that many PHP developers will often encounter the use of Excel files for data processing and operations at work. This article will introduce you to the methods and precautions for using the PHPExcel library to process Excel files. What is PHPExcel? PHPExcel is a PHP class

PHPEXCEL is an excellent PHP class library for reading and writing Excel files. It provides a very sufficient API that allows us to use PHP to read and write Excel files. Sometimes, we need to convert Excel files into CSV files for use on some occasions. So, this article mainly describes how to use the PHPEXCEL class library to convert Excel files into CSV files and open them.

PHPExcel is an open source PHP library for processing Microsoft Excel files. It can read, create, modify and save Excel files. It is a powerful and highly customizable tool that can be used to handle tasks such as data analysis, report generation, data import and export, etc. In this article, we will introduce why PHPExcel has become the focus of PHP developers.

PHPExcel is an open source PHP library for processing Microsoft Excel (.xls and .xlsx) files. It can read, write and operate Excel files, and provides a wealth of functions and methods. Using the PHPExcel library in PHP projects, you can quickly and easily process Excel files and implement functions such as data import, export and data processing. This article will introduce how to use PHPExcel to process Excel files. 1. To install PHPExcel, use

In today's era of rapid information transfer, data processing and storage have become increasingly important. The use of Excel tables is the first choice for many people because Excel tables can integrate various data and can be easily analyzed and processed. In order to complete the creation of Excel tables more efficiently, we can use two powerful tools, PHP and PHPExcel. In this article, we will introduce how to create Excel files using PHP and PHPExcel. 1. Install PHPExcel first

PHP development tips: How to use PHPExcel and PHPExcel_IOFactory to operate MySQL database Overview: In web development, processing Excel files is a common and important task. PHPExcel is a powerful and easy-to-use PHP library that can help us read and write Excel files. This article will introduce how to use PHPExcel and PHPExcel_IOFactory libraries to operate MySQL database. step 1

PHP development skills: How to use PHPExcel to operate MySQL database. With the booming development of the Internet, a large amount of data is stored in the database, and operations such as import, export, and processing are required. In PHP development, PHPExcel is a powerful library that can simplify the interaction with Excel files and realize the import and export of data. This article will introduce how to use PHPExcel to operate the MySQL database and implement data import and export functions. Installation and configuration of PHPExcel
