php 导出excel

<?php
class Excel {
var $inEncode;
var $outEncode;
public function _construct() {
}
public function setEncode($incode, $outcode) {
$this->inEncode = $incode;
$this->outEncode = $outcode;
}
public function setTitle($titlearr) {
$title = "";
foreach ( $titlearr as $v ) {
if ($this->inEncode == $this->outEncode) {
$title .= iconv ( $this->inEncode, $this->outEncode, $v ) . "\t";
} else {
$title .= $v . "\t";
}
}
$title .= "\n";
return $title;
}
public function setRow($array) {
$content = "";
foreach ( $array as $k => $v ) {
foreach ( $v as $vs ) {
if ($this->inEncode != $this->outEncode) {
$content .= iconv ( $this->inEncode, $this->outEncode, $vs ) . "\t";
} else {
$content .= $vs . ".\t";
}
}
$content .= "\n";
}
return $content;
}
public function getExcel($titlearr, $array, $filename = ‘‘) {
if ($filename == ‘‘) {
$filename = date ( ‘Y-m-d‘ );
}
$title = $this->setTitle ( $titlearr );
$content = $this->setRow ( $array );
$filename=iconv("UTF-8","GB2312",$filename);
header ( "Content-type:application/vnd.ms-exce8 charset=UTF-8" );
header ( "Content-Disposition:attachment; filename=" . $filename . ".xls" );
echo $title;
echo $content;
}
}

$excel = new Excel ();
// 设置编码:
$excel->setEncode ( "utf-8", "gb2312" ); // 如果不转码,参数写一样即可,例如$excel->setEncode("utf-8","utf-8");
// 设置标题栏
$titlearr = array (
"a",
"b",
"c",
"d"
);
// 设置内容栏
$contentarr = array (
1 => array (
"中国",
"ac",
"ad",
"ae"
),
2 => array (
"abc",
"acc",
"adc",
"aec"
),
3 => array (
"abd",
"acd",
"add",
"aed"
),
4 => array (
"abe",
"ace",
"ade",
"aee"
)
);
$excel->getExcel ( $titlearr, $contentarr, "导出excel" );

?>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。