This article mainly introduced the thinkphp framework implementation of the export of Excel data method, combined with the example form analysis thinkphp add org extension based on phpexcel Excel data export operation related implementation skills, the need for friends can refer to the next
In this paper, we describe the method of thinkphp framework for exporting Excel data. Share to everyone for your reference, as follows:
In the thinkphp framework, an example of how to export Excel data:
Before you do this, you should add the ORG library in the extended directory of the system framework to include the thinkphp\extend\library\org\util\phpexcel.class.php file and its associated supporting files.
<?phpheader ("content-type:text/html; Charset=utf-8 "), class Mestestaction extends Action {//test export Excel Data Public function Tpgetexcel () {//Create object Import (" O Rg. Util.phpexcel "); From Phpexcel directory phpexcel.php class file $excel = new Phpexcel (); $data = M ()->query (' SELECT userid,username,stepgoal from Tp_data_user LIMIT 2775 '); Excel table format, where 3 columns are briefly written $letter = Array (' A ', ' B ', ' C '); Table header Array $tableheader = Array (' userid ', ' User name ', ' Target step '); $count = count ($data);//Total data rows $listNum = 500;//The maximum number of data rows per sheet page $num = ceil ($count/$listNum);//sheet page Number $MuitData = Array_chunk ($data, $listNum, false);//Split total data, up to $listnum rows of valid data//var_dump per page ($MuitData);//die (' as '); By default, Phpexcel automatically creates the first sheet, whose index sheetindex=0//sets the sheet that is currently active phpexcel the first sheet created automatically $excel Setactivesheetindex (0); Objphpexcel//Set sheet title $excel->getactivesheet ()->settitle (' Assessment score '. ') 1 '. ' Page '); Set the column name for the sheet for ($k = 0; $k < count ($tableheader), + + $k) {$excel->getactivesheet ()->setcellvalUE ("$letter [$k]". 1 ', "$tableheader [$k]");//First row Data}//Fill table Information processing 1th block Data $crrntSheetLineNo = count ($MuitData [0]) + 1; for ($j = 2; $j <= $crrntSheetLineNo; + + $j) {//traverse each row $k = 0; foreach ($MuitData [0][$j-2] as $key = = $value) {//Traverse a column of a specific row $excel->getactivesheet ()->setcellvalue ("$let ter[$k] ". $j," $value ");//$k row $j $k + +; }}//subsequent sheet pages and blocks for ($i = 1; $i < $num; + + $i) {//create $i sheet $msgWorkSheet = new Phpexcel_worksheet ($excel, ' Assessment score first '. ($i + 1). ' Page '); Create a worksheet $excel->addsheet ($msgWorkSheet); Insert Sheet $excel->setactivesheetindex ($i); Switch to the newly created worksheet//Set the column name for the sheet for ($k = 0; $k < count ($tableheader), + + $k) {$excel->getactivesheet ()-& Gt;setcellvalue ("$letter [$k]1", "$tableheader [$k]");//The first row of data}//Fill table Information Processing Section $i block Data $crrntSheetLineNo = count ($Mui tdata[$i]) + 1; Var_dump ($crrntSheetLineNo); Var_dump ($MuitData [$i-1]);d ie (' as '); for ($j = 2; $j <= $crrntSheetLineNo; + + $j) {//TimesCalendar each row $k = 0; foreach ($MuitData [$i -1][$j-2] as $key + $value) {//Traverse a column of a specific row $excel->getactivesheet ()->setcellvalu E ("$letter [$k] $j", "$value");//$k $j line + + + $k; }} usleep (100); }//Create Excel Output Object $filename = "Grand Prix training assessment score. xls"; $write = new Phpexcel_writer_excel5 ($excel); Ob_end_clean ();//clear buffer, avoid garbled *//output to local $write->save (iconv (' utf-8 ', ' GBK ', $filename)); *///output to the browser header ("Pragma:public"); Header ("expires:0"); Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0"); Header ("Content-type:application/force-download"); Header ("Content-type:application/vnd.ms-execl"); Header ("Content-type:application/download"); Header (' Content-type:application/octet-stream '); $encoded _filename = UrlEncode ($filename); $encoded _filename = str_replace ("+", "%20", $encoded _filename); $ua = $_server["Http_user_agent"]; if (Preg_match ("/msie/", $ua)) {header (' Content-disposItion:attachment; Filename= "'. $encoded _filename. '"'); } else if (Preg_match ("/firefox/", $ua)) {header (' content-disposition:attachment; filename*= ' utf8\ ' \ '. $filename . '"'); } else {header (' content-disposition:attachment; Filename= '. $filename. '"'); } header ("Content-transfer-encoding:binary"); $write->save (' php://output '); }}?>
Articles you may be interested in:
Native JS implements an example of how Ajax interacts with PHP via post. PHP tips
Laravel Integration of Geetest Verification code PHP instance
PHP Example of phpstorm+xdebug breakpoint debugging PHP