用php導入導出csv文件
最近在做一些模組的時候,頻繁導入導出csv文件,與excel相比他的處理更簡單,文件體積更小,數據處理量大,而且很多語言都支援。
廢話不多,上代碼~
導入代碼:
<code><span><<span>meta</span><span>charset</span> = "<span>utf-8</span>"></span><span><<span>form</span><span>method</span> = "<span>post</span>" <span>enctype</span> = "<span>multipart</span>/<span>form-data</span>" ></span><span><<span>h3</span>></span><strong>上传文件</strong><span></<span>h3</span>></span><span><<span>input</span><span>type</span> = "<span>file</span>" <span>name</span> = "<span>file</span>"></span><span><<span>button</span><span>type</span> = "<span>submit</span>" ></span>提交<span></<span>button</span>></span><span></<span>form</span>></span><span><span><?php</span><span>$csv</span> = <span>$_FILES</span>[<span>'file'</span>]; <span>$suffix</span> = array_pop(explode(<span>'.'</span>, <span>$csv</span>[<span>'name'</span>])); <span>//检查是否是csv文件</span><span>if</span>(<span>$suffix</span> <> <span>'csv'</span>) <span>exit</span>(<span>'请上传csv文件'</span>); <span>$file</span> = fopen(<span>$csv</span>[<span>'tmp_name'</span>], <span>'r'</span>); <span>$lines</span> = <span>array</span>(); <span>while</span>(<span>$data</span> = fgetcsv(<span>$file</span>)) <span>$lines</span>[] = <span>$data</span>; <span>echo</span><span>'<pre class="brush:php;toolbar:false">'</span>; print_r(<span>$lines</span>); <span>?></span></span></span></code>
導出代碼:
<code><span><<span>meta</span><span>charset</span> = "<span>utf-8</span>" /></span><span><<span>a</span><span>href</span> = "?<span>test</span>"></span>点我下载CSV文件<span></<span>a</span>></span><span><span><?php</span><span>if</span>(<span>isset</span>(<span>$_REQUEST</span>[<span>'test'</span>])){ ob_end_clean(); ob_start(); <span>//第一行题头</span><span>echo</span><span>"题头1,题头2,题头3,题头4\n"</span>; <span>//第一行数值</span><span>echo</span><span>"数值1,数值2,数值3,数值4\n"</span>; <span>$content</span> = ob_get_contents(); ob_end_clean(); header(<span>'Content-Type: text/html;charset=gb2312'</span>); header(<span>'Content-Type: application/force-download'</span>); header(<span>'Content-Disposition: attachment; filename=test.csv'</span>); <span>echo</span> iconv(<span>'utf-8'</span>, <span>'gb2312'</span>, <span>$content</span>); } <span>?></span></span></code>
測試鏈接
上傳功能測試鏈接
下載功能測試鏈接到留言
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i
').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了用php導入導出csv文件,包括了上傳文件,導入導出方面的內容,希望對PHP教程有興趣的朋友有所幫助。