2

PHPを使用してmySQLテーブルから.XLSドキュメントを作成するにはどうすればよいですか?

私はほとんどすべてを試しましたが、成功しませんでした。

基本的に、フォームデータを取得し、それをデータベースに入力する必要があります。これを実行してから、そのテーブルデータを取得し、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ファイルに書き込む方法がわからないようですが、どうすればよいでしょうか。

私はこれを非常に緊急に機能させる必要があるので、どんな助けでも大歓迎です。みんなありがとう。

4

5 に答える 5

1

データベースに何らかのフロントエンド(phpMyAdminやSQLyogなど)がある場合は、テーブル(またはSELECTクエリの結果)をCSVにエクスポートして、Excelで開くことができます。

コメント後に編集:XLSを一度作成しました。それは少し異なっていましたが、私がしたことはこれを私のPHPの一番上に置くことでした(出力が生成される前に):

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

そして、スクリプトの残りの部分では、テーブル(table、tr、td ...など)をエコーアウトしました。このスクリプトを実行すると、ユーザーにダウンロードが提供されます。Content-disposition属性にはいくつかの異なるオプションがあると思います(スクリプトにファイルを保存させるオプションがあるかもしれません)。

于 2010-03-22T08:58:00.383 に答える
1

私は自分のプロジェクトでPEARSpreadsheet_Excel_Writerを頻繁に使用しましが、うまく機能します。ただし、Excel 5.0レベルのファイルが生成されるため、それよりも高度なファイルが必要な場合は、目的には不十分な場合がありますが、.xlsを装った.csvだけでなく、ネイティブの.xlsも生成されます。

于 2010-03-22T15:08:09.480 に答える
0

正しく理解できれば、投稿したスクリプトは正しく機能し、正しいxlsファイルを作成します。出力を保存したい場合は、http://php.net/manual/en/book.outcontrol.phpを参照してください。ob_get_clean()を使用すると、作成されたxlsファイルを取得して、サーバーのどこかに書き込むことができます。

たぶん、Excelが読み取ることができる他の形式(.csv、おそらくhtml / xmlテーブルも読み取ることができる)にデータを保存するなど、他のオプションも検討できます。

于 2010-03-22T09:07:19.867 に答える
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();



?>

これが理にかなっているかどうかさえわかりませんが、xlsBOF、xlsEOF、xlsWriteLabel関数にfwriteを追加して、exported.xlsファイルにデータを書き込もうとしました。そのように機能しますか?

于 2010-03-22T12:40:01.027 に答える
0

すべてが機能しています。ここに答えがあります。:-)

fwriteで.xlsファイルを保存する

于 2010-03-25T11:22:14.390 に答える