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