Home > Backend Development > PHP Tutorial > Example of exporting Excel using PHPExcel in PHP_PHP tutorial

Example of exporting Excel using PHPExcel in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:47:02
Original
956 people have browsed it

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
 代码如下 复制代码

// load library
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');
?>

// load library
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

/**
* PHPEXCEL generates excel files
* @author:firmy
* @desc supports generating excel files from any row and column data, but cell styles and alignment have not been added yet
​*/
 代码如下
 代码如下 复制代码

/**
* PHPEXCEL生成excel文件
* @author:firmy
* @desc 支持任意行列数据生成excel文件,暂未添加单元格样式和对齐
*/

require_once 'library/PHPExcel.php';
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;

}

复制代码

require_once 'library/PHPExcel.php';
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632892.htmlTechArticleOne of the easiest ways to export excel in php is to export a csv file, but you must actually export excel We can realize the file with the help of PHPExcel plug-in. PHPExcel is a very powerful...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template