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>meta</span><span>charset</span> = "<span>utf-8</span>"><span>form</span><span>method</span> = "<span>post</span>" <span>enctype</span> = "<span>multipart</span>/<span>form-data</span>" ><span>h3</span>><strong>上传文件</strong><span><span>h3</span>></span><span>input</span><span>type</span> = "<span>file</span>" <span>name</span> = "<span>file</span>"><span>button</span><span>type</span> = "<span>submit</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">'; print_r($lines); ?>
Export code:
<code><span>meta</span><span>charset</span> = "<span>utf-8</span>" /><span>a</span><span>href</span> = "?<span>test</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></span></code>
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.

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

Laravel是业界比较出色的PHP框架之一,其强大的功能和易于使用的API使得其深受开发者的喜爱。在实际开发中,我们经常需要进行数据的导入和导出工作,而CSV作为一种广泛应用的数据格式,也成为了常用的导入和导出格式之一。本文就将介绍如何使用LaravelExcel扩展来进行CSV文件的导入和导出操作。一、安装LaravelExcel首先,我们需

php中json字符串转csv格式的方法:1、创建一个php示例文件;2、将JSON字符串转换为PHP数组或对象;3、创建一个文件句柄并打开一个CSV文件进行写入;4、在CSV文件中编写标题行和数据行;5、将数据行写入CSV文件,并在字段之间使用逗号分隔符,关闭文件句柄并完成转换即可。

这篇文章将为大家详细讲解有关PHP将行格式化为CSV并写入文件指针,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。将行格式化为CSV并写入文件指针步骤1:打开文件指针$file=fopen("path/to/file.csv","w");步骤2:将行转换为CSV字符串使用fputcsv()函数将行转换为CSV字符串。该函数接受以下参数:$file:文件指针$fields:作为数组的CSV字段$delimiter:字段分隔符(可选)$enclosure:字段引号(

Python中的XML数据转换为CSV格式XML(ExtensibleMarkupLanguage)是一种可扩展标记语言,常用于数据的存储和传输。而CSV(CommaSeparatedValues)则是一种以逗号分隔的文本文件格式,常用于数据的导入和导出。在处理数据时,有时需要将XML数据转换为CSV格式以便于分析和处理。Python作为一种功能强大

Java是一种广泛使用的编程语言,开发者们常常需要处理各种数据格式。CSV(Comma-SeparatedValues,逗号分隔值)是一种常见的数据格式,广泛应用于数据交换和存储。在Java中,我们可以使用OpenCSV库来读写CSV文件。OpenCSV是一个简单易用的开源库,提供了方便的API来处理CSV数据。本文将介绍如何在

php导入csv乱码问题的解决办法:1、构造一个解析函数“function tb_str_getcsv($string, $delimiter=',', $enclosure='"') {...}”;2、读取文件到变量;3、通过“substr($s,2)”去掉BOM头即可。

概述主要知识点a.SwingNode类:把Javaswing组件封装成一个JavaFX的Node,使得JavaSwing可以和JavaFX嵌套在一起使用,JavaSwing贼丑,但操作简单,JavaFX的表格组件(TableView等)有点复杂,所以选择嵌套JavaSwing来使用,丑就丑吧b.javacsv-2.0.jar:用于通过文件地址读取csv文件,并可以进行一系列操作.尽管2008年之后就不再更新,但操作个csv文件也够用了。c.FileChoose类:JavaFX的一个文件选择器,可


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
