php匯出CSV方法 (轉)

來源:互聯網
上載者:User
 

製作網站時,經常會遇到檢索資料列表的情況。通常使用者希望下載這些列表資料並儲存到用戶端。當然下載這些資料的時候是需要固定的格式,以便於用Excel等軟體閱覽。說的簡單一些就是CSV/Excel資料匯出 。

注意事項:

輸出文檔的基本格式為:列1,列2,列3,……,列nn

格式化資料的同時要對特殊的字元進行過濾。譬如“,”如果不轉換為全形“,”會導致資料格式的混亂。

實現方法:

舉例:檢索資料庫表,將結果儲存為字串,進行格式和特殊字元的過濾後,匯出到用戶端的CSV檔案。 

OrderSearchEdit_saveCSV.php



<?php
include($_SERVER[''DOCUMENT_ROOT''] . "/ftcart/OrderInfoManager.class.php");
include($_SERVER[''DOCUMENT_ROOT''] . "/ftcart/CommonUtil.php");
include($_SERVER[''DOCUMENT_ROOT''] . "/ftcart/CommonConst.php");
include($_SERVER[''DOCUMENT_ROOT''] . "/ftcart/CommonErrorMsg.php");
include($_SERVER[''DOCUMENT_ROOT''] . "/webadmin.php");

$searchcase = new OrderInfoManager();
$nowTime = microtime_float();
$searchcase = unserialize($_SESSION["ORDER_SEARCH_CASE"]);
$listcsv = $searchcase->doCSV();// 調用doCSV()方法
$filename = $nowTime;
//$filename = str_replace(":","",$nowTime);
//$filename = trim($filename);
//echo $filename;
header("Content-Disposition: attachment; filename=".$filename.".csv");
header(''Content-Type:APPLICATION/OCTET-STREAM'');
echo "注文No.,注文日,氏名,住所,支

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.