首页 >php教程 >PHP源码 >mysql数据导出Excel文件格式

mysql数据导出Excel文件格式

PHP中文网
PHP中文网原创
2016-05-23 13:07:391514浏览

mysql2Excel.php

<?php
/**
* author:PHP中文网
* create at:2015-11-13
* last mod:	2015-12-12 14:23:52
*/
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_data.xls");
// header("Content-type:text/html;charset=utf-8"); //测试启用
 
$dbhost = &#39;localhost&#39;;
$dbname = &#39;mydb&#39;;		//所在数据库名
$dbuser = &#39;root&#39;;
$dbpwd = &#39;&#39;;
$language = &#39;utf8&#39;;
$tbname = &#39;mytable&#39;;	//要导出的表单
$style = "border=&#39;1&#39; width=&#39;100%&#39; cellspacing=0";//自定义表的样式,如果加CSS请使用连接符.
 
//链接数据库
$link = mysqli_connect($dbhost,$dbuser,$dbpwd,$dbname);
//设置utf8编码
mysqli_query($link,"set names ".$language);
 
//获取user表的段名和备注
$sql = "select COLUMN_NAME,COLUMN_COMMENT from INFORMATION_SCHEMA.Columns where table_name=&#39;$tbname&#39; and table_schema=&#39;$dbname&#39;";
$cos = mysqli_query($link,$sql);
 
echo "";
//导出表头(也就是表中拥有的字段)
while($col = mysqli_fetch_assoc($cos)){
 
    if ($col[&#39;COLUMN_COMMENT&#39;]) {//取备注名组成数组,如果没有,则直接用段名
 
        $t_field[] = $col[&#39;COLUMN_COMMENT&#39;];
        echo "".$col[&#39;COLUMN_COMMENT&#39;]."";
 
    }else{
        $t_field[] = $col[&#39;COLUMN_NAME&#39;];
        echo "".$col[&#39;COLUMN_NAME&#39;]."";
    }
}
 
echo "";
//查出10条数据
$sql = "select * from $tbname limit 10";
$res = mysqli_query($link,$sql);

while($row = mysqli_fetch_array($res)){
    echo "";
    for ($i=0; $i < count($t_field); $i++) { //循环输出一行记录
        echo "".$row[$i]."";
    }
    echo "";
}
echo "";
 
?>

                                       


声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn