Table of Contents
mysql2Excel.php
Home php教程 PHP源码 mysql数据导出Excel文件格式

mysql数据导出Excel文件格式

May 23, 2016 pm 01:07 PM

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 "";
 
?>
Copy after login

                                       


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)