PHP exports tables from any MySQL database to an Excel file

Source: Internet
Author: User

Introduction: This is a detailed page for PHP to export tables from any MySQL database to excel files. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 337541 'rolling = 'no'>

Test the table creation script

Set SQL _mode = "no_auto_value_on_zero ";

--
-- Database: 'mysqlnew'
--

----------------------------------------------------------

--
-- Table structure 'test'
--

Create Table if not exists 'test '(
'Id' int (10) Not null auto_increment,
'Websitename' varchar (200) Character Set utf8 not null,
'Websiteurl' varchar (200) Character Set utf8 not null,
Primary Key ('id ')
) Engine = MyISAM default charset = Latin1 auto_increment = 8;

--
-- Export the table data 'test'
--

Insert into 'test' ('id', 'websitename', 'websiteurl') Values
(1, 'Baidu ', 'HTTP: // www.baidu.com '),
(5, 'Google ', 'HTTP: // www.google.com '),
(4, 'phone 400 ', 'HTTP: // www.my400800.cn '),
(6, 'sogou ', 'www .sogou.com '),
(7, 'bin', 'HTTP: // www.bing.com ');

PHP filesCode

<? PHP

Function xlsbof (){
Echo pack ("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0 );
Return;
}

Function xlseof (){
Echo pack ("SS", 0x0a, 0x00 );
Return;
}

Function xlswritenumber ($ row, $ Col, $ value ){
Echo pack ("sssss", 0x203, 14, $ row, $ Col, 0x0 );
Echo pack ("D", $ value );
Return;
}

Function xlswritelabel ($ row, $ Col, $ value ){
$ L = strlen ($ value );
Echo pack ("Ssssss", 0x204, 8 + $ L, $ row, $ Col, 0x0, $ L );
Echo $ value;
Return;
}

/**
* Add header information
* @ Param <type> $ rsrec
*/
Function addheader ($ rsrec ){
$ Fieldlen = mysql_num_fields ($ rsrec );
Xlsbof ();
$ Icell = 0;
For ($ I = 0; $ I <$ fieldlen; $ I ++ ){
$ Fieldname = mysql_field_name ($ rsrec, $ I );
Xlswritelabel (0, $ icell ++, iconv ("UTF-8", "gb2312", $ fieldname ));
}

}

/**
* Add record information
* @ Param <type> $ rsrec
*/
Function adddata ($ rsrec ){
$ Xlsrow = 1;
$ Fieldlen = mysql_num_fields ($ rsrec );
While ($ rsone = mysql_fetch_object ($ rsrec )){
$ Icell = 0;
For ($ I = 0; $ I <$ fieldlen; $ I ++ ){
$ Fieldname = mysql_field_name ($ rsrec, $ I );
$ Fieldnamevalue = $ rsone-> $ fieldname;
Xlswritelabel ($ xlsrow, $ icell ++, iconv ("UTF-8", "gb2312", $ fieldnamevalue ));
}

$ Xlsrow ++;
}
Xlseof ();
Exit;
}

If (isset ($ _ post ["tablename"]) {

$ Con = mysql_connect ("localhost", "root", "vertrigo ");

$ Result = mysql_db_query ("mysqlnew", "select * from". $ _ post ["tablename"]);

// File Header
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/octet-stream ");
Header ("Content-Type: Application/download ");
Header ("content-Disposition: attachment?filename=test.xls ");
Header ("content-transfer-encoding: Binary ");

// Add data to the table
Addheader ($ result );
Adddata ($ result );

Mysql_close ($ con );

}

?>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> export Excel Information </title>
</Head>
<Body>
<Div>
<Form name = "form1" method = "Post" Action = "" onsubmit = "">
<Input type = "text" name = "tablename" value = ""/>
<Br>

<Input class = "CCC" name = "" type = "Submit" value = "Submit"/>
</Form>
</Div>
</Body>
</Html>

Running Screen

Generate EXCEL file content

ID Websitename Websiteurl
1 Baidu Http://www.baidu.com
5 Google Http://www.google.com
4 400 phone Http://www.my400800.cn
6 Sogou Www.sogou.com
7 Bing Http://www.bing.com

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/337541.html pageno: 9.

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.