1. [代码][PHP]代码
<?php /**
* @desc excel接口
* @author mengdejun
*/
interface IExcel
{
//导入excel
public function import($fileName,$convert_callback_function=null);
//导出excel
public function export($fileName="excel");
//添加行
public function addRow(array $array,$sheet="sheet1");
//添加表头
public function addHead(array $array,$sheet="sheet1");
//添加工作簿
public function addSheet($sheet);
//释放资源
public function release();
}
?>
2. [代码][PHP]代码
<?php include_once ('IExcel.php');
/**
* @desc php生成excel类函数
* @author mengdejun
* @date 20100806
* @version 1.1.2
*/
class HtmlExcel implements IExcel
{
private $_line=array();
private $isHeader=false;
private $convert=false;
public function __construct(){}
/**
* @desc 添加表头信息,注:表头仅可添加一次,若无表头则将内容行的第一行作为表头
* @param array $array
* @param unknown_type $sheet
*/
public function addHead(array $array, $sheet = "sheet1")
{
if(!$this->isHeader)
$this->_line[]=$this->getLine($array);
$this->isHeader=true;
}
/**
* @desc 添加自定义字符转换或过滤函数
* @param unknown_type $functionName 回调函数名
*/
public function addConvert($functionName="convert")
{
$this->convert=$functionName;
}
protected function getLine(array $array,$sheet='sheet1')
{
$_temp="";
$_count=sizeof($array);
$index=0;
foreach($array as $value):
$_temp.=$this->convert($value);
if($index==$_count-1):
$_temp.="\t\n";
else:
$_temp.="\t";
endif;
$index++;
endforeach;
return $_temp;
}
/**
* @desc 添加excel行,若没有设置表头则将该数组的第一个元素作为表头
* @param array $array
* @param unknown_type $sheet
*/
public function addRow(array $array, $sheet = "sheet1")
{
$this->_line[]=$this->getLine($array,$sheet);
return $this->_line;
}
/**
* @desc 嵌套添加excel行,若没有设置表头则将该数组的第一个元素作为表头
* @param array $array
* @param unknown_type $sheet
*/
public function addRows(array $array,$sheet = "sheet1")
{
foreach($array as $value):
if(is_array($value)):
$this->addRow($value);
else:
$this->addRow($array);
endif;
endforeach;
}
/**
* @desc 添加工作簿,暂不支持V1.0
* @deprecated
* @param array $array
*/
public function addSheet($sheet) {return;}
/**
* @desc 返回excel表行数
*/
public function getRows()
{
return sizeof($this->_line);
}
/**
* @desc 返回excel内容
*/
public function getBody()
{
return $this->_line;
}
/**
* @desc 导出excel文件
* @param unknown_type $fileName 导出文件名
*/
public function export($fileName = "excel")
{
header("Content-Type: application/vnd.ms-excel;");
header("Content-Disposition:filename={$fileName}.xls");
for($index=0;$index<sizeof>_line);$index++):
echo $this->_line[$index];
endfor;
}
/**
* @desc 用户自定义编码转化以及数据筛选函数
* @param unknown_type $str
*/
protected function convert($str)
{
if(function_exists($this->convert)):
return call_user_func($this->convert,$str);
else:
return $str;
endif;
}
/**
* @desc 设置表格内容
* @param unknown_type $array
*/
public function setBody($array)
{
$this->_line=$array;
}
/**
* @desc 释放资源
*/
public function release()
{
unset($this->_line);
}
public function import($fileName,$convert_callback_function=null){}
}
?></sizeof>
3. [代码][PHP]代码
<?php include_once ('IExcel.php');
/**
* @desc php生成excel类函数 支持导入 导出 多工作薄(数据分卷技术)
* @filesource XmlExcel.php
* @author mengdejun
* @date 20100801
* @version 1.8.1
*/
if(!defined("CHARSET")):define("CHARSET","UTF-8");endif;
if(!defined("VERSION")):define("VERSION","12.00");endif;
if(!defined("THIS_VERSION")):define("THIS_VERSION","1.8.1");endif;
if(!defined("NULL")):define("NULL",null);endif;
class XmlExcel implements IExcel
{
private $header = "<?xml version=\"1.0\" encoding=\"%s\"?>\n<workbook>";
private $documentInfo="<documentproperties><author>{author}</author><created>{time}</created><company>{company}</company><version>{version}</version></documentproperties>";
private $footer = "</workbook>";
private $align_left="<style><Alignment ss:Horizontal=\"Left\" ss:Vertical=\"Center\"/></style>";
private $align_center="<style><Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/></style>";
private $align_right="<style><Alignment ss:Horizontal=\"Right\" ss:Vertical=\"Center\"/></style>";
private $align_bold="<style><Alignment ss:Horizontal=\"Center\" ss:Vertical=\"Center\"/><Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"12\" ss:Color=\"#000000\" ss:Bold=\"1\"/></style>";
private $align_default="<style><Alignment ss:Horizontal=\"%s\" ss:Vertical=\"Center\"/><Borders/><Font ss:FontName=\"宋体\" x:CharSet=\"134\" ss:Size=\"11\" ss:Color=\"#000000\"/><Interior/><NumberFormat/><Protection/></style>";
private $charset=CHARSET;
private $convert="convert";
private static $pre_workBook=NULL;
private $_line=NULL;
private $_column=NULL;
private $_columnType=NULL;
private $_styles=NULL;
private $_style=NULL;
private $_title=NULL;
private $_align="Left";
private $defaultHeight=13.5;
private $defaultWidth=54;
private $_sheets=NULL;
private $_heads=NULL;
/**
* @desc 构造方法 PHP5.X
* @param string $charset 字符编码
*/
public function __construct($charset = 'UTF-8')
{
$this->charset=$charset;
}
/**
* @desc 构造方法 PHP4.X
* @param string $charset 字符编码
*/
public function XmlExcel($charset = 'UTF-8')
{
$this->charset=$charset;
}
/**
* @desc 析构方法
*/
public function __destruct(){}
/**
* @desc 释放可用资源
* @return null
*/
public function release()
{
unset($this->_line,$this->_column,$this->_heads,$this->_sheets,$this->_styles,$this->_style,$this->_title,self::$pre_workBook);
}
/**
* @desc 数组行转换函数
* @param array $array
*/
protected function getLine(array $array)
{
$_temp="<row>";
foreach($array as $key=>$val):
#读取指定数据类型,默认String
$_type=!empty($this->_columnType)&&isset($this->_columnType)?!empty($this->_columnType[$key])&&isset($this->_columnType)?$this->_columnType[$key]:"String":"String";
$_temp.="<cell><data>{$this->convert($val)}</data></cell>";
endforeach;
$_temp.="</row>";
return $_temp;
}
/**
* @desc 添加表格头,默认的第一个数组将作为表头
* @param array $array
* @param string $sheet 工作表名
* @exception $array 不能为空
*/
public function addHead(array $array, $sheet = "sheet1")
{
$this->_line[$sheet][0]=$this->getLine($array);
$this->_title[$sheet]['width']=count($array)-1;
$this->_sheets[]=$sheet;
$this->_heads[$sheet][0]=$array;
}
/**
* @desc 添加行
* @param array $array
* @param string $sheet
*/
public function addRow(array $array, $sheet = "sheet1",$isErrorReport=true)
{
if($isErrorReport):
if(empty($array)||!isset($array)||count($array)==0):
exit("data can't null'");
else:
$this->_line[$sheet][]=$this->getLine($array);
endif;
else:
$this->_line[$sheet][]=$this->getLine($array);
endif;
}
/**
* @desc 设置工作簿的表头对象
* @param $head 表头数据
* @param $sheet 工作簿名称
*/
public function setSheetHead(array $head,$sheet="Sheet1")
{
$this->_line[$sheet][]=$this->getLine($head);
}
/**
* @desc 添加多行 支持嵌套数组
* @param array $array
* @param unknown_type $sheet
*/
public function addRows(array $array,$sheet = "Sheet1")
{
foreach($array as $value):
if(is_array($value)):
$this->addRow($value,$sheet);
else:
$this->addRow($array,$sheet);
endif;
endforeach;
}
/**
* @desc 获取制定工作薄的列宽度
* @param @sheet 工作薄名称
*/
public function getColumnLength($sheet="Sheet1")
{
return $this->_title[$sheet]['width'];
}
/**
* @desc 添加工作薄
* @param unknown_type unknown_type $sheet
*/
public function addSheet($sheet,$array=array())
{
$this->_line[$sheet][]=$array;
}
/**
* @desc 工作薄添加标题
* @param string $str 标题
* @param string $sheet 工作薄名
*/
public function addTitle($str,$sheet="Sheet1")
{
$str=$this->convert($str);
$this->_title[$sheet]['title']="<row><cell><data>{$str}</data></cell></row>";
}
/**
* @desc excel导出
* @param string $fileName 导出的文件名
*/
public function export($fileName = "excel",$isConvert=false)
{
if($isConvert):
$fileName=$this->getConvertString($fileName);
endif;
header("Content-Type: application/vnd.ms-excel; charset=" . $this->charset);
header("Content-Disposition:attachment; filename=\"{$fileName}.xls\"");
echo stripslashes(sprintf($this->header, $this->charset));
echo str_replace("{company}","xiao meng online",str_replace("{time}",date("Y-m-dH:i:s",time()),str_replace("{author}","mengdejun",str_replace("{version}",VERSION,$this->documentInfo))));
echo "<styles>";
echo stripslashes(sprintf($this->align_default, $this->_align));
echo $this->align_left;
echo $this->align_right;
echo $this->align_center;
echo $this->align_bold;
echo "</styles>";
$_hasData=count($this->_line)==0?false:true;
if($_hasData):
#有数据,解析数组对象到excel表格
foreach($this->_line as $key=>$value):
echo "<worksheet>convert($key)}\"><table>defaultWidth}\" ss:DefaultRowHeight=\"{$this->defaultHeight}\">";
#列样式和宽度
if(isset($this->_column[$key]['style_width'])):
foreach($this->_column[$key]['style_width'] as $s_key=>$s_value):
echo "<column></column>";
endforeach;
endif;
#表格标题
if(!empty($this->_title[$key]['title'])):
echo str_replace("{num}",$this->_title[$key]['width'],$this->_title[$key]['title']);
endif;
#单元格
foreach($value as $_v):
echo $_v;
endforeach;
echo "</table></worksheet>";
endforeach;
#加载标准工作薄(默认三个工作簿)
$length=count($this->_line);
while($length
4. [代码][PHP]代码
<?php include'lib/XmlExcel.php';
$xls=new XmlExcel;
$xls->setDefaultWidth(80);
$xls->setDefaultAlign("center");
$xls->setDefaultHeight(18);
$xls->addHead(array("title1","title2","title3","title4","title5","title6"),"demo1");
for($ind=0;$indaddRow(array($ind,$ind,$ind,$ind,$ind,$ind),"demo1");
endfor;
$xls->export("demo1");
?>
5. [代码][PHP]代码
<?php include'lib/XmlExcel.php';
$xls=new XmlExcel;
$xls->setDefaultWidth(80);
$xls->setDefaultAlign("center");
$xls->setDefaultHeight(18);
for($ind=0;$indaddPageRow(array("title1","title2","title3","title4","title5","title6"),array($ind,$ind,$ind,$ind,$ind,$ind),10,$xls->uniqueName("demo"));
endfor;
$xls->export("demo2");
?>
6. [图片] e1.jpg
7. [图片] e2.jpg

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!











