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 ) . " ";
} else {
$title .= $v . " ";
}
}
$title .= " ";
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 ) . " ";
} else {
$content .= $vs . ". ";
}
}
$content .= " ";
}
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" );

?>

原文地址:https://www.cnblogs.com/cxlings/p/3979044.html