用php
匯入匯出csv檔案
最近在做一些模組的時候,頻繁匯入匯出csv檔案,與excel相比他的處理更簡單,檔案體積更小,資料處理量大,而且很多語言都支援。
廢話不多,上代碼~
匯入代碼:
<metacharset = "utf-8"><formmethod = "post" enctype = "multipart/form-data" ><h3>上傳檔案h3><inputtype = "file" name = "file"><buttontype = "submit" >提交button>form>$csv = $_FILES['file']; $suffix = array_pop(explode('.', $csv['name'])); //檢查是否是csv檔案if($suffix <> 'csv') exit('請上傳csv檔案'); $file = fopen($csv['tmp_name'], 'r'); $lines = array(); while($data = fgetcsv($file)) $lines[] = $data; echo'匯出代碼:
<metacharset = "utf-8" /><ahref = "?test">點我下載CSV檔案a>if(isset($_REQUEST['test'])){ ob_end_clean(); ob_start(); //第一行題頭echo"題頭1,題頭2,題頭3,題頭4\n"; //第一行數值echo"數值1,數值2,數值3,數值4\n"; $content = ob_get_contents(); ob_end_clean(); header('Content-Type: text/html;charset=gb2312'); header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename=test.csv'); echo iconv('utf-8', 'gb2312', $content);}?>
測試連結
上傳功能測試連結
下載功能測試連結
以上~如果有不明白,可以留言
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了用php匯入匯出csv檔案,包括了上傳檔案,匯入匯出方面的內容,希望對PHP教程有興趣的朋友有所協助。
'; print_r($lines);?>