我传的这个代码是从本地自己创建的数据库里面导出数据并以excel文件打开,代码如下,你可以参考一下: <?php // export records from DB to excel function Create_Excel_File($ExcelFile,$Data) { header('Content-Type: application/vnd.ms-excel;');// This should work for IE & Opera header("Content-type: application/x-msexcel");// This should work for the rest header ("Content-Disposition: attachment; filename=$ExcelFile" ); header ("Content-Description: PHP/INTERBASE Generated Data" );
function xlsBOF() // write excel file begin flag { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() // write excel file end flag { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteNumber($Row, $Col, $Value) { echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); echo pack("d", $Value); return; } function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } xlsBOF(); for ($i=0;$i<count($Data);$i++) { for ($j=0;$j<count($Data[$i]);$j++) { $v = $Data[$i][$j]; xlsWriteLabel($i,$j,$v); } } xlsEOF(); }// 连接选择数据库 $link = mysql_connect("localhost", "root", "1") or die("Could not connect: " . mysql_error()); mysql_select_db("test") or die("Could not select database"); // 执行 SQL 查询 $sql = "SELECT * FROM test"; $res = mysql_query($sql) or die("Query failed : " . mysql_error()); $ss = array(); while ($line = mysql_fetch_row($res)) { $ss[] = $line; }/* while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) { $ss[] = array($line['encode'],$line['decode'],$line['num']); } */mysql_free_result($res);// 释放资源 mysql_close($link);// 断开连接Create_Excel_File("Book2.xls",$ss);?>
<?php
// export records from DB to excel
function Create_Excel_File($ExcelFile,$Data)
{
header('Content-Type: application/vnd.ms-excel;');// This should work for IE & Opera
header("Content-type: application/x-msexcel");// This should work for the rest
header ("Content-Disposition: attachment; filename=$ExcelFile" );
header ("Content-Description: PHP/INTERBASE Generated Data" );
function xlsBOF() // write excel file begin flag
{
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() // write excel file end flag
{
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value)
{
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteLabel($Row, $Col, $Value )
{
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
} xlsBOF();
for ($i=0;$i<count($Data);$i++)
{
for ($j=0;$j<count($Data[$i]);$j++)
{
$v = $Data[$i][$j];
xlsWriteLabel($i,$j,$v);
}
}
xlsEOF();
}// 连接选择数据库
$link = mysql_connect("localhost", "root", "1") or die("Could not connect: " . mysql_error());
mysql_select_db("test") or die("Could not select database");
// 执行 SQL 查询
$sql = "SELECT * FROM test";
$res = mysql_query($sql) or die("Query failed : " . mysql_error());
$ss = array();
while ($line = mysql_fetch_row($res))
{
$ss[] = $line;
}/*
while ($line = mysql_fetch_array($res, MYSQL_ASSOC))
{
$ss[] = array($line['encode'],$line['decode'],$line['num']);
}
*/mysql_free_result($res);// 释放资源
mysql_close($link);// 断开连接Create_Excel_File("Book2.xls",$ss);?>
EXCEL_Reader_Class和EXCEL_Writer_Class
根据EXCEL格式读写的,你sf搜搜看
加装 mysql,obdc 3.51
在excel里用VBA直接连ado调用mysql,直接写入excel其他都或多或少有问题
http://club.21php.com/showthread.php?t=2482不知道对你会否有帮助
header("Content-Type: application/vnd.ms-excel");
header("Pragma: no-cache");
header("Expires: 0");
连接数据库
$sql = "select * from test";
$result = mysql_query($sql);
echo "字段名0"."\t";
echo "字段名1"."\t";
echo "字段名2"."\t";
echo "字段名3"."\t";
echo "字段名4"."\t";
echo "字段名5"."\n";
while ($row = mysql_fetch_array($ressult))
{
echo $row[字段0]."\t";
echo $row[字段1]."\t";
echo $row[字段2]."\t";
echo $row[字段3]."\t";
echo $row[字段4]."\t";
echo $row[字段5]."\n";
}