码迷,mamicode.com
首页 > Web开发 > 详细

php 导出excel

时间:2014-09-18 14:41:53      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:io   os   ar   for   sp   cti   on   c   ad   

<?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" );

?>

 

php 导出excel

标签:io   os   ar   for   sp   cti   on   c   ad   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!