将数组转换为CSV文件输出

时间:2014-04-12 15:45:40

标签: php csv

我想从我的PHP脚本中生成的数据表中下载CSV文件。当我运行脚本时,系统会提示我下载,但是当我打开它时,它似乎只是将所有代码(包括标签等)转储到CSV文件中。我需要的只是表格中包含的结果。我一直在撕扯我的头发!有任何想法吗?继承我脚本的PHP部分:

<?php
session_start();
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename='. 'file.csv');
header('Pragma: no-cache');
header("Expires: 0");

$serverName = "localhost\SQLEXPRESS";
$connectionInfo = array( "Database"=>"AuServer", "ReturnDatesAsStrings"=>"true");
$conn = sqlsrv_connect( $serverName, $connectionInfo);

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,                                        LogEventDescription.description
        FROM AuEvent
        INNER JOIN LogEventDescription
        ON AuEvent.event_id=LogEventDescription.event_id
        INNER JOIN AusDatabase
        ON AuEvent.db_id=AusDatabase.ID
        WHERE LogEventDescription.lcid='1033'
        ORDER BY time_stamp DESC";

    $stmt = sqlsrv_query( $conn, $sql );
    if( $stmt === false) {
        die( print_r( sqlsrv_errors(), true) );
}

//sort results into an array for the table
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC) ){
    echo "
      <tr>
        <td>". $row['time_stamp'] . "</td>
        <td><a href=\"dbfilter.php?id=". $row['Title'] . "\">". $row['Title'] . "</a></td>
        <td><a href=\"userfilter.php?id=". $row['user_name'] . "\">". $row['user_name'] . "</a></td>
        <td><a href=\"eventfilter.php?id=". $row['description'] . "\">". $row['description'] . "</a></td>
        <td>". $row['rec_id'] . "</td>
      </tr>";



$fp = fopen('file.csv', 'w');

foreach ($row as $fields) {
fputcsv($fp, $row);
}

}
?>

1 个答案:

答案 0 :(得分:0)

如果要创建可下载文件,请停止回显表数据

<?php

session_start();
$serverName = "localhost\SQLEXPRESS";
$connectionInfo = array("Database" => "AuServer", "ReturnDatesAsStrings" => "true");
$conn = sqlsrv_connect($serverName, $connectionInfo);

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,                                        LogEventDescription.description
        FROM AuEvent
        INNER JOIN LogEventDescription
        ON AuEvent.event_id=LogEventDescription.event_id
        INNER JOIN AusDatabase
        ON AuEvent.db_id=AusDatabase.ID
        WHERE LogEventDescription.lcid='1033'
        ORDER BY time_stamp DESC";

$stmt = sqlsrv_query($conn, $sql);
if ($stmt === false) {
    die(print_r(sqlsrv_errors(), true));
}

$fp = fopen('file.csv', 'w');
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    foreach ($row as $fields) {
        fputcsv($fp, $row);
    }
}

$path = path/to/file;
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: '.filesize($file));
readfile($file);

?>

如果你想要打印和下载文件,那么

<?php

session_start();
$serverName = "localhost\SQLEXPRESS";
$connectionInfo = array("Database" => "AuServer", "ReturnDatesAsStrings" => "true");
$conn = sqlsrv_connect($serverName, $connectionInfo);

$sql = "select top 100 AuEvent.time_stamp, AusDatabase.Title, AuEvent.user_name, AuEvent.rec_id,                                        LogEventDescription.description
        FROM AuEvent
        INNER JOIN LogEventDescription
        ON AuEvent.event_id=LogEventDescription.event_id
        INNER JOIN AusDatabase
        ON AuEvent.db_id=AusDatabase.ID
        WHERE LogEventDescription.lcid='1033'
        ORDER BY time_stamp DESC";

$stmt = sqlsrv_query($conn, $sql);
if ($stmt === false) {
    die(print_r(sqlsrv_errors(), true));
}

$fp = fopen('file.csv', 'w');
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    foreach ($row as $fields) {
        fputcsv($fp, $row);
    }
}

$path = path/to/file;
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: '.filesize($file));
readfile($file);

ob_end_clean();
ob_start();

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
    echo "echo all data here, tr and td etc";
}
ob_flush();
?>