用fwrite保存.xls文件 [英] Saving a .xls file with fwrite

查看:92
本文介绍了用fwrite保存.xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have to create a script that takes a mySQL table, and exports it into .XLS format, and then saves that file into a specified folder on the web host.

我可以使它正常工作,但是现在看来我无法在不提示用户的情况下自动将文件保存到该位置.

I got it working, but now I can't seem to get it to automatically save the file to the location without prompting the user.

它需要每天在指定时间运行,因此可以将前几天的销售线索保存到Web主机上的.XLS文件中.

It needs to run every day at a specified time, so it can save the previous days leads into a .XLS file on the web host.

这是代码:

<?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来完成此操作,但它似乎进行得并不顺利,我也删除了标头信息,但没有任何效果.

I tried using, fwrite to accomplish this, but it didn't seem to go very well, I removed the header information too, but nothing worked.

这是我发现的原始代码,任何帮助将不胜感激. :-)

Here is the original code, as I found it, any help would be greatly appreciated. :-)

先谢谢您. :-)

推荐答案

首先,由于您是通过cron将其保存到磁盘的,因此您应该删除所有怀疑的header()调用.为了尽可能少地重写代码,我建议您使用输出缓冲( http://www.php.net/manual/zh/ref.outcontrol.php ).为此,请在文件输出开始之前对ob_start()进行调用:

First, since you are saving this to disk via cron you should remove all of the header() calls as you suspected. In order to rewrite as little of your code as possible, I would recommend using output buffering (http://www.php.net/manual/en/ref.outcontrol.php). To accomplish this, place a call to ob_start() before your file output begins:

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

在输出结束后,关闭输出缓冲区,捕获其内容并将其写入文件:

And after your output ends, close the output buffer, capture its contents and write them to a file:

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

这篇关于用fwrite保存.xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆