使用PHP从mySQL到XLS?
如何使用PHP从mySQL表创建.XLS文档 我几乎什么都试过了,没有成功 基本上,我需要获取表单数据,并将其输入数据库(我已经完成了),然后我需要检索该表数据并将其解析为microsoft excel文件,该文件需要自动保存到web服务器上使用PHP从mySQL到XLS?,php,mysql,xls,Php,Mysql,Xls,如何使用PHP从mySQL表创建.XLS文档 我几乎什么都试过了,没有成功 基本上,我需要获取表单数据,并将其输入数据库(我已经完成了),然后我需要检索该表数据并将其解析为microsoft excel文件,该文件需要自动保存到web服务器上 <?php // DB TABLE Exporter // // How to use: // // Place this file in a safe place, edit the info just below here // bro
<?php
// DB TABLE Exporter
//
// How to use:
//
// Place this file in a safe place, edit the info just below here
// browse to the file, enjoy!
// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO
$dbhost = "-";
$dbuser = "-";
$dbpass = "-";
$dbname = "-";
$dbtable = "-";
// END CHANGING STUFF
$cdate = date("Y-m-d"); // get current date
// first thing that we are going to do is make some functions for writing out
// and excel file. These functions do some hex writing and to be honest I got
// them from some where else but hey it works so I am not going to question it
// just reuse
// This one makes the beginning of the xls file
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
// This one makes the end of the xls file
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
// this will write text in the cell you specify
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// make the connection an DB query
$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname );
$q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'";
$qr = mysql_query( $q ) or die( mysql_error() );
// start the file
xlsBOF();
// these will be used for keeping things in order.
$col = 0;
$row = 0;
// This tells us that we are on the first row
$first = true;
while( $qrow = mysql_fetch_assoc( $qr ) )
{
// Ok we are on the first row
// lets make some headers of sorts
if( $first )
{
foreach( $qrow as $k => $v )
{
// take the key and make label
// make it uppper case and replace _ with ' '
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
// prepare for the first real data row
$col = 0;
$row++;
$first = false;
}
// go through the data
foreach( $qrow as $k => $v )
{
// write it out
xlsWriteLabel( $row, $col, $v );
$col++;
}
// reset col and goto next row
$col = 0;
$row++;
}
xlsEOF();
exit();
?>
我似乎不知道如何将fwrite集成到所有这些中,以便将生成的数据写入一个.xls文件中,我该怎么做呢
我需要得到这个工作相当紧迫,所以任何帮助将不胜感激。
Thanx guys.如果您的数据库有某种前端(如phpMyAdmin或SQLyog),您可以将表(或任何SELECT查询的结果)导出到CSV并在Excel中打开 评论后编辑: 我曾经创建过一个XLS。这有点不同,但我所做的是将其放在PHP的顶部(在生成任何输出之前): 在脚本的其余部分,我只是重复了一个表(table、tr、td…等等)
此脚本的执行将为用户提供下载。我认为Content disposition属性有一些不同的选项(可能有一个选项允许脚本保存文件)。如果我没有弄错,您发布的脚本工作正常并创建了正确的xls文件,您只想保存输出,请参阅。使用ob_get_clean(),您可以获取创建的xls文件并将其写入服务器的某个位置
也许你还可以考虑其他选项,比如把你的数据保存到Excel中可以读取的其他格式(.CSV,它也可以读取一些HTML/XML表)。
<?php
// DB TABLE Exporter
//
// How to use:
//
// Place this file in a safe place, edit the info just below here
// browse to the file, enjoy!
// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO
$dbhost = "-";
$dbuser = "-";
$dbpass = "-";
$dbname = "-";
$dbtable = "-";
// END CHANGING STUFF
$cdate = date("Y-m-d"); // get current date
// first thing that we are going to do is make some functions for writing out
// and excel file. These functions do some hex writing and to be honest I got
// them from some where else but hey it works so I am not going to question it
// just reuse
// This one makes the beginning of the xls file
function xlsBOF() {
$output = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
// This one makes the end of the xls file
function xlsEOF() {
$output .= pack("ss", 0x0A, 0x00);
return;
}
// this will write text in the cell you specify
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
$output .= pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
$file = fopen("exported.xls","w");
fwrite($file, "$output");
fclose($file);
return;
}
// make the connection an DB query
$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname );
$q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'";
$qr = mysql_query( $q ) or die( mysql_error() );
// Ok now we are going to send some headers so that this
// thing that we are going make comes out of browser
// as an xls file.
//
//header("Pragma: public");
//header("Expires: 0");
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("Content-Type: application/force-download");
//header("Content-Type: application/octet-stream");
//header("Content-Type: application/download");
//this line is important its makes the file name
//header("Content-Disposition: attachment;filename=export_".$dbtable.".xls ");
//header("Content-Transfer-Encoding: binary ");
// start the file
xlsBOF();
// these will be used for keeping things in order.
$col = 0;
$row = 0;
// This tells us that we are on the first row
$first = true;
while( $qrow = mysql_fetch_assoc( $qr ) )
{
// Ok we are on the first row
// lets make some headers of sorts
if( $first )
{
foreach( $qrow as $k => $v )
{
// take the key and make label
// make it uppper case and replace _ with ' '
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
// prepare for the first real data row
$col = 0;
$row++;
$first = false;
}
// go through the data
foreach( $qrow as $k => $v )
{
// write it out
xlsWriteLabel( $row, $col, $v );
$col++;
}
// reset col and goto next row
$col = 0;
$row++;
}
xlsEOF();
exit();
?>
我甚至不知道这些是否有意义,但我在xlsBOF、xlsEOF和xlsWriteLabel函数中添加了fwrite,以尝试将数据写入导出的.xls文件,它可以这样工作吗?我在项目中经常使用PEAR,而且效果很好。不过,它会生成Excel 5.0级别的文件,因此,如果您需要更高级的文件,它可能不足以满足您的需要,但它会生成一个本机.xls,而不仅仅是一个伪装为.xls的.csv文件。一切正常,答案就在这里。:-)
它需要自动化,脚本将每天在指定的时间通过cron作业运行,我只需要弄清楚如何让脚本自动将生成的xls数据保存到文件中。在cron中,您还可以运行wget将php脚本的输出保存到某个地方。我将尝试对Content-disposition属性进行一些研究,看看它是否也可以使用cron。有什么方法可以用fopen,fwrite fclose的方式来做吗?你可以试着打开一个XLS文件,写一个逗号分隔的值或者一个html表格,保存它,然后在Excel中打开它。看看会发生什么。这就是我现在试图做的,但我会试试看,看看会发生什么,这会让你们保持联系。:-)我用ob_get_clean()尝试了它,但它不起作用,下面是我现在正在编写的代码:fopen&co.不应该在xlsEOF中吗?xlsWriteLabel是为每个单元格调用的。我试试看,thanx。我会向你通报进展情况。
<?php
// DB TABLE Exporter
//
// How to use:
//
// Place this file in a safe place, edit the info just below here
// browse to the file, enjoy!
// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO
$dbhost = "-";
$dbuser = "-";
$dbpass = "-";
$dbname = "-";
$dbtable = "-";
// END CHANGING STUFF
$cdate = date("Y-m-d"); // get current date
// first thing that we are going to do is make some functions for writing out
// and excel file. These functions do some hex writing and to be honest I got
// them from some where else but hey it works so I am not going to question it
// just reuse
// This one makes the beginning of the xls file
function xlsBOF() {
$output = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
// This one makes the end of the xls file
function xlsEOF() {
$output .= pack("ss", 0x0A, 0x00);
return;
}
// this will write text in the cell you specify
function xlsWriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
$output .= pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
$file = fopen("exported.xls","w");
fwrite($file, "$output");
fclose($file);
return;
}
// make the connection an DB query
$dbc = mysql_connect( $dbhost , $dbuser , $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname );
$q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'";
$qr = mysql_query( $q ) or die( mysql_error() );
// Ok now we are going to send some headers so that this
// thing that we are going make comes out of browser
// as an xls file.
//
//header("Pragma: public");
//header("Expires: 0");
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
//header("Content-Type: application/force-download");
//header("Content-Type: application/octet-stream");
//header("Content-Type: application/download");
//this line is important its makes the file name
//header("Content-Disposition: attachment;filename=export_".$dbtable.".xls ");
//header("Content-Transfer-Encoding: binary ");
// start the file
xlsBOF();
// these will be used for keeping things in order.
$col = 0;
$row = 0;
// This tells us that we are on the first row
$first = true;
while( $qrow = mysql_fetch_assoc( $qr ) )
{
// Ok we are on the first row
// lets make some headers of sorts
if( $first )
{
foreach( $qrow as $k => $v )
{
// take the key and make label
// make it uppper case and replace _ with ' '
xlsWriteLabel( $row, $col, strtoupper( ereg_replace( "_" , " " , $k ) ) );
$col++;
}
// prepare for the first real data row
$col = 0;
$row++;
$first = false;
}
// go through the data
foreach( $qrow as $k => $v )
{
// write it out
xlsWriteLabel( $row, $col, $v );
$col++;
}
// reset col and goto next row
$col = 0;
$row++;
}
xlsEOF();
exit();
?>