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

		return iconv(self::$charset, 'GBK', $output);
	}

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

        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('') . "\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 = 'example';
$title = 'This is title';
$firstRow = array(
  'id' => 'ID',
  'name' => '名字',
  'title' => '标题'
  );
$data = array(
  array('id' => 1, 'name' => '名字1', 'title' => '标题1'),
  array('id' => 2, 'name' => '名字2', 'title' => '标题2'),
  array('id' => 3, 'name' => '名字3', 'title' => '标题3'),
  array('id' => 4, 'name' => '名字4', 'title' => '标题4'),
  );
Excel_tool::exportFile($fileName,$title,$firstRow,$data);
?>
登录后复制
09-18 11:31