欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

mysql数据导出Excel文件格式

程序员文章站 2022-04-09 23:36:50
...

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 = 'localhost';
$dbname = 'mydb';		//所在数据库名
$dbuser = 'root';
$dbpwd = '';
$language = 'utf8';
$tbname = 'mytable';	//要导出的表单
$style = "border='1' width='100%' 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='$tbname' and table_schema='$dbname'";
$cos = mysqli_query($link,$sql);
 
echo "";
//导出表头(也就是表中拥有的字段)
while($col = mysqli_fetch_assoc($cos)){
 
    if ($col['COLUMN_COMMENT']) {//取备注名组成数组,如果没有,则直接用段名
 
        $t_field[] = $col['COLUMN_COMMENT'];
        echo "".$col['COLUMN_COMMENT']."";
 
    }else{
        $t_field[] = $col['COLUMN_NAME'];
        echo "".$col['COLUMN_NAME']."";
    }
}
 
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 "";
 
?>