ThinkPHP5.0 reference PHPExcel plug-ins, export database data in the page

extend a file, ThinkPHP5.0 folder under the framework of the introduction of plug-PHPExcel

 

Second, the common functions:

/ ** 
* Export data to xls format success
* @param $ headArr // header
* @param $ data // query data
* @param $ filename // download file name
* Example:
* the Data $ db = ( 'the Message ') -> wherein. (' ID ', $ IDS) -> SELECT ();
* $ = Field Array (
*' A '=> Array (' ID ',' unique identifiers'),
* 'B' => array ( 'title', 'theme'),
* 'C' => Array ( 'phoneNum', 'phone'),
* 'D' => Array ( 'AddTime', 'time'),
*);
* phpExcel (Field $, $ Data, DATE ( 'Ym of-D'));
* /
function phpExcel (headArr $, $ Data, $ filename)
{
// file is introduced
require_once (EXTEND_PATH 'PHPExcel / Classes / PHPExcel.php'.) ;
// Instantiate
$ objPHPExcel = new PHPExcel ();
$ objWriter = new PHPExcel_Writer_Excel5 ($ objPHPExcel ); // save the settings format version
the foreach ($ AS Data Key $ => $ value) {
the foreach ($ headArr AS = K $> $ V) {

// Set the column header statements
if ( Key == 0 $) {
$ objPHPExcel-> getActiveSheet () -> setCellValue ($ K '. 1', V $ [. 1]);.
}

// Add the contents of the table
$ i = $ key + 2; // table is 2 starting from
$ objPHPExcel-> getActiveSheet () -> setCellValue (K $ I $, $ value [$ V [0]].);


/ **
* set the relevant table
* /

// when formatting PHPExcel_Style_NumberFormat :: FORMAT_NUMBER to avoid some big numbers
// is displayed using scientific notation, with the following setAutoSize ways to make the contents of each line
// are all displayed according to the original content
$ objPHPExcel-> getActiveSheet () -> the getStyle (K $ I $.) -> getNumberFormat () -> setFormatCode (PHPExcel_Style_NumberFormat :: FORMAT_NUMBER);

// set the cell width adaptive text length
$ objPHPExcel-> getActiveSheet () - > getColumnDimension ($ k) -> setAutoSize (to true);
// centered horizontally
// $ objPHPExcel-> getActiveSheet () - > getStyle (. $ k $ i) -> getAlignment () -> setHorizontal (\ PHPExcel_Style_Alignment :: HORIZONTAL_CENTER );
// centered vertically
. // $ objPHPExcel-> getActiveSheet () -> getStyle ($ k $ i) -> getAlignment () -> setVertical (\ PHPExcel_Style_Alignment :: VERTICAL_CENTER);
automatically wrap text //
$ objPHPExcel-> getActiveSheet () -> getStyle ($ k $ i.) -> getAlignment () -> setWrapText (true);
}
}

// send to the client as a download format xls header information
    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/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename=' . $filename . '.xls');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}

Third, call the function in the controller, to download the file

 

Guess you like

Origin www.cnblogs.com/dandanyoudu/p/11857890.html