簡體   English   中英

用fwrite保存.xls文件

[英]Saving a .xls file with fwrite

我必須創建一個使用mySQL表的腳本,並將其導出為.XLS格式,然后將該文件保存到Web主機上的指定文件夾中。

我可以使用它,但是現在似乎無法在不提示用戶的情況下自動將文件保存到該位置。

它需要每天在指定時間運行,因此可以將以前的銷售線索保存到Web主機上的.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來完成此操作,但它似乎進行得並不順利,我也刪除了標頭信息,但沒有任何效果。

這是我發現的原始代碼,任何幫助將不勝感激。 :-)

提前感謝。 :-)

首先,由於您是通過cron將其保存到磁盤上的,因此您應該刪除所有懷疑的header()調用。 為了盡可能少地重寫代碼,我建議使用輸出緩沖( 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());

這是最終代碼,它就像一個魅力。

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

    ?>

感謝所有幫助人員!!!

是xls還是xsl文件格式? 讓我感到困惑。

  • 我猜是XLS:

第一:您是否需要設置字體樣式,使用多個選項卡,使用公式? 如果是這樣,請嘗試使用phpwriteexcel等Excel庫。

否則,一個簡單的csv文件就足夠了(用逗號分隔的值,非常容易從數組創建的值,可以使用excel和其他電子表格軟件完美讀取)。

然后,要自動保存它而不提示:轉到計划任務/ cron任務,調用腳本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM