phpExcel中文帮助手册之常用功能指南_PHP
PHPExcel基本操作:
定义EXCEL实体
即定义一个PHPEXCEL对象,并设置EXCEL对象内显示内容
// Excel开始 // 准备EXCEL的包括文件 // Error reporting error_reporting(0); // PHPExcel require_once dirname(__FILE__) . 'PHPExcel.php'; // 生成新的excel对象 $objPHPExcel = new PHPExcel(); // 设置excel文档的属性 $objPHPExcel->getProperties()->setCreator("Sam.c") ->setLastModifiedBy("Sam.c Test") ->setTitle("Microsoft Office Excel Document") ->setSubject("Test") ->setDescription("Test") ->setKeywords("Test") ->setCategory("Test result file"); // 开始操作excel表 // 操作第一个工作表 $objPHPExcel->setActiveSheetIndex(0); // 设置工作薄名称 $objPHPExcel->getActiveSheet()->setTitle(iconv('gbk', 'utf-8', 'phpexcel测试')); // 设置默认字体和大小 $objPHPExcel->getDefaultStyle()->getFont()->setName(iconv('gbk', 'utf-8', '宋体')); $objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
三、输出文件
// 如果需要输出EXCEL格式 if($m_exportType=="excel"){ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 从浏览器直接输出$filename header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type: application/vnd.ms-excel;"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Content-Disposition:attachment;filename=".$filename); header("Content-Transfer-Encoding:binary"); $objWriter->save("php://output"); } // 如果需要输出PDF格式 if($m_exportType=="pdf"){ $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF'); $objWriter->setSheetIndex(0); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type: application/pdf"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header("Content-Disposition:attachment;filename=".$m_strOutputPdfFileName); header("Content-Transfer-Encoding:binary"); $objWriter->save("php://output"); }
设置一列的宽度:
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
设置一行的高度:
$objPHPExcel->getActiveSheet()->getRowDimension('6')->setRowHeight(30);
合并单元格:
$objPHPExcel->getActiveSheet()->mergeCells('A1:P1');
设置A1单元格加粗,居中:
$styleArray1 = array( 'font' => array( 'bold' => true, 'size'=>12, 'color'=>array( 'argb' => '00000000', ), ), 'alignment' => array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, ), ); // 将A1单元格设置为加粗,居中 $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($styleArray1); $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold(true);
给特定单元格中写入内容:
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'Hello Baby');
设置单元格样式(水平/垂直居中):
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
设置单元格样式(黑色字体):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
设置单元格格式(背景):
$objPHPExcel->getActiveSheet()->getStyle('H5')->getFill()->getStartColor()->setARGB('00ff99cc'); // 将背景设置为浅粉色
设置单元格格式(数字格式):
$objPHPExcel->getActiveSheet()->getStyle('F'.$iLineNumber)->getNumberFormat()->setFormatCode('0.000');
给单元格中放入图片:
// 将数据中心图片放在J1单元格内 $objDrawing = new PHPExcel_Worksheet_Drawing(); $objDrawing->setName('Logo'); $objDrawing->setDescription('Logo'); $objDrawing->setPath('test.jpg'); $objDrawing->setWidth(400); $objDrawing->setHeight(123); $objDrawing->setCoordinates('J1'); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
在单元格中设置超链接:
$objPHPExcel->getActiveSheet()->setCellValue('H8', iconv('gbk', 'utf-8', '燕南天')); $objPHPExcel->getActiveSheet()->getCell('H8')->getHyperlink()->setUrl('http://www.bitsCN.com/');
设置单元格边框
$styleThinBlackBorderOutline = array( 'borders' => array ( 'outline' => array ( 'style' => PHPExcel_Style_Border::BORDER_THIN, //设置border样式 //'style' => PHPExcel_Style_Border::BORDER_THICK, 另一种样式 'color' => array ('argb' => 'FF000000'), //设置border颜色 ), ), ); $objPHPExcel->getActiveSheet()->getStyle( 'A4:E10')->applyFromArray($styleThinBlackBorderOutline); //添加一个新的worksheet $objExcel->createSheet(); $objActSheet = $objExcel->getSheet($s); $objActSheet->setTitle('表'.$GSheet);

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

AI Hentai Generator
Generate AI Hentai for free.

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



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

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

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 (.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

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.

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
