PHP
·
发表于 5年以前
·
阅读量:8393
×i½u¹«Ë¾ÒªÉu³É±¨±i£¬ÓÃPHPÉu³É¡£
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=test_data.xls");
ÎÒ°Ù¶ÈÁËÏ£¬Ã²ËÆÕa¸oºÜ¿iÄܹ»ÊµÏÖ£¬µ«ÊÇÕa¸oÎļþÈ´ÊÇÉu³ÉÔÚÔÚa¯ÀÀÆ÷ÏÂÔصĵØ*½£¬
ÎÒÏe°ÑÉu³ÉµÄÎļþÉu³Éµ½Ö¸¶¨µÄĿ¼£¬ÕaÑuÄÜ*ñʵÏÖÄØ£¿
»¹ÓУ¬¿ÉÒÔÍuÀiÃae²aÈeͼƬÂð£¿
PHPExcelÊÇÓ¢Îĵģ¬¿´ÁË°eÌiû¿´¶®¡£ÓÐûÓÐÖ±½ÓÉu³ÉµÄÀý×Ó¿´°¡£¿
ÏÂÃaeÎÒÃǸ½ÉÏÀý×Ó£º
/**
+----------------------------------------------------------
* µ¼ÈeËuÐeµÄÀa¿a ͬjavaµÄImport
* ±¾º¯ÊýÓлº´ae¹¦ÄÜ
+----------------------------------------------------------
* @param string $class Àa¿aÃuÃu¿Õ¼a×Ö*u´®
* @param string $baseUrl ÆðʼÂ*¾¶
* @param string $ext µ¼ÈeµÄÎļþÀ(C)Õ¹Ãu
+----------------------------------------------------------
* @return boolen
+----------------------------------------------------------
*/
function import($class, $baseUrl = '', $ext='.class.php') {
static $_file = array();
$class = str_replace(array('.', '#'), array('/', '.'), $class);
if ('' === $baseUrl && false === strpos($class, '/')) {
// ¼i²e±ðÃuµ¼Èe
return alias_import($class);
}
if (isset($_file[$class . $baseUrl]))
return true;
else
$_file[$class . $baseUrl] = true;
$class_strut = explode('/', $class);
if (empty($baseUrl)) {
if ('@' == $class_strut[0] || APP_NAME == $class_strut[0]) {
//¼ÓÔص±Ç°ÏiÄ¿Ó¦ÓÃÀa¿a
$baseUrl = dirname(LIB_PATH);
$class = substr_replace($class, basename(LIB_PATH).'/', 0, strlen($class_strut[0]) + 1);
}elseif ('think' == strtolower($class_strut[0])){ // think ¹Ù*½»uÀa¿a
$baseUrl = CORE_PATH;
$class = substr($class,6);
}elseif (in_array(strtolower($class_strut[0]), array('org', 'com'))) {
// org µÚÈý*½¹«¹²Àa¿a com ÆoÒµ¹«¹²Àa¿a
$baseUrl = LIBRARY_PATH;
}else { // ¼ÓÔØÆaËuÏiÄ¿Ó¦ÓÃÀa¿a
$class = substr_replace($class, '', 0, strlen($class_strut[0]) + 1);
$baseUrl = APP_PATH . '../' . $class_strut[0] . '/'.basename(LIB_PATH).'/';
}
}
if (substr($baseUrl, -1) != '/')
$baseUrl .= '/';
$classfile = $baseUrl . $class . $ext;
if (!class_exists(basename($class),false)) {
// Èç¹uÀa²»´aeÔÚ Ôoµ¼ÈeÀa¿aÎļþ
return require_cache($classfile);
}
}
/**
* µ¼³oEXCEL±i¸ñ
* @param array $data Êý¾Ý£¬¶þάÊý×e£¬Ã¿ÌoÊý¾ÝÒ»Ìo¼Ç¼
* @param array $title ÿÁÐÊý¾ÝµÄ×Ö¶ÎÃu£¬Ò»Î¨Êý×e£¬±ØÐeºÍÊý¾Ý˳ÐoÒ»Ö£¨¿ÉÊ¡ÂÔ£(C)
* @param string $filename excelÃu³Æ
* @param array $field ÐeÒªÖ¸¶¨µ¼³oµÄÊý¾Ý×ֶΣ¬ÅÅÐo±ØÐeºÍtitleÒ»Ö£¬¾ÍÊǺͲe³oÊý¾ÝµÄÊý×ekeyÖµ
*/
function exportExcel($data='',$title='',$filename='excel',$field=array()){
if(!$data || !is_array($data)) return false;
if($filename=='') $filename='excel';
if($field && is_array($field)){//Ö»Òªµ¼³oÖ¸¶¨×ֶΣ¬ÇÒ°´Õa¸o˳Ðoµ¼³o
$dateNew=array();
foreach ($data as $k=>$v){
foreach ($field as $fkey){
$dateNew[$k][$fkey]=$v[$fkey];
}
}
$data=$dateNew;
}
import("@.ORG.Util.ExcelXml");//µ÷Óõ¼³oexcelÀa
$xls = new ExcelXml('UTF-8', false, 'Sheet1');
$xls->addArray($data,$title);
$xls->generateXML($filename);
}