Thinkphp3.2 use PHPExcel to export the structure of all tables in the database

Source: Internet
Author: User
Tags php excel
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

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.