将mysql检索结果导出EXCEL表

作者:李悦 发表于:2008年12月5日
版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息及本版权声明http://www.liyue.org/tech/archives/94

说明:数据表fawen是文件的标题,正文等信息,数据表pishi是各领导对文件的批示信息,pishi的articleid字段内容与fawen的id相关联。要统计出某位领导的所有批示,导出EXCEL,内容是:文件名称、批示内容、批示时间。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
$DB_Server = "localhost";
$DB_Username = "root";
$DB_Password = "pass";
$DB_DBName = "office";
$DB_TBLName = "fawen";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date('Y-m-d H:i');
$title = "格式:第一列为文件名,第二列为领导批示内容,第三列为批示日期";
$sql = "Select $DB_TBLName.title,pishi.content,pishi.time from $DB_TBLName,pishi where pishi.leadername='领导名称' and $DB_TBLName.id=pishi.articleid";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database");
 
 
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
 
$result2 = @mysql_query($sql2,$Connect)
or die(mysql_error());
 
echo("$titlen");
$sep = "t";
//for ($i = 0; $i < mysql_num_fields($result); $i++) {
//echo mysql_field_name($result,$i) . "n";
//}
print("nnn");
$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 "nnnn";
$i++;
}
return (true);
?>
目前还没有任何评论.