求源码!PHP导出数据到csv文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了求源码!PHP导出数据到csv文件相关的知识,希望对你有一定的参考价值。
要求:在php程序里提出需要导出的数据,并按一定要求的格式重新写入CSV文件里面!
哪位大虾做过? 求源码! 急!
需要输出来的格式为下 求源码!!
"goods_id","price","shipfee"这是字段
导出来的数据
"1","10","15"
"2","12","5"
"3","13","4"
............
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "";
$DB_DBName = "DBName";
$DB_TBLName = "DB_TBLName";
$savename = date("YmjHis");
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect.");
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_DBName,数据表:$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;
elseif ($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);
?> 参考技术A 为你写了一段最简单的演示代码.
$filename = 'demo';
header("Content-Disposition: attachment; filename=".$filename.".csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
echo "字段1,字段2,字段3\r\n";
echo "aaa,bbb,ccc\r\n";//这里可以换成循环语句从查询结果拿数据.记得每条数据以\r\n结束.
echo "222,333,444\r\n";
以上是关于求源码!PHP导出数据到csv文件的主要内容,如果未能解决你的问题,请参考以下文章
用php导入10W条+ 级别的csv大文件数据到mysql。导出10W+级别数据到csv文件
忽略空列以将数据导出到 php mysql 中的 csv 文件