`

超简单的XmlExcelExport-生成Excel

 
阅读更多

<?php
// 数据导出 类文件 -- 作者 色色

/**
 * 导出 XML格式的 Excel 数据
 */
class XmlExcelExport
{

	/**
	 * 文档头标签
	 * 
	 * @var string
	 */
	private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\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\">";

	/**
	 * 文档尾标签
	 * 
	 * @var string
	 */
	private $footer = "</Workbook>";

	/**
	 * 内容编码
	 * @var string
	 */
	private $sEncoding;

	/**
	 * 是否转换特定字段值的类型
	 * 
	 * @var boolean
	 */
	private $bConvertTypes;
	
	/**
	 * 生成的Excel内工作簿的个数
	 * 
	 * @var int
	 */
	private $dWorksheetCount = 0;

	/**
	 * 构造函数
	 * 
	 * 使用类型转换时要确保:页码和邮编号以'0'开头
	 *
	 * @param string $sEncoding 内容编码
	 * @param boolean $bConvertTypes 是否转换特定字段值的类型
	 */
	function __construct($sEncoding = 'UTF-8', $bConvertTypes = false)
	{
		$this->bConvertTypes = $bConvertTypes;
		$this->sEncoding = $sEncoding;
	}

	/**
	 * 返回工作簿标题,最大 字符数为 31
	 *
	 * @param string $title 工作簿标题
	 * @return string
	 */
	function getWorksheetTitle($title = 'Table1')
	{
		$title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", empty($title) ? 'Table' . ($this->dWorksheetCount + 1) : $title);
		return substr($title, 0, 31);
	}
	
	/**
	 * 向客户端发送Excel头信息
	 *
	 * @param string $filename 文件名称,不能是中文
	 */
	function generateXMLHeader($filename){
		
		$filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
		$filename = urlencode($filename);
		
		// 中文名称使用urlencode编码后在IE中打开能保存成中文名称的文件,但是在FF上却是乱码
		header("Pragma: public");   header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: application/force-download");
		header("Content-Type: application/vnd.ms-excel; charset={$this->sEncoding}");
		header("Content-Transfer-Encoding: binary");
		header("Content-Disposition: attachment; filename={$filename}.xls");
		
		echo stripslashes(sprintf($this->header, $this->sEncoding));
	}
	
	/**
	 * 向客户端发送Excel结束标签
	 *
	 * @param string $filename 文件名称,不能是中文
	 */
	function generateXMLFoot(){
		echo $this->footer;
	}
	
	/**
	 * 开启工作簿
	 * 
	 * @param string $title
	 */
	function worksheetStart($title){
		$this->dWorksheetCount ++;
		echo "\n<Worksheet ss:Name=\"" . $this->getWorksheetTitle($title) . "\">\n<Table>\n";
	}
	
	/**
	 * 结束工作簿
	 */
	function worksheetEnd(){
		echo "</Table>\n</Worksheet>\n";
	}
	
	/**
	 * 设置表头信息
	 * 
	 * @param array $header
	 */
	function setTableHeader(array $header){
		echo $this->_parseRow($header);
	}
	
	/**
	 * 设置表内行记录数据
	 * 
	 * @param array $rows 多行记录
	 */
	function setTableRows(array $rows){
		foreach ($rows as $row) echo $this->_parseRow($row);
	}
	
	/**
	 * 将传人的单行记录数组转换成 xml 标签形式
	 *
	 * @param array $array 单行记录数组
	 */
	private function _parseRow(array $row)
	{
		$cells = "";
		foreach ($row as $k => $v){
			$type = 'String';
			if ($this->bConvertTypes === true && is_numeric($v))
				$type = 'Number';
				
			$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
			$cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";
		}
		return "<Row>\n" . $cells . "</Row>\n";
	}

}
 

 

 

经过测试在Excel2007 ,  单工作簿支持 10w条 以上的数据....

 

对于大数据量的导出,一次取出全部数据不太现实,容易内存溢出,建议分段提取,每次 提取 200 条数据

 

 

此类配合 我之前发的 coredb 类 性能还好 哈哈哈

 

  • 大小: 32.9 KB
分享到:
评论
2 楼 vb2005xu 2012-03-16  
http://msdn.microsoft.com/en-us/library/aa140066%28v=office.10%29.aspx#odc_xmlss_ss:row 微软XML excel 格式
1 楼 vb2005xu 2011-09-14  
米的qq601287035

相关推荐

Global site tag (gtag.js) - Google Analytics