php连接excel表格数据,php 怎么把数据导出到excel表格|php连接excel表格数据库

php 怎么把数据导出到excel表格

php 把数据到excel表格有多种方法,比如使用 phpExcel 等,以下代码是通过 header 生成 excel 文件的代码:

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 配置

//链接数据库

$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 "";

//导出表头(也就是表中拥有的字段)

while($row = mysql_fetch_array($res)){

$t_field[] = $row['Field']; //Field中的F要大写,否则没有结果

echo "".$row['Field']."";

}

echo "";

//导出100条数据

$sql = "select * from users limit 100";

$res = mysql_query($sql);

while($row = mysql_fetch_array($res)){

echo "

";

foreach($t_field as $f_key){

echo "".$row[$f_key]."";

}

echo "";

}

echo "";

?>

php如何把mysql数据库导入到excel表格

//需求:用php将mysql导入到excel中

//数据库配置信

$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");

// 数据接

$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");

//注意mysql 导入cxcel中的时候

mysql_query("Set Names 'gbk'");

//定件导出的格式

$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");

//定义要输出的数据表标题

$title = "数据表名:$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";

//逐个取出数据表字段

for ($i = 0; $i < mysql_num_fields($result); $i ) {

echo mysql_field_name($result,$i) . "/t";

}

print("/n");

// $i = 0;

//循环打印出数据表中的数据

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);

?>

php怎么导入Excel表格到数据库,根据表格内的字段修改对应数据,请问大神怎么导入数据库????

5 php怎么导入E

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值