PHP: Referencing Phpexcel exporting data to an Excel table

Source: Internet
Author: User

I'm using the tp3.2 framework (: http://www.thinkphp.cn/topic/38123.html)

1. First download the Phpexcel class library, placed in the directory

2. Calling methods

Public Function Exportexcel ($expTitle, $expCellName, $expTableData)
{
$xlsTitle = Iconv (' utf-8 ', ' gb2312 ', $expTitle);//File name
$fileName = $_session[' account '].date (' _ymdhis ');//or $xlsTitle file name can be set according to your own situation
$cellNum = count ($expCellName);
$dataNum = count ($expTableData);
vendor ("Phpexcel.phpexcel");//Introduction of Class library, must be cited, otherwise it will be an error to find this class
$objPHPExcel = new \phpexcel ();
$cellName = Array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' AA ', ' AB ', ' AC ', ' AD ', ' AE ', ' AF ', ' AG ', ' AH ', ' AI ', ' AJ ', ' AK ', ' AL ', ' AM ', ' an ', ' AO ', ' APs ', ' AQ ', ' AR ', ' as ', ' at ', ' AU ', ' AV ', ' AW ', ' AX ', ' AY ', ' AZ ';

$objPHPExcel->getactivesheet (0)->mergecells (' A1: '. $cellName [$cellNum-1]. ' 1 ');//Merge cells
$objPHPExcel->setactivesheetindex (0)->setcellvalue (' A1 ', $expTitle. ' Export time: '. Date (' y-m-d h:i:s ');
for ($i =0; $i < $cellNum; $i + +) {
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($cellName [$i]. ' 2 ', $expCellName [$i][1]);
}
Miscellaneous Glyphs, UTF-8
for ($i =0; $i < $dataNum; $i + +) {
for ($j =0; $j < $cellNum; $j + +) {
$objPHPExcel->getactivesheet (0)->setcellvalue ($cellName [$j]. ( $i +3), $expTableData [$i] [$expCellName [$j][0]];
}
}

Header (' Pragma:public ');
Header (' Content-type:application/vnd.ms-excel;charset=utf-8;name= '. $xlsTitle. '. XLS "');
Header ("Content-disposition:attachment;filename= $fileName. xls");//attachment new window print inline this window
Ob_clean ();//Release the data from the buffer of PHP.
flush ();//Send data that is not buffered or released to the browser.
$objWriter = \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
Exit
}
/**
*
* Export Excel
*/
function Daochu ()
{//Export Excel
$xlsName = "User";
$xlsCell = Array (
Array (' ID ', ' account sequence '),
Array (' U_name ', ' User name '),
Array (' U_addres ', ' address '),
Array (' U_tel ', ' phone '),
Array (' U_card ', ' ID number '),
Array (' u_time ', ' declared time '),
Array (' U_imgz ', ' declared time '),
Array (' U_IMGF ', ' declared time '),
Array (' B_one_beizhu ', ' office staff notes '),
Array (' b_shenhe_p ', ' Office Auditor '),
Array (' water_kj ', ' water meter caliber '),
Array (' water_wz ', ' water meter specific position '),
Array (' Water_quyu ', ' region position '),
Array (' Water_start_time ', ' construction date '),
Array (' Water_start_peo ', ' construction staff '),
Array (' Water_start_jingli ', ' construction manager '),
Array (' Water_beizhu ', ' construction manager notes '),
Array (' Water_xingzhi ', ' water quality '),
Array (' Watch_num ', ' water meter base '),
Array (' Watch_time ', ' meter reading Date '),
Array (' Watch_peo ', ' meter reading staff '),
Array (' Moneyjingli_name ', ' Charge manager '),
Array (' Moneyjingli_beizhu ', ' billing manager notes '),

Array (' Jingli_name ', ' manager name ')

);
$xlsModel = M (' Api_aliucheng ');

$xlsData = $xlsModel->field (' Id,u_name,u_addres,u_tel,u_card,u_time,b_one_beizhu,b_shenhe_p,water_kj,water_wz , Water_quyu,water_start_time,water_start_peo,water_start_jingli,water_beizhu,water_xingzhi,watch_num,watch_ Time,watch_peo,moneyjingli_name,moneyjingli_beizhu,jingli_name ')->where (Array (' Jingli_state_san ' =>1))- >select ();

$this->exportexcel ($xlsName, $xlsCell, $xlsData);

}
3. Export Results (pay special attention to the places marked red above)



PHP: Referencing Phpexcel exporting data to an Excel table

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.