Home > php教程 > PHP源码 > excel导出

excel导出

PHP中文网
Release: 2016-05-23 16:40:59
Original
1096 people have browsed it

<?php
/**
 * @author Jceee
 */
class excel_tool
{
    public static $readerObj;
    public static $charset = &#39;utf-8&#39;;
 
	/**
	 * 输出切换编码
	 */
	public static function excelExportIconv($output){

		return iconv(self::$charset, &#39;GBK&#39;, $output);
	}

    /**
     * 导出文件
     * @param $fileName  string  
     * @param $title     string
     * @param $firstRow  array       
     *          如:array(&#39;name&#39;=>&#39;名字&#39;, &#39;title&#39; => &#39;标题&#39;) 键名与后面的数组$data的子元素键名关联
     * @param $data      array
     */
	public static function exportFile($fileName, $title = &#39;&#39;, $firstRow = array(), $data = array())
	{
		header(&#39;Content-Type: application/vnd.ms-execl&#39;);
		header(&#39;Content-Disposition: attachment; filename=&#39; . $fileName . &#39;.xls&#39;);
		header(&#39;Pragma: no-cache&#39;);
		header(&#39;Expires: 0&#39;);

        if (!empty($title)) {
			echo self::excelExportIconv($title) . "\t\n";
        }

        /**
         *  第一行与后面的数据以键名关联
         */
		if (!empty($firstRow) && is_array($firstRow)) {

			//输出第一行内容
			foreach ($firstRow as $first) {
				echo self::excelExportIconv($first) . "\t";
			}
			echo "\n";

			if (!empty($data) && is_array($data)) {
				foreach ($data as $item) {
					foreach ($firstRow as $_key => $_val) {
						if (isset($item[$_key])) {
							echo self::excelExportIconv($item[$_key]) . "\t";
						} else {
							echo self::excelExportIconv(&#39;&#39;) . "\t";
						}
					}
					echo "\n";
				}
			}
		} else {

			if (!empty($data) && is_array($data)) {
				foreach ($data as $item) {
					foreach ($item as $val) {
						echo self::excelExportIconv($val) . "\t";
					}
					echo "\n";
				}
				echo "\n";
			}
		}

	}
}


/**
 * example:
 */
$fileName = &#39;example&#39;;
$title = &#39;This is title&#39;;
$firstRow = array(
  &#39;id&#39; => &#39;ID&#39;,
  &#39;name&#39; => &#39;名字&#39;,
  &#39;title&#39; => &#39;标题&#39;
  );
$data = array(
  array(&#39;id&#39; => 1, &#39;name&#39; => &#39;名字1&#39;, &#39;title&#39; => &#39;标题1&#39;),
  array(&#39;id&#39; => 2, &#39;name&#39; => &#39;名字2&#39;, &#39;title&#39; => &#39;标题2&#39;),
  array(&#39;id&#39; => 3, &#39;name&#39; => &#39;名字3&#39;, &#39;title&#39; => &#39;标题3&#39;),
  array(&#39;id&#39; => 4, &#39;name&#39; => &#39;名字4&#39;, &#39;title&#39; => &#39;标题4&#39;),
  );
Excel_tool::exportFile($fileName,$title,$firstRow,$data);
?>
Copy after login

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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template