search
HomeBackend DevelopmentPHP TutorialHow to export csv file in php (code example)

The content of this article is about how to export csv files (code examples) in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

/**  如果大量数据导出 支持分页写入
	 * [DownloadDate 公共导出csv]
	 * @param  string                   $name   [文件名称]
	 * @param  array                   $header [表头]
	 * @param  array                    $data   [数据集]
	 * @param                      $is_header   [真 假 是否取表头 解决循环写入问题]
	 * @return array             name[文件名称]   filePath[文件路径]                               
	 */
	public static function DownloadData($name='',$header=array(),$data=array(),$is_header=true){
      	set_time_limit(0);
      	ini_set('memory_limit','2048M'); 
      	header("Content-type:application/vnd.ms-excel");
      	header("content-Disposition:filename=downloaded.pdf ");
      	try {
      		if (!$name || !$data) {
      			throw new BadRequestHttpException('参数不可为空');
      		}
	      	$filePath = "./temp/download/{$name}.csv";
	      	$header = implode(",",$header);
	      	$header = iconv('UTF-8', 'GBK//IGNORE', $header);
	      	$header = explode(",", $header);
	      	$fp = fopen($filePath, 'a+');   
	      	if (!empty($header) && is_array($header) && $is_header) {
	      		fputcsv($fp, $header);
	      	}
	      	foreach ($data as $row) {
	      		$str = implode("@@@@",$row);
	      		$str = iconv('UTF-8', 'GBK//IGNORE', $str);
	      		$str = str_replace(",","|",$str);
	      		$row = explode("@@@@", $str);
	      		fputcsv($fp, $row);
	      	}
	      	unset($data);  
	      	if(ob_get_level()>0){
	      		ob_flush();
	      	}  
	      	flush(); 	
      	} catch (Exception $e) {
      		 throw new BadRequestHttpException($e->getMessage());//抛出异常
      	}
      	return [
      	'filePath'=>ltrim($filePath,"./"),
      	'name'=>$name.'.csv',
      	];
 	}

Related recommendations:

php export csv file: specify encoding export and csv file import and export class

php implements CSV file import and export,

The above is the detailed content of How to export csv file in php (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Java 中使用 OpenCSV 读取和写入 CSV 文件的示例Java 中使用 OpenCSV 读取和写入 CSV 文件的示例Dec 20, 2023 pm 01:39 PM

Java中使用OpenCSV读取和写入CSV文件的示例CSV(Comma-SeparatedValues)指的是以逗号分隔的数值,是一种常见的数据存储格式。在Java中,OpenCSV是一个常用的工具库,用于读取和写入CSV文件。本文将介绍如何使用OpenCSV来实现读取和写入CSV文件的示例。引入OpenCSV库首先,需要引入OpenCSV库到

CSV文件操作速成指南CSV文件操作速成指南Dec 26, 2023 pm 02:23 PM

快速学会打开和处理CSV格式文件的方法指南随着数据分析和处理的不断发展,CSV格式成为了广泛使用的文件格式之一。CSV文件是一种简单且易于阅读的文本文件,其以逗号分隔不同的数据字段。无论是在学术研究、商业分析还是数据处理方面,都经常会遇到需要打开和处理CSV文件的情况。下面的指南将向您介绍如何快速学会打开和处理CSV格式文件。步骤一:了解CSV文件格式首先,

Oracle导入中文数据时出现乱码怎么解决?Oracle导入中文数据时出现乱码怎么解决?Mar 10, 2024 am 09:54 AM

标题:解决Oracle导入中文数据乱码问题的方法及代码示例在Oracle数据库中导入中文数据时,经常会出现乱码的情况,这可能是由于数据库字符集设置不正确或者导入过程中出现编码转换问题所致。为了解决这个问题,我们可以采取一些方法来保证导入的中文数据能够正确显示。下面是一些解决方案及具体的代码示例:一、检查数据库字符集设置在Oracle数据库中,字符集的设置对于

csv文件怎么打开csv文件怎么打开Oct 27, 2023 am 11:07 AM

CSV文件可以使用文本编辑器、电子表格软件、编程语言或数据库工具等多种方式打开。详细介绍:1、文本编辑器,CSV文件可以使用任何文本编辑器打开,如记事本、TextEdit或Vim,通过双击CSV文件,系统会默认使用关联的文本编辑器打开;2、电子表格软件,CSV文件可以使用电子表格软件打开,如Microsoft Excel等,这些软件支持直接导入CSV文件,并将其解析为表格等等。

pandas怎么读取csv文件pandas怎么读取csv文件Dec 01, 2023 pm 04:18 PM

读取CSV文件的方法有使用read_csv()函数、指定分隔符、指定列名、跳过行、缺失值处理、自定义数据类型等。详细介绍:1、read_csv()函数是Pandas中最常用的读取CSV文件的方法。它可以从本地文件系统或远程URL加载CSV数据,并返回一个DataFrame对象;2、指定分隔符,默认情况下,read_csv()函数将使用逗号作为CSV文件的分隔符等等。

vcf文件用什么软件打开vcf文件用什么软件打开Feb 19, 2024 pm 01:34 PM

vcf文件是一种常用的电子名片文件格式,用于存储联系人信息,例如姓名、电话号码、电子邮件地址等。一般情况下,我们可以使用多种软件来打开vcf文件,包括以下几种常见的方法。通讯录应用程序:大多数智能手机都配备了通讯录应用程序,这些应用程序可以直接导入和打开vcf文件。用户只需要点击文件或使用导入功能,通讯录应用程序就会自动加载和显示vcf文件中的联系人信息。例

csv是什么样的格式文件csv是什么样的格式文件Jul 25, 2023 pm 04:33 PM

csv文件是一种常用的文件格式,用于存储和交换数据,通常以纯文本形式存在,可以使用任何文本编辑器进行打开和编辑,使用逗号作为字段分隔符,并且可以通过引号进行转义。由于其简单和灵活的结构,它被广泛用于不同的应用程序和领域,方便数据的导入、导出和处理。

csv格式文件是什么csv格式文件是什么Jul 25, 2023 pm 04:36 PM

CSV格式文件是一种常见的文本文件格式,其全称为Comma-Separated Values,中文名为逗号分隔值文件,其简单明了的结构和易读性使得它成为数据导入、备份和传输的理想选择,在使用CSV文件时需要注意对兼容性和数据类型的处理,以确保数据的准确和可靠。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools