2010-03-22 71 views
2

如何使用PHP从mySQL表创建一个.XLS文档?mySQL to XLS using PHP?

我已经尝试了几乎所有的东西,没有成功。

基本上,我需要采取表单数据,并将其输入到我已经完成的数据库中,然后我需要检索该表数据并将其解析为Microsoft Excel文件,该文件需要自动保存到Web服务器。

<?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家伙。

回答

1

如果您的数据库有某种类型的前端(如phpMyAdmin或SQLyog),您可以将表格(或任何SELECT查询的结果)导出为CSV并在Excel中打开该表格。

评论后编辑: 我创建了一次XLS。这是一个有点不同,但我所做的就是把这个在我的PHP的顶部(产生任何输出前):

header("Content-type: application/vnd.ms-excel"); 
header("Content-disposition: attachment; filename=\"name.xls\""); 

而在脚本的其余部分我只是呼应了一个表(表, tr,td ...等) 该脚本的执行会给用户下载。我认为Content-disposition属性有一些不同的选项(也许有一个让脚本保存文件的选项)。

+0

它需要实现自动化,该脚本将通过cron作业每天在指定时间运行,我只需要弄清楚如何让脚本保存生成的XLS数据到自动文件。 – Odyss3us 2010-03-22 09:02:42

+0

好的..在这种情况下,我的解决方案并不是那么好..让我编辑它.. – 2010-03-22 09:05:15

+0

在cron中,你也可以运行wget来保存php脚本的输出。 – Krab 2010-03-22 09:13:06

0

如果我理解正确,您发布的脚本工作正常,并创建一个正确的xls文件,并且您只想保存输出,请查看http://php.net/manual/en/book.outcontrol.php。使用ob_get_clean(),您可以获取创建的xls文件并将其写入服务器的某个位置。也许你可以考虑其他选项,例如将数据保存为Excel可以读取的其他格式(.csv,可能它也可以读取一些html/xml表格)。

+0

我用ob_get_clean()试过了,但没有奏效,下面是我现在正在处理的代码: – Odyss3us 2010-03-22 12:32:46

0

我尝试了与ob_get_clean(),但它没有工作,这里是我一直在努力代码:

<?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(); 



?> 

我甚至不知道是否有任何这是有道理的,但我将fwrite添加到xlsBOF,xlsEOF和xlsWriteLabel函数中,试图将数据写入exported.xls文件,它可以像那样工作吗?

+0

不应该打开&co。在xlsEOF?每个单元格都调用xlsWriteLabel。 – Krab 2010-03-23 07:26:59

+0

我会放弃它,thanx。我会保持你的进展。 – Odyss3us 2010-03-23 19:49:09

1

我在项目中使用了很多PEAR Spreadsheet_Excel_Writer,效果很好。它生成Excel 5.0级别的文件,所以如果你需要比这更高级的anyhing,它可能不足以满足你的目的,但它会生成本地.xls,而不仅仅是.csv伪装成.xls。