PHP export MySQL database for Excel code

Source: Internet
Author: User
Tags mysql tutorial pack php tutorial mysql database

SET sql_mode= "No_auto_value_on_zero";

--
--Database Tutorial: ' MySQL tutorial new '
--

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

--
--The structure of the table ' test '
--

CREATE TABLE IF not EXISTS ' test ' (
' ID ' int (a) not NULL auto_increment,
' Websitename ' varchar character set UTF8 not NULL,
' Websiteurl ' varchar character set UTF8 not NULL,
PRIMARY KEY (' id ')
) Engine=myisam DEFAULT charset=latin1 auto_increment=8;

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

INSERT into ' test ' (' id ', ' websitename ', ' Websiteurl ') VALUES
(1, 'Baidu', 'http://www.baidu.com'),
(5, 'Google', 'http://www.google.com'),
(4, '400 Telephone', 'http://www.111cn.net'),
(6, 'Sogou', 'www.sogou.com'),
(7, 'Bing', 'http://www.111cn.net');

PHP Tutorial File code

<?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, $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 a 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" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title>excel Information Export </title>
<body>
<div>
<form name= "Form1" method= "Post" action= "" onsubmit= "" >
<input type= "text" name= "tablename" value= ""/>
<br>

<input class= "CCC" Name= "" type= "Submit" value= "submitted"/>
</form>
</div>
</body>

Run the screen

Generate Excel File contents

6
id websitename websiteurl    
Baidu http://www.baidu.com  
Google http://www.google.com  
http://www ... 111cn.net  
sogou w ww.sogou.com    
7 Bing http://www.111cn.net
Related Article

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.