php 把數(shù)據(jù)導(dǎo)出到excel表格有多種方法,比如使用 phpExcel 等,以下代碼是直接通過 header 生成 excel 文件的代碼示例:

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于網(wǎng)站設(shè)計、網(wǎng)站制作、武威網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、武威網(wǎng)絡(luò)營銷、武威企業(yè)策劃、武威品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供武威建站搭建服務(wù),24小時服務(wù)熱線:13518219792,官方網(wǎng)址:chinadenli.net
?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=xls_region.xls");
$cfg_dbhost?=?'localhost';
$cfg_dbname?=?'testdb';
$cfg_dbuser?=?'root';
$cfg_dbpwd?=?'root';
$cfg_db_language?=?'utf8';
//?END?配置
//鏈接數(shù)據(jù)庫
$link?=?mysql_connect($cfg_dbhost,$cfg_dbuser,$cfg_dbpwd);
mysql_select_db($cfg_dbname);
//選擇編碼
mysql_query("set?names?".$cfg_db_language);
//users表
$sql?=?"desc?users";
$res?=?mysql_query($sql);
echo?"tabletr";
//導(dǎo)出表頭(也就是表中擁有的字段)
while($row?=?mysql_fetch_array($res)){
$t_field[]?=?$row['Field'];?//Field中的F要大寫,否則沒有結(jié)果
echo?"th".$row['Field']."/th";
}
echo?"/tr";
//導(dǎo)出100條數(shù)據(jù)
$sql?=?"select?*?from?users?limit?100";
$res?=?mysql_query($sql);
while($row?=?mysql_fetch_array($res)){
echo?"tr";
foreach($t_field?as?$f_key){
echo?"td".$row[$f_key]."/td";
}
echo?"/tr";
}
echo?"/table";
?
?php
//需求:用php將mysql數(shù)據(jù)導(dǎo)入到excel中
//數(shù)據(jù)庫配置信息
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "admin";
$DB_DBName = "shop";
$DB_TBLName = "sdb_widgets_set";
$savename = date("Y-m-j H:i:s");
// 數(shù)據(jù)庫連接
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
//注意mysql 導(dǎo)入cxcel中的時候
mysql_query("Set Names 'gbk'");
//定義文件導(dǎo)出的格式
$file_type = "vnd.ms-excel";
//定義文件后綴名稱
$file_ending = "xls";
header("Content-Type: application/$file_type;charset=gbk");
header("Content-Disposition: attachment; filename=".$savename.".$file_ending");
//header("Pragma: no-cache");
$now_date = date("Y-m-j H:i:s");
//定義要輸出的數(shù)據(jù)表標題
$title = "數(shù)據(jù)表名:$DB_TBLName, | 日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database");
$result = @mysql_query($sql,$Connect) or die(mysql_error());
echo("$title/n");
//定義制表格符號
$sep = "/t";
//逐個取出數(shù)據(jù)表字段
for ($i = 0; $i mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "/t";
}
print("/n");
// $i = 0;
//循環(huán)打印出數(shù)據(jù)表中的數(shù)據(jù)
while($row = mysql_fetch_row($result)) {
$schema_insert = "";
for($j=0; $j mysql_num_fields($result);$j++) {
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
else if ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "/t";
print(trim($schema_insert));
print "/n";
// $i++;
}
return (true);
?
如果你要求的excle表格不是很復(fù)雜 .可以用html的table表格來實現(xiàn).因為excle的兼容性可以打開xml文檔.所以你可以使用html文件來當做excle文件.
如果比較復(fù)雜 ,就要使用phpexcle了
這個是網(wǎng)頁自動導(dǎo)出的基本思路
如果只是導(dǎo)出幾個少數(shù)的表和內(nèi)容
你可以使用phpmyadmin的導(dǎo)出功能.能夠?qū)崿F(xiàn)導(dǎo)出excle文檔.
$result = mysql_query("SELECT * FROM table_name");
while($row = mysql_fetch_array($result))
{
echo $row['ID'] . " " . $row['xuehao'] . " " . $row['xingming'] . " " . $row['chengji1'] . " " . $row['chengji2'];
echo "br /";
}
下面是我寫的一個PHP導(dǎo)出數(shù)據(jù)到CSV問價的函數(shù),你到時候直接調(diào)用就行了
/**
*?導(dǎo)出CSV文件
*?@param?string?$fileName 文件名字
*?@param?string|array?$data?導(dǎo)出數(shù)據(jù),csv格式的字符串|數(shù)值數(shù)組
*?@param?string?$to_encoding?目標轉(zhuǎn)換編碼
*?@param?string?$from_encoding?當前編碼
*/
function?exportCSV($fileName?=?'',?$data?=?'',?$to_encoding?=?'gb2312',?$from_encoding?=?'utf-8')?{
$fileName?=?empty($fileName)???date('YmdHis')?:?$fileName;
//?文件標簽
Header("Content-type:?application/octet-stream");
header("Content-type:?application/vnd.ms-excel;?charset=$from_encoding");
Header("Content-Disposition:?attachment;?filename=$fileName.csv");
$str?=?'';
if($data)?{
if(is_array($data))?{
foreach?($data?as?$v)?{
if(is_array($v))?{
foreach?($v?as?$vo)?{
$str?.=?(is_numeric($vo)???"'".$vo?:?$vo."").",";
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?.=?(is_numeric($v)???"'".$v?:?$v).",";
}
}
$str?=?trim($str,?",")."\r\n";
}?else?{
$str?=?$data;
}
}
echo?mb_convert_encoding($str,?"gb2312",?"utf-8");
exit;
}
新聞標題:php封裝數(shù)據(jù)輸入到表格,php封裝數(shù)據(jù)輸入到表格中
文章源于:http://chinadenli.net/article23/dsiehjs.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、小程序開發(fā)、App設(shè)計、網(wǎng)站策劃、標簽優(yōu)化、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)