使用PHP将MySQL转换为XLS? [英] mySQL to XLS using PHP?

查看:99
本文介绍了使用PHP将MySQL转换为XLS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用PHP从mySQL表中创建.XLS文档?

how can I create a .XLS document from a mySQL table using PHP?

我几乎尝试了所有事情,但没有成功.

I have tried just about everything, with no success.

基本上,我需要获取表单数据,并将其输入到数据库中(已完成),然后需要检索该表数据并将其解析为microsoft Excel文件,该文件需要自动保存到网络服务器.

Basically, I need to take form data, and input it into a database, which I have done, and then I need to retrieve that table data and parse it into a microsoft excel file, which needs to be saved automatically onto the web server.

    <?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文件,该怎么办?

I just can't seem to figure out how to integrate fwrite into all that to write the generated data into a .xls file, how would I go about doing that?

我需要非常紧急地进行这项工作,因此我们将不胜感激. 谢谢你们.

I need to get this working quite urgently, so any help would be greatly appreciated. Thanx guys.

推荐答案

一切正常,这里是答案所在. :-)

Everything is working, here is where the answer is located. :-)

使用fwrite保存.xls文件

这篇关于使用PHP将MySQL转换为XLS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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