PHP export Excel file, the second step to implement a self-write two-dimensional array template Excel file after export

Source: Internet
Author: User
Tags echo date

Today, the main research data to add to Excel and export the problem, do not extract data from the database export, first write a two-dimensional array, and then traverse the two-dimensional array to write Excel template export, first according to the content of the template Excel to write the corresponding two-dimensional array

$arr =array (Array ("111-3004394-8497032", "umn207-05mm", "umn207-05mm", "2", "Eric S herbert/entergy", "Rockyhill Rd") , "PNPS", "", "Plymouth", "Ma", "02360", "US", "508 830-8823", "", "", "", "", "", "" "," 1 "," "),
Array ("112-3297805-3545827", "umn207-05mm", "umn207-05mm", "1", "Yibai Liao", "one Bannister DR", "", "", "HOPEWELL JUNCTION "," "," 12533-8204 "," US "," 9175926724 "," "," "," "," "," "," "", "1", "" ");

Then use foreach to traverse the two-dimensional array and write the template Excle file with the following code:

$row = 2;

foreach ($arr as $order)
{
$col = 0;
foreach ($order as $val)

$objPHPExcel->setactivesheetindex ()->setcellvaluebycolumnandrow ($col, $row, $val);
$col + +;
}

$row + +;
}

$row as a row, specify to insert data from the second row, and loop wrap, $val as two-dimensional array data, $col as columns, basic implementation data to the Excel table and exported, the overall code is as follows:

<?php
if ($_post[' Eub ']==1)
{
Header ("Location:listorders.php?t=orderstatus&fc=all&orderstatus=unshipped&range=14&submit=go" );
}
Error_reporting (E_all);
Ini_set (' display_errors ', TRUE);
Ini_set (' display_startup_errors ', TRUE);
Date_default_timezone_set (' Europe/london ');

if (Php_sapi = = ' CLI ')
Die (' This example should only is run from a Web Browser ');

/** Include Phpexcel */
Require_once dirname (__file__). ‘/.. /plugins/phpexcel-1.8/classes/phpexcel.php ';


Create New Phpexcel Object
$objPHPExcel = new Phpexcel ();

Read from Excel2007 (. xlsx) template
echo Date (' H:i:s '), "Load Excel5 template file", EOL;
$objReader = Phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = $objReader->load (".. /template/eub.xls ");
Print_r ($objPHPExcel);
Die ();

$filename = "eub-". Date ("Ymdhis"). ". XLS ";

$row = 2;

ADD some data
$arr =array (Array ("111-3004394-8497032", "umn207-05mm", "umn207-05mm", "2", "Eric S herbert/entergy", "Rockyhill Rd") , "PNPS", "", "Plymouth", "Ma", "02360", "US", "508 830-8823", "", "", "", "", "", "" "," 1 "," "),
Array ("112-3297805-3545827", "umn207-05mm", "umn207-05mm", "1", "Yibai Liao", "one Bannister DR", "", "", "HOPEWELL JUNCTION "," "," 12533-8204 "," US "," 9175926724 "," "," "," "," "," "," "", "1", "" ");

foreach ($OrderList as $order)
{
$col = 0;
foreach ($order as $val)

$objPHPExcel->setactivesheetindex ()->setcellvaluebycolumnandrow ($col, $row, $val);
$col + +;
}

$row + +;
}

Redirect output to a client ' s Web browser (Excel2007)
Header (' content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset= ' GB2312 "');
Header (' Content-disposition:attachment;filename= '. $filename. ' ');
Header (' cache-control:max-age=0 ');
If you ' re serving to IE 9 and then the following is needed
Header (' cache-control:max-age=1 ');

If you ' re serving to IE over SSL and then the following is needed
Header (' Expires:mon, Jul 1997 05:00:00 GMT '); Date in the past
Header (' last-modified: '. Gmdate (' d, D M Y h:i:s '). ' GMT '); Always modified
Header (' Cache-control:cache, Must-revalidate '); http/1.1
Header (' Pragma:public '); http/1.0

$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');
$objWriter->save (' php://output ');
Exit
?>

The next step is to read the data from the database to the Excel Template table and export the implementation process.

PHP export Excel file, the second step to implement a self-write two-dimensional array template Excel file after export

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.