3

mysqlデータベースにクエリを実行し、テーブルにデータを表示しようとしています。その部分は機能しています。現時点では、特定の日付範囲内の結果を表示するように設定されています。

ここで、テーブルを取得して、Excelファイルにエクスポートできるボタンを作成します。日付範囲を選択するオプションを追加する前は、Excelにエクスポートできましたが、2番目のファイルは私が話しているテーブルを認識していないようです。POSTを使用してデータの値を送信し、別のページで再クエリしてみました。

ボタンをクリックしてエクスポートすると、ダウンロードされたExcelドキュメントは空になります(サイズはありますが)。何か助けてください?

-----クエリmysql---------

 <html><head><title>New Production Rejections</title></head></html>
    <?php 

    include("config.php");
    //get serial from submitted data
        //$serial = $_POST['sNumber'];
        //if the submitted data is empty
        $serial = $_POST['entryDate'];
    $dateEnd = $_POST['entryDate2'];
        //parse the serial from the link in tracker
    ?>
        <form method="post" action="<?php echo "queryNewProdRejections.php?"?>">
        Search between dates: (Format: YYYY-MM-DD)<input type='text' size='20' maxlength='20' name='entryDate'> - <input type='text' size='20' maxlength='20' name='entryDate2'>
        <input type="submit" value="Search Date Range"><br/></form>

    <?php


    //query based on approved date that is nothing, repaired date that is nothing, 
    //tech is a real tech, location that is not Revite (RVP), action was to replace, 
    //and the status is not (declined or skipped).

    $query = "SELECT *
    FROM `rma`
    WHERE `origin` NOT LIKE 'Field_failure'
    AND `origin` NOT LIKE 'DOA_at_Customer'
    AND `origin` NOT LIKE 'Sweden_Fail_VI'
    AND `entry` > '$serial' AND `entry` < '$dateEnd'";
    $data = mysql_query($query) or die(mysql_error());

    //Create a table with the array of data from repairs, based on the previous query

    echo "<table border='1'><tr><th>RMA</th><th>Product</th><th>Serial</th><th>Origin</th><th>Return To</th><th>Credit Num</th><th>Order</th><th>Entry Date</th><th>Tech</th><th>Traking Num</th></tr>";

    while($row = mysql_fetch_array($data)){ 
        print "<tr><td>".$row['intrma']."</td><td>".$row['product']."</td><td>".$row['serial']."</td><td>".$row['origin']."</td><td>".$row['retto']."</td><td>".$row['creditnum']."</td><td>".$row['ordernum']."</td><td>".$row['entry']."</td><td>".$row['tech']."</td><td>".$row['tracknum']."</td></tr>";
    }
    print "</table>";

    ?>
    <html>
    <form method="post" action="saveQueryToExcel.php">
    <input type='hidden' name='ent_1' value="<?php echo $_POST['entryDate']; ?>">
    <input type='hidden' name='ent_2' value="<?php echo $_POST['entryDate2']; ?>">
      <input type="submit" value="Save to Excel">
    </form>
    </html>

--------------- Excelファイルに印刷-(saveQueryToExcel.php)

<html><head><title>New Production Rejections</title></head></html>

<?php
error_reporting(0);

$dateBeg=$_POST['ent_1'];
$dateEnd=$_POST['ent_2'];

//Connect to the database, repairs in maprdweb
include("config.php");

//query based on approved date that is nothing, repaired date that is nothing, 
//tech is a real tech, location that is not Revite (RVP), action was to replace, 
//and the status is not (declined or skipped).

$query = "SELECT *
FROM `rma`
WHERE `origin` NOT LIKE 'Field_failure'
AND `origin` NOT LIKE 'DOA_at_Customer'
AND `origin` NOT LIKE 'Sweden_Fail_VI'
AND `entry` > '$dateBeg' AND `entry` < '$dateEnd'";
$data = mysql_query($query) or die(mysql_error());

//Create a table with the array of data from repairs, based on the previous query
header('Content-type: application/vnd.ms-excel');

echo "<table border='1'><tr><th>RMA</th><th>Product</th><th>Serial</th><th>Origin</th><th>Return To</th><th>Credit Num</th><th>Order</th><th>Entry Date</th><th>Tech</th><th>Traking Num</th></tr>";

while($row = mysql_fetch_array($data)){ 
    print "<tr><td>".$row['intrma']."</td><td>".$row['product']."</td><td>".$row['serial']."</td><td>".$row['origin']."</td><td>".$row['retto']."</td><td>".$row['creditnum']."</td><td>".$row['ordernum']."</td><td>".$row['entry']."</td><td>".$row['tech']."</td><td>".$row['tracknum']."</td></tr>";
}
print "</table>";
?>
4

3 に答える 3

3

PHPexcelは、実際のExcelドキュメントにデータをエクスポートするのに最適です。

結果を使用してHTMLテーブルを生成しているようです。これは正確にはExcel形式ではありません。

于 2013-03-01T22:14:41.107 に答える
1

同じコードを使用して、行を印刷する代わりに、fputcsv<tr>に送信する必要があります。標準出力をファイルハンドルとして使用します。この投稿の行に沿って、出力をcsvとして送信するための適切なhtmlヘッダーを設定する必要があります:forcefiledownload

于 2013-03-01T22:12:09.717 に答える
1

このクラスを見てください。それは問題を解決することができます。

https://github.com/GSTVAC/HtmlExcel

$xls = new HtmlExcel();
$xls->addSheet("Names", $names);
$xls->headers();
echo $xls->buildFile();
于 2016-09-29T19:16:31.917 に答える