Provides various official and user-released code examples. For code reference, you are welcome to learn thinkphp3.2 and use PHPExcel to export the structure of all tables in the database, including annotations.
First look at the figure
I. The controller code is as follows:
Public function out (){
$ SQL = "show tables"; // read all tables in the database
$ Result = M ()-> query ($ SQL );
Foreach ($ result as $ k => $ v ){
$ K ++;
$ _ SQL = "SHOW FULL COLUMNS FROM". $ v ['tables _ in _ '. C ('db _ name')];
$ Data [] [0] = array ("table {$ k }. {$ v ['tables _ in_test ']} table ",'','','','','','');
$ Data [] [1] = array ("field", "type", "proofreader", "NULL", "key", "default", "additional ", "permission", "comment ");
$ Data [] = M ()-> query ($ _ SQL );
$ Data [] [] = array ();
}
// Import the PHPExcel class library
Import ("Org. Util. PHPExcel ");
Import ("Org. Util. PHPExcel. Writer. Excel5 ");
Import ("Org. Util. PHPExcel. IOFactory. php ");
$ Filename = "test_excel ";
$ HeadArr = array ("ID", "field", "type", "NULL", "key", "default", "additional", "comment ");
$ This-> getExcel ($ filename, $ headArr, $ data );
}
Private function getExcel ($ fileName, $ headArr, $ data ){
// Check the data
If (empty ($ data) |! Is_array ($ data )){
Die ("data must be a array ");
}
$ Date = date ("Y_m_d", time ());
$ FileName. = "_00000000date0000.xls ";
// Create a php excel object. Note that \ is not missing \
$ ObjPHPExcel = new \ PHPExcel ();
$ ObjProps = $ objPHPExcel-> getProperties ();
// Set the header
$ Key = ord ("");
$ Column = 2;
$ ObjActSheet = $ objPHPExcel-> getActiveSheet ();
Foreach ($ data as $ ke => $ row ){
Foreach ($ row as $ key => $ rows) {// write row
$ Span = ord ("B ");
Foreach ($ rows as $ keyName => $ value) {// write a column
$ J = chr ($ span );
$ ObjActSheet-> setCellValue ($ j. $ column, $ value );
$ Span ++;
}
$ Column ++;
}
}
$ FileName = iconv ("UTF-8", "gb2312", $ fileName );
// Set the activity ticket index to the first table, so open it in Excel. This is the first table.
$ ObjPHPExcel-> setActiveSheetIndex (0 );
Header ('content-Type: application/vnd. ms-excel ');
Header ("Content-Disposition: attachment; filename = \" $ fileName \"");
Header ('cache-Control: max-age = 0 ');
$ ObjWriter = \ PHPExcel_IOFactory: createWriter ($ objPHPExcel, 'excel5 ');
$ ObjWriter-> save ('php: // output'); // download the file in a browser
Exit;
}
2. Download the attachment and put the files and folders in the core directory ThinkPHP \ Library \ Org \ Util of tp.
Iii. Configure database configuration items
4. Access the out method to export the excel table.
5. If you want more functions, modify them by yourself. OVER
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB