簡體   English   中英

導出CSV打印HTML

[英]Exporting CSV printing HTML

我正在嘗試使用PHP導出CSV。 但是,不是打印結果,而是在生成的CSV文件中打印Resource id #26 如果我從末尾刪除退出,則打印我的整個HTML頁面內容。 我的代碼是......

      if (isset($_REQUEST['download']) && ($_REQUEST['download'] == "yes")) {
            header('Content-Type: text/csv; charset=utf-8');
            header('Content-Disposition: attachment; filename=link_point_report.csv');
            $output = fopen('php://memory', 'w+');
            fputcsv($output, array('Member Id', 'Customer Name', 'Customer Email'));

            $customerListAll = $oAdmFun->linkpoint_check_customer('', '');       
             $oAdmFun->pr($customerListAll, true);
//                if (count($customerListAll) > 0) {
//                        for ($c = 0; $c < count($customerListAll); $c++) {
//                                fputcsv($output, array('Sankalp', 'Sankalp', 'Sankalp'));
//                        }
//                }

            ob_clean();
            flush();
            exit($output);
    }

那是因為$ output是一個資源,這是fopen()返回的。 您需要使用fread()來獲取其內容。

編輯:現在我理解你在問什么。 要輸出CSV的內容,您需要從資源獲取文本輸出:

rewind( $output );
$csv_output = stream_get_contents( $output );
fclose( $output );
die( $csv_output );

並且設置內容長度標頭也是一個好主意,以便客戶端知道會發生什么:

header('Content-Length: ' . strlen($csv_output) );

打開一大塊可寫內存:

$file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+'); // 128mb

寫入...然后用以下內容獲取內容:

rewind($file);
$output = stream_get_contents($file);
fclose($file);
function CSV_HtmlTable($csvFileName)
{
// better to open new webpage 
echo "<html>\n<body>\n\t<table style=''>\n\n";
$f = fopen($csvFileName, "r");
$trcount = 0; //start the row count as 0
while (($line = fgetcsv($f)) !== false) {
        $trclass = ''; if ($trcount%2==0) { $trclass=' class="dark"'; } //default to nothing, but if it's even apply a class
        echo "\t\t<tr".$trclass.">\n"; //same as before, but now this also has the variable $class to setup a class if needed
        $tdcount = 0; //reset to 0 for each inner loop
        foreach ($line as $cell) {
                $tdclass = ''; if ($tdcount%2==0) { $tdclass=' class="dark"'; } //default to nothing, but if it's even apply a class
                echo "\t\t\t<td ".$tdclass."style='padding:.4em;'>" . htmlspecialchars($cell) . "</td>"; //same as before, but now this also has the variable $class to setup a class if needed
                $tdcount++; //go up one each loop
        }
        echo "\r</tr>\n";
        $trcount++; //go up one each loop
}
fclose($f);
echo "\n\t</table>\n</body>\n</html>";


}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM