2010-03-19 112 views
1

我必须创建一个脚本,该脚本需要一个mySQL表,并将其导出为.XLS格式,然后将该文件保存到Web主机上的指定文件夹中。使用fwrite保存.xls文件

我懂了工作,但现在我似乎无法得到它的自动保存文件的位置,而不会提示用户。

它需要在指定时间运行的每一天,所以它可以保存前几天通向虚拟主机上的.xls文件。

下面是代码:

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


// 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来做到这一点,但它似乎没有去很好,我删除了标头信息太多,但毫无效果。

这是原始代码,因为我发现它,任何帮助将不胜感激。 :-)

Thanx提前。 :-)

+0

你有头要发送到浏览器,但你谈论的文件保存到Web主机的文件系统。你能解释更多关于你试图做什么以及哪一个是你有问题/意外结果? – 2010-03-19 14:37:51

+0

我想将它保存到网络主机,我根本不需要头文件,因为它将在特定的时间通过cron任务每天执行。 – Odyss3us 2010-03-20 06:17:59

回答

3

首先,由于您通过cron将其保存到磁盘,因此应该删除所有的头部()调用。为了尽可能少地重写代码,我建议使用输出缓冲(http://www.php.net/manual/en/ref.outcontrol.php)。要做到这一点,将调用ob_start()文件输出开始之前:

ob_start(); 
// start the file 
xlsBOF(); 

而且你的输出结束后,关闭输出缓冲器,捕捉它的内容,并将其写入文件:

xlsEOF(); 
// $filename should be set to some writeable location 
file_put_contents($filename, ob_get_clean()); 
+0

这似乎已经做到了,比一百万! – Odyss3us 2010-03-25 11:13:30

1

它是xls还是xsl文件格式? 让我困惑。

  • 我想这是XLS:

第一:你需要设置字体样式,使用多个标签,使用公式? 如果是这样,尝试去一个excel库,如phpwriteexcel。

否则,一个简单的CSV文件足够(逗号分隔值,从阵列创建很容易,使用Excel和其他电子表格软件完全读取)。

然后,在不提示的情况下自动保存:进行规划化任务/ cron任务,调用脚本。

+0

这是Microsoft Excel xls文件,对不起。 – Odyss3us 2010-03-19 14:28:48

+0

不,我基本上输出它的方式,我希望它,但我不会在那里每次它通过cron任务运行下载文件,所以我需要它将文件保存到脚本是目录在,我假设我可以使用fwrite呢?但是,我会如何将其整合到此代码中? 我试过一个csv文件,它工作得很好,但不幸的是它需要以xls格式。 Thanx再次寻求帮助。 – Odyss3us 2010-03-20 06:22:19

3

这是最终的代码,它的作用就像一个魅力。

<?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 
     $cdate = date("Y-m-d"); 
     $dbhost = "-"; 
     $dbuser = "-"; 
     $dbpass = "-"; 
     $dbname = "-"; 
     $dbtable = "-"; 
     $filename = "exported_on_$cdate.xls"; 

    // END CHANGING STUFF 


    // 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 object 
    ob_start(); 

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

    //write the contents of the object to a file 
    file_put_contents($filename, ob_get_clean()); 

    ?> 

Thanx for all the help guys !!!

+0

是否有任何方法可以在超出特定行数后添加新的工作表 – Jeeva 2013-02-17 10:22:43

相关问题