Example of exporting Excel using PHPExcel in PHP_PHP tutorial
One of the easiest ways to export excel in php is to export a csv file, but to truly export excel files we can use the PHPExcel plug-in.
PHPExcel is a very powerful PHP class library for operating Excel, but it is a bit complicated to simply export data to Excel using PHP. There is a PHP Excel export class on Google Code, which can be easily called and is very convenient. .
The code is as follows | Copy code | ||||
require 'php-excel.class.php'; // create a simple 2-dimensional array $data = array( 1 => array ('Name', 'Surname'), array('Schwarz', 'Oliver'), array('Test', 'Peter') ); // generate file (constructor parameters are optional) $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); $xls->addArray($data); $xls->generateXML('my-test'); ?> |
例2
代码如下
|
复制代码
|
||||
/** |
require_once 'library/PHPExcel/Reader/Excel2007.php';
require_once 'library/PHPExcel/Reader/Excel5.php';
include_once 'library/PHPExcel/IOFactory.php';<🎜> <🎜>$fileName = "test_excel";
$headArr = array("第一列","第二列","第三列");
$data = array(array(1,2),array(1,3),array(5,7));
getExcel($fileName,$headArr,$data);<🎜> <🎜>
function getExcel($fileName,$headArr,$data){
if(empty($data) || !is_array($data)){
die("data must be a array");
}
if(empty($fileName)){
exit;
}
$date = date("Y_m_d",time());
$fileName .= "_{$date}.xlsx";<🎜> <🎜> //创建新的PHPExcel对象
$objPHPExcel = new PHPExcel();
$objProps = $objPHPExcel->getProperties();
//设置表头
$key = ord("A");
foreach($headArr as $v){
$colum = chr($key);
$objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
$key += 1;
}
$column = 2;
$objActSheet = $objPHPExcel->getActiveSheet();
foreach($data as $key => $rows){ //行写入
$span = ord("A");
foreach($rows as $keyName=>$value){// 列写入
$j = chr($span);
$objActSheet->setCellValue($j.$column, $value);
$span++;
}
$column++;
} $fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
$objPHPExcel->getActiveSheet()->setTitle('Simple');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
//将输出重定向到一个客户端web浏览器(Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-Disposition: attachment; filename="$fileName"");
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
if(!empty($_GET['excel'])){
$objWriter->save('php://output'); //文件通过浏览器下载
}else{
$objWriter->save($fileName); //脚本方式运行,保存在当前目录
}
exit; } phpexcel类的下载地址:http://php-excel.googlecode.com/files/php-excel-v1.1-20090910.zip

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











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
