php csv操作

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   for   strong   檔案   資料   

csv的寫入資料: [php] view plaincopyprint?
  1. $data = array(
  2. array(‘qq號‘,‘登入時間‘,‘名稱‘),
  3. array(‘123456‘,‘2012-08-21 15:21:10‘.chr(1),‘我是來測試的‘),
  4. array(‘56788‘,‘2012-08-21 18:21:20 ‘.chr(1),‘test測試資料‘),
  5. array(‘321789‘,‘2012-08-21 11:21:25 ‘.chr(1),‘HELLO‘)
  6. );
  7. $filename = "./file/test.csv";
  8. if( !file_exists( $filename ) ){
  9. file_put_contents($filename, ‘‘);
  10. }
  11. $file = fopen($filename, ‘w‘);
  12. foreach ( $data as $val ){
  13. if( false === fputcsv($file, $val) ){
  14. die(‘寫入資料失敗‘);
  15. }
  16. }
  17. fclose($file);
$data = array(array(‘qq號‘,‘登入時間‘,‘名稱‘),array(‘123456‘,‘2012-08-21 15:21:10‘.chr(1),‘我是來測試的‘),array(‘56788‘,‘2012-08-21 18:21:20 ‘.chr(1),‘test測試資料‘),array(‘321789‘,‘2012-08-21 11:21:25 ‘.chr(1),‘HELLO‘));$filename = "./file/test.csv";if( !file_exists( $filename ) ){file_put_contents($filename, ‘‘);}$file = fopen($filename, ‘w‘);foreach ( $data as $val ){if( false === fputcsv($file, $val) ){die(‘寫入資料失敗‘);}}fclose($file);

在寫入到csv的時候我的日期格式出現了問題,只顯示格式為:2011/06/05 12:02。導致我的秒數不存在了,所以在時間的後面都要加上chr(1)來得到正確的格式

csv讀資料:

[php] view plaincopyprint?
  1. $file = fopen($filename, ‘w‘) or die(‘開啟檔案失敗‘);
  2. //讀資料
  3. $file = fopen($filename, ‘r‘);
  4. while ( $val = fgetcsv($file) ){
  5. print_r($val);
  6. }
  7. fclose($file);
$file = fopen($filename, ‘w‘) or die(‘開啟檔案失敗‘);//讀資料$file =  fopen($filename, ‘r‘);while ( $val = fgetcsv($file) ){print_r($val);}fclose($file);

csv的下載:

[php] view plaincopyprint?
  1. $data = array(
  2. array(‘qq號‘,‘登入時間‘,‘名稱‘),
  3. array(‘123456‘,‘2012-08-21 15:21:10‘.chr(1),‘我是來測試的‘),
  4. array(‘56788‘,‘2012-08-21 18:21:20 ‘.chr(1),‘test測試資料‘),
  5. array(‘321789‘,‘2012-08-21 11:21:25 ‘.chr(1),‘HELLO‘)
  6. );
  7. //下載功能
  8. $date = time();
  9. header("Content-Type: text/csv");
  10. header("Content-Disposition: attachment; filename=".$date.".csv");
  11. header(‘Cache-Control:must-revalidate,post-check=0,pre-check=0‘);
  12. header(‘Expires:0‘);
  13. header(‘Pragma:public‘);
  14. foreach ( $data as $val ){
  15. echo implode(",", $val)."\n";
  16. }  
  17. ----------------------------------------------------------------------------
  18. <?php
    $filename = "./creattest.csv";
    //讀資料
    $file = fopen($filename, ‘r‘);
    $prex = "linpre";
    while ( $val = fgetcsv($file) ){
    print_r($val);
    echo "<hr/>";
    $val_list[] = $val;
    }
    $val_list[0][0] = $prex.$val_list[0][0];
    $val_list[1][0] = $prex.$val_list[1][0];
    echo $val_list[0][0]."------------";
    $file = fopen($filename, ‘w‘);
    foreach ( $val_list as $list ){
    if( false === fputcsv($file, $list) ){
    die(‘寫入資料失敗‘);
    }}
    fclose($file);

php csv操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.