MySQL export Excel file method in PHP

Source: Internet
Author: User

We use the simplest method directly with Php+mysql to achieve, the method is as follows.

The code is as follows Copy Code
<?php
Include (' db/db.php '); Include a library class
$db = new db ();
$result = mysql_query (' select * market_sig into outfile ' D:product3.xls; ');
Var_dump ($result);
?>

Above is our native PHP combined with the MySQL outfile file export method, this method has a problem is not to implement the download function, only in the build on the server.

The following methods are more comprehensive

Download phpexcel:http://phpexcel.codeplex.com

Let's look at the code first.

The code is as follows Copy Code

<?php

Class 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)
{
$this->load->database ();
$query = $this->db->query ("select * from ' $table _name ' WHERE del= 1");
$query = mb_convert_encoding ("gb2312", "UTF-8", $query);
if (! $query)
return false;

Starting the Phpexcel Library
$this->load->library (' Phpexcel ');
$this->load->library (' phpexcel/iofactory ');

$objPHPExcel = new Phpexcel ();
$objPHPExcel->getproperties ()->settitle ("Export")->setdescription ("none");

$objPHPExcel->setactivesheetindex (0)
->setcellvalue (' A1 ', iconv (' GBK ', ' utf-8 ', ' Chinese Hello '))
->setcellvalue (' B2 ', ' world! ')
->setcellvalue (' C1 ', ' Hello ');
Field names in the
$fields = $query->list_fields ();
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col, 1, $field);
$col + +;
}

Fetching the table data
$row = 2;
foreach ($query->result () as $data)
{
$col = 0;
foreach ($fields as $field)
{
$objPHPExcel->getactivesheet ()->setcellvaluebycolumnandrow ($col, $row, $data-> $field);
$col + +;
}

$row + +;
}

$objPHPExcel->setactivesheetindex (0);

$objWriter = Iofactory::createwriter ($objPHPExcel, ' Excel5 ');

Send banner to force users to download files
Header (' Content-type:application/vnd.ms-excel ');
Header (' content-disposition:attachment;filename= ' products_ '. Date (' dmy '). XLS "');
Header (' cache-control:max-age=0 ');

$objWriter->save (' php://output ');
}

}
?>


Look at the configuration method.

1 Extract the contents of the Classes folder in the compressed package into the Applicationlibraries directory, the directory structure is as follows:

--applicationlibrariesphpexcel.php

--Applicationlibrariesphpexcel (folder)

2) Modify applicationlibrariesphpexceliofactory.php file

--Change its class name from Phpexcel_iofactory to Iofactory and follow the CI class naming rules.

--change its constructor to public


There are many other ways to use this method like this one, because Phpexcel is a practical plug-in that is easy to operate on Excel tables.

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.