search
HomeBackend DevelopmentPHP TutorialPHP Export EXCEL Quick Development Guide
PHP Export EXCEL Quick Development GuideNov 24, 2017 am 09:50 AM
excelphpDevelopment Guide

php excel has a proprietary development document. For detailed operations, please refer to the php excel development document. This article only optimizes and integrates its use to facilitate new projects. Rapid development. If you are interested, you can also look at the php excel development documentation.

There are also two ways for phpexcel to generate files, one is direct output, and the other is to generate static files.

Direct output:
The main file is (the file in the same directory of the class directory):

<?php
    include("./class/class.php"); // 包含class的基本头文件
    include("./class/phpexcel/PHPExcel.php"); // 生成excel的基本类定义(注意文件名的大小写)
    // 如果直接输出excel文件,则要包含此文件
    include("./class/phpexcel/PHPExcel/IOFactory.php");
    // 创建phpexcel对象,此对象包含输出的内容及格式
    $m_objPHPExcel = new PHPExcel();
    // 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中
    // 模板文件将对象$m_objPHPExcel进行操作
    include("./include/excel.php");
    // 输出文件的类型,excel或pdf
    $m_exportType = "excel";
    $m_strOutputExcelFileName = date(&#39;Y-m-j_H_i_s&#39;).".xls"; // 输出EXCEL文件名
    $m_strOutputPdfFileName = date(&#39;Y-m-j_H_i_s&#39;).".pdf"; // 输出PDF文件名
    // PHPExcel_IOFactory, 输出excel
    //require_once dirname(__FILE__).&#39;/Classes/PHPExcel/IOFactory.php&#39;;
    // 如果需要输出EXCEL格式
    if($m_exportType=="excel"){
    $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, &#39;Excel5&#39;);
    // 从浏览器直接输出$m_strOutputExcelFileName
    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=".$m_strOutputExcelFileName);
    header("Content-Transfer-Encoding:binary");
    $objWriter->save("php://output");
    }
    // 如果需要输出PDF格式
    if($m_exportType=="pdf"){
    $objWriter = PHPExcel_IOFactory::createWriter($m_objPHPExcel, &#39;PDF&#39;);
    $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");
    }
?>

Template file content (with additional common operations)

<?php
global $m_objPHPExcel; // 由外部文件定义
// 设置基本属性
$m_objPHPExcel->getProperties()->setCreator("Sun Star Data Center")
->setLastModifiedBy("Sun Star Data Center")
->setTitle("Microsoft Office Excel Document")
->setSubject("Test Data Report -- From Sunstar Data Center")
->setDescription("LD Test Data Report, Generate by Sunstar Data Center")
->setKeywords("sunstar ld report")
->setCategory("Test result file");
// 创建多个工作薄
$sheet1 = $m_objPHPExcel->createSheet();
$sheet2 = $m_objPHPExcel->createSheet();
// 通过操作索引即可操作对应的工作薄
// 只需设置要操作的工作簿索引为当前活动工作簿,如
// $m_objPHPExcel->setActiveSheetIndex(0);
// 设置第一个工作簿为活动工作簿
$m_objPHPExcel->setActiveSheetIndex(0);
// 设置活动工作簿名称
// 如果是中文一定要使用iconv函数转换编码
$m_objPHPExcel->getActiveSheet()->setTitle(iconv(&#39;gbk&#39;, &#39;utf-8&#39;, &#39;测试工作簿&#39;));
// 设置默认字体和大小
$m_objPHPExcel->getDefaultStyle()->getFont()->setName(iconv(&#39;gbk&#39;, &#39;utf-8&#39;, &#39;宋体&#39;));
$m_objPHPExcel->getDefaultStyle()->getFont()->setSize(10);
// 设置一列的宽度
$m_objPHPExcel->getActiveSheet()->getColumnDimension(&#39;A&#39;)->setWidth(15);
// 设置一行的高度
$m_objPHPExcel->getActiveSheet()->getRowDimension(&#39;6&#39;)->setRowHeight(30);
// 合并单元格
$m_objPHPExcel->getActiveSheet()->mergeCells(&#39;A1:P1&#39;);
// 定义一个样式,加粗,居中
$styleArray1 = array(
&#39;font&#39; => array(
&#39;bold&#39; => true,
&#39;color&#39;=>array(
&#39;argb&#39; => &#39;00000000&#39;,
),
),
&#39;alignment&#39; => array(
&#39;horizontal&#39; => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,
),
);
// 将样式应用于A1单元格
$m_objPHPExcel->getActiveSheet()->getStyle(&#39;A1&#39;)->applyFromArray($styleArray1);
// 设置单元格样式(黑色字体)
$m_objPHPExcel->getActiveSheet()->getStyle(&#39;H5&#39;)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLACK); // 黑色
// 设置单元格格式(背景)
$m_objPHPExcel->getActiveSheet()->getStyle(&#39;H5&#39;)->getFill()->getStartColor()->setARGB(&#39;00ff99cc&#39;); // 将背景设置为浅粉色
// 设置单元格格式(数字格式)
$m_objPHPExcel->getActiveSheet()->getStyle(&#39;F1&#39;)->getNumberFormat()->setFormatCode(&#39;0.000&#39;);
// 给特定单元格中写入内容
$m_objPHPExcel->getActiveSheet()->setCellValue(&#39;A1&#39;, &#39;Hello Baby&#39;);
// 设置单元格样式(居中)
$m_objPHPExcel->getActiveSheet()->getStyle(&#39;H5&#39;)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 给单元格中放入图片, 将数据图片放在J1单元格内
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName(&#39;Logo&#39;);
$objDrawing->setDescription(&#39;Logo&#39;);
$objDrawing->setPath("../logo.jpg"); // 图片路径,只能是相对路径
$objDrawing->setWidth(400); // 图片宽度
$objDrawing->setHeight(123); // 图片高度
$objDrawing->setCoordinates(&#39;J1&#39;);
$objDrawing->setWorksheet($m_objPHPExcel->getActiveSheet());
// 设置A5单元格内容并增加超链接
$m_objPHPExcel->getActiveSheet()->setCellValue(&#39;A5&#39;, iconv(&#39;gbk&#39;, &#39;utf-8&#39;, &#39;超链接keiyi.com&#39;));
$m_objPHPExcel->getActiveSheet()->getCell(&#39;A5&#39;)->getHyperlink()->setUrl(&#39;http://www.keiyi.com/&#39;);
?>


Generate static files on the server side

Compared with direct generation, the main difference between these two methods is the generation format. The template file is completely The same, the following is a modified version based on the above example, pay attention to the difference from the above example.

<?php
// 包含class的基本头文件
include("./class/class.php");
// 生成excel的基本类定义(注意文件名的大小写)
include("./class/phpexcel/PHPExcel.php");
// 包含写Excel5格式的文件,如果需要生成excel2007的文件,包含对应的Writer即可
include("./class/phpexcel/PHPExcel/Writer/Excel5.php");
// 包含写PDF格式文件
include("./class/phpexcel/PHPExcel/Writer/PDF.php");
// 创建phpexcel对象,此对象包含输出的内容及格式
$m_objPHPExcel = new PHPExcel();
// 模板文件,为了实现格式与内容分离,有关输出文件具体内容实现在模板文件中
// 模板文件将对象$m_objPHPExcel进行操作
include("./include/excel.php");
// 输出文件的类型,excel或pdf
$m_exportType = "pdf";
$m_strOutputExcelFileName = date(&#39;Y-m-j_H_i_s&#39;).".xls"; // 输出EXCEL文件名
$m_strOutputPdfFileName = date(&#39;Y-m-j_H_i_s&#39;).".pdf"; // 输出PDF文件名
// 输出文件保存路径,此路径必须可写
$m_strOutputPath = "./output/";
// 如果需要输出EXCEL格式
if($m_exportType=="excel"){
$objWriter = new PHPExcel_Writer_Excel5($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputExcelFileName);
}
// 如果需要输出PDF格式
if($m_exportType=="pdf"){
$objWriter = new PHPExcel_Writer_PDF($m_objPHPExcel);
$objWriter->save($m_strOutputPath.$m_strOutputPdfFileName);
}
?>

The above is all the contents of the PHP Export EXCEL Rapid Development Guide. If you are interested, you can search for more on the PHP Chinese website~

##Related recommendations:

Introduction to the method of freezing and locking table headers in PHPExcel

Example of how phpexcel imports excel to process big data code

Use PHPExcel to upload data in batches

Introduction to the use process of PHPExcel


The above is the detailed content of PHP Export EXCEL Quick Development Guide. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
一文教会你Excel折叠表格分类汇总功能一文教会你Excel折叠表格分类汇总功能May 12, 2022 am 11:58 AM

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于折叠表格的相关问题,就是分类汇总的功能,这样查看数据会非常的方便,下面一起来看一下,希望对大家有帮助。

实用Excel技巧分享:计算年数差、月数差、周数差实用Excel技巧分享:计算年数差、月数差、周数差Apr 22, 2022 am 09:56 AM

在之前的文章《实用Excel技巧分享:利用 数据透视表 来汇总业绩》中,我们学习了下Excel数据透视表,了解了利用数据透视表来汇总业绩的方法。而今天我们来聊聊怎么计算时间差(年数差、月数差、周数差),希望对大家有所帮助!

实例总结Excel中AGGREGATE函数的八个用法实例总结Excel中AGGREGATE函数的八个用法May 17, 2022 am 11:37 AM

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于AGGREGATE函数的相关内容,该函数用法与SUBTOTAL函数类似,但在功能上比SUBTOTAL函数更加强大,下面一起来看一下,希望对大家有帮助。

Word与Excel联动:表格借用Word进行分栏打印!Word与Excel联动:表格借用Word进行分栏打印!May 07, 2022 am 10:28 AM

在之前的文章《实用Word技巧分享:聊聊你没用过的“行号”功能》中,我们了解了Word中你肯定没用过的"行号”功能。今天继续实用Word技巧分享,看看Excel表格怎么借用Word进行分栏打印,快来收藏使用吧!

实例详解利用Excel制作倒计时牌实例详解利用Excel制作倒计时牌May 16, 2022 am 11:53 AM

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于zenmm制作倒计时牌的相关内容,使用Excel中的日期函数结合按指定时间刷新的VBA代码,即可制作出倒计时牌,下面一起来看一下,希望对大家有帮助。

实用Excel技巧分享:合并单元格后实现筛选功能实用Excel技巧分享:合并单元格后实现筛选功能May 11, 2022 am 10:22 AM

在之前的文章《实用Excel技巧分享:原来“定位功能”这么有用!》中,我们了解了定位功能的妙用。而今天我们聊聊合并后的单元格如何实现筛选功能,分享一种复制粘贴和方法解决这个问题,另外还会给大家分享一种合并单元格的不错的替代方式。

Excel怎么查找总和为某个值的组合Excel怎么查找总和为某个值的组合May 18, 2022 am 11:25 AM

本篇文章给大家带来了关于excel的相关知识,其中主要介绍了关于如何使用函数寻找总和为某个值的组合的问题,下面一起来看一下,希望对大家有帮助。

图文详解Excel中XLOOKUP函数典型用法整理图文详解Excel中XLOOKUP函数典型用法整理Apr 21, 2022 am 11:46 AM

本篇文章给大家带来了关于Excel的相关知识,其中主要介绍了关于XLOOKUP函数的相关知识,包括了常规查询、逆向查询、返回多列、自动除错以及近似查找等内容,下面一起来看一下,希望对大家有帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools