Home > Backend Development > PHP Tutorial > Import and export csv files using php

Import and export csv files using php

WBOY
Release: 2016-07-29 09:12:28
Original
1072 people have browsed it

Use php to import and export csv files

When I was working on some modules recently, I frequently import and exportcsv files. Compared with excel, its processing is simpler, the file size is smaller, and the data processing volume is large, and there are a lot of them. All languages ​​are supported.

Not much nonsense, let’s get to the code~

Import code:

<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>
Copy after login

Export 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>
Copy after login

Test link

Upload function test link

Download function test link

Above~ If you don’t understand, you can leave a message

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the use of PHP to import and export csv files, including uploading files, importing and exporting. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template