Using Phpexcel to export Excel samples in PHP

Source: Internet
Author: User

Phpexcel is a very powerful class library for PHP operations Excel, but it's a little complicated to simply export data to excel in PHP, which has a class of PHP export Excel on Google code that can be easily invoked.

The code is as follows Copy Code

<?php
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 ');
?>

Example 2

The code is as follows Copy Code

<?php
/**
* Phpexcel Generate Excel files
* @author: Firmy
* @desc supports arbitrary row and column data to generate Excel files, no cell styles and snaps are added
*/

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 ("First column", "second column", "third column");
$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";

Create a new Phpexcel object
$objPHPExcel = new Phpexcel ();
$objProps = $objPHPExcel->getproperties ();

To set the table header
$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) {//row write
$span = Ord ("A");
foreach ($rows as $keyName => $value) {//Column write
$j = Chr ($span);
$objActSheet->setcellvalue ($j. $column, $value);
$span + +;
}
$column + +;
}

$fileName = Iconv ("Utf-8", "gb2312", $fileName);
Renaming tables
$objPHPExcel->getactivesheet ()->settitle (' simple ');
Set the activity single index to the first table, so Excel opens this is the first table
$objPHPExcel->setactivesheetindex (0);
REDIRECT output to a client Web browser (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 '); Files are downloaded through the browser
}else{
$objWriter->save ($fileName); Script to run, save in the current directory
}
Exit

}

Download address of Phpexcel class: Http://php-excel.googlecode.com/files/php-excel-v1.1-20090910.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.