PHP using Phpexcel to export Excel sample _php tutorial

Source: Internet
Author: User
One of the simplest ways to export excel in PHP is to export a CSV file, but to do so, we can do it with the help of the Phpexcel plugin.

Phpexcel is a very powerful PHP operation of the class Library of Excel, but for the simple data in PHP to export Excel is a bit complicated, in Google Code has a PHP export of Excel class, can be easily called, 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 is 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

/**
* Phpexcel Generate Excel File
* @author: Firmy
* @desc supports arbitrary row and column data to generate Excel files without adding cell styles and alignment
*/

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), array (1,3), array (5,7));
Getexcel ($fileName, $HEADARR, $data);


function Getexcel ($fileName, $HEADARR, $data) {
if (Empty ($data) | |!is_array ($DATA)) {
Die ("Data must is a array");
}
if (empty ($fileName)) {
Exit
}
$date = Date ("Y_m_d", Time ());
$fileName. = "_{$date}.xlsx";

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

//Set header
$key = Ord ("A"),
foreach ($headArr as $v) {
$colum = Chr ($key);
$objPHPExcel->setactivesheet Index (0)->setcellvalue ($colum. ' 1 ', $v);
$key + = 1;
}

$column = 2;
$objActSheet = $objPHPExcel->getactivesheet ();
foreach ($data as $key + = $rows) { The line is written to
$span = Ord ("A");
foreach ($rows as $keyName = + $value) {//column writes
$j = Chr ($span);
$objActSheet->se Tcellvalue ($j. $column, $value);
$span + +;
}
$column + +;
}

$fileName = Iconv ("Utf-8", "gb2312", $fileName);
Renaming a table
$objPHPExcel->getactivesheet ()->settitle (' simple ');
Set the activity Order 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 run, save in current directory
}
Exit

}

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

http://www.bkjia.com/PHPjc/632892.html www.bkjia.com true http://www.bkjia.com/PHPjc/632892.html techarticle One of the simplest ways to export excel in PHP is to export a CSV file, but to do so, we can do it with the help of the Phpexcel plugin. Phpexcel is a very powerful ...

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