search
Homephp教程php手册php动态导出Excel大文件

php动态导出Excel大文件

Jun 06, 2016 pm 07:32 PM
excelphpprimarydynamicExportdocument

初级excel文件下载方式。 1.解决excel行数过多下载失败问题。 2.解决中文文件名在ie下下载乱码问题。 无 ?php/** * 生成excel文件操作 * * @author wesley wu * @date 2013.12.9 */class Excel{private $limit = 10000;public function download($fileName, $

初级excel文件下载方式。
1.解决excel行数过多下载失败问题。
2.解决中文文件名在ie下下载乱码问题。
<?php
/**
 * 生成excel文件操作
 *
 * @author wesley wu
 * @date 2013.12.9
 */
class Excel
{
	
	private $limit = 10000;
	
	public function download($fileName, $data)
	{
		$fileName = $this->_charset($fileName);
		header("Content-Type: application/vnd.ms-excel; charset=gbk");
		header("Content-Disposition: inline; filename=\"" . $fileName . ".xls\"");
		echo "<?xml version=\"1.0\" encoding=\"gbk\"?>\n
			<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"
			xmlns:x=\"urn:schemas-microsoft-com:office:excel\"
			xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\"
			xmlns:html=\"http://www.w3.org/TR/REC-html40\">";
		echo "\n<Worksheet ss:Name=\"" . $fileName . "\">\n<Table>\n";
		$guard = 0;
		foreach($data as $v)
		{
			$guard++;
			if($guard==$this->limit)
			{
				ob_flush();
				flush();
				$guard = 0;
			}
			echo $this->_addRow($this->_charset($v));
		}
		echo "</Table>\n</Worksheet>\n</Workbook>";
	}
	
	private function _addRow($row)
	{
		$cells = "";
		foreach ($row as $k => $v)
		{
			$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n"; 
		}
        return "<Row>\n" . $cells . "</Row>\n";
	}
	
	private function _charset($data)
	{
		if(!$data)
		{
			return false;
		}
		if(is_array($data))
		{
			foreach($data as $k=>$v)
			{
				$data[$k] = $this->_charset($v);
			}
			return $data;
		}
		return iconv('utf-8', 'gbk', $data);
	}
	
}

$excel = new Excel();
$data = array(
	array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
	array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
	array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
	array('数据1','数据2','数据3','数据4','数据5','数据6','数据7'),
	array('数据1','数据2','数据3','数据4','数据5','数据6','数据7')
);
$excel->download('这是一个测试', $data);
?>
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

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.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools