php将数据库导出成excel的方法
程序员文章站
2022-05-14 11:54:25
上传cvs并导入到数据库中,测试成功(部分代码不规范,如php_self那里要改写成$_server["php_self"] ) php代码 复制代码 代码如下: <...
上传cvs并导入到数据库中,测试成功(部分代码不规范,如php_self那里要改写成$_server["php_self"] )
php代码
<?php
$fname = $_files['myfile']['name'];
$do = copy($_files['myfile']['tmp_name'],$fname);
if ($do)
{
echo"导入数据成功<br>";
} else {
echo "";
}
?>
<form enctype="multipart/form-data" action="<?php echo"".$php_self.""; ?>" method="post">
<p>导入cvs数据 <input name="myfile" type="file"> <input value="提交" type="submit">
</p>
</form>
<?
error_reporting(0);
//导入csv格式的文件
$connect=mysql_connect("localhost","a0530093319","123456") or die("could not connect to database");
mysql_select_db("a0530093319",$connect) or die (mysql_error());
$fname = $_files['myfile']['name'];
$handle=fopen("$fname","r");
while($data=fgetcsv($handle,10000,","))
{
$q="insert into test (code,name,date) values ('$data[0]','$data[1]','$data[2]')";
mysql_query($q) or die (mysql_error());
}
fclose($handle);
?>
用php将数据库导出成excel,测试完全成功
php代码 www.devdao.com
<?php
$db_server = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "ishop";
$db_tblname = "oi_mall_payment";
$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=big5");
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);
?>
php代码
复制代码 代码如下:
<?php
$fname = $_files['myfile']['name'];
$do = copy($_files['myfile']['tmp_name'],$fname);
if ($do)
{
echo"导入数据成功<br>";
} else {
echo "";
}
?>
<form enctype="multipart/form-data" action="<?php echo"".$php_self.""; ?>" method="post">
<p>导入cvs数据 <input name="myfile" type="file"> <input value="提交" type="submit">
</p>
</form>
<?
error_reporting(0);
//导入csv格式的文件
$connect=mysql_connect("localhost","a0530093319","123456") or die("could not connect to database");
mysql_select_db("a0530093319",$connect) or die (mysql_error());
$fname = $_files['myfile']['name'];
$handle=fopen("$fname","r");
while($data=fgetcsv($handle,10000,","))
{
$q="insert into test (code,name,date) values ('$data[0]','$data[1]','$data[2]')";
mysql_query($q) or die (mysql_error());
}
fclose($handle);
?>
用php将数据库导出成excel,测试完全成功
php代码 www.devdao.com
<?php
$db_server = "localhost";
$db_username = "root";
$db_password = "";
$db_dbname = "ishop";
$db_tblname = "oi_mall_payment";
$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=big5");
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);
?>
上一篇: Java继承中的构造方法
下一篇: 提取HTML标签