Home > Article > Backend Development > How to export excel file from mysql in php_PHP tutorial
The article introduces two methods to export data in the mysql database into excel documents. You can refer to each for its own merits.
We use the simplest method to implement it directly using php+mysql. The method is as follows.
代码如下 | 复制代码 |
include('db/db.php'); //包含连库类 $db = new db(); $result = mysql_query('select * from market_sig into outfile "d:product3.xls";'); var_dump($result); ?> |
The above is our native PHP combined with the mysql outfile file export method. One problem with this method is that it cannot implement the download function and is only generated on the server.
The following method is more comprehensive
Download PHPExcel: http://phpexcel.codeplex.com
Let’s take a look at the code first,
The code is as follows
|
Copy code |
||||
phpclass Table_export extends CI_Controller {function __construct() { parent::__construct();// Here you should add some sort of user validation// to prevent strangers from pulling your table data }function index($table_name){ | // $query = mb_convert_encoding("gb2312", "UTF -8", $query);
$this->load->library(' PHPExcel');
{ $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);$col++;