本文章来给各位同学详细介绍关于php导出excel类完整实例程序代码,这里我们使用了phpexcel插件哦,大家使用前先去下载一个phpexcel插件。
php exeel.class.php文件
代码如下 复制代码
bconverttypes = $bconverttypes;
$this->setencoding($sencoding);
$this->setworksheettitle($sworksheettitle);
}
/**
* set encoding
* @param string encoding type to set
*/
public function setencoding($sencoding)
{
$this->sencoding = $sencoding;
}
/**
* set worksheet title
*
* strips out not allowed characters and trims the
* title to a maximum length of 31.
*
* @param string $title title for worksheet
*/
public function setworksheettitle ($title)
{
$title = preg_replace (/[\|:|/|?|*|[|]]/, , $title);
$title = substr ($title, 0, 31);
$this->sworksheettitle = $title;
}
/**
* add row
*
* adds a single row to the document. if set to true, self::bconverttypes
* checks the type of variable and returns the specific field settings
* for the cell.
*
* @param array $array one-dimensional array with row content
*/
private function addrow ($array)
{
$cells = ;
foreach ($array as $k => $v):
$type = 'string';
if ($this->bconverttypes === true && is_numeric($v)):
$type = 'number';
endif;
$v = htmlentities($v, ent_compat, $this->sencoding);
$cells .= . $v . n;
endforeach;
$this->lines[] = n . $cells . n;
}
/**
* add an array to the document
* @param array 2-dimensional array
*/
public function addarray ($array)
{
foreach ($array as $k => $v)
$this->addrow ($v);
}
/**
* generate the excel file
* @param string $filename name of excel file to generate (...xls)
*/
public function generatexml ($filename = 'excel-export')
{
// correct/validate filename
$filename = preg_replace('/[^aa-zz0-9_-]/', '', $filename);
// deliver header (as recommended in php manual)
header(content-type: application/vnd.ms-excel; charset= . $this->sencoding);
header(content-disposition: inline; filename= . $filename . .xls);
// print out document to the browser
// need to use stripslashes for the damn >
echo stripslashes (sprintf($this->header, $this->sencoding));
echo nsworksheettitle . >nn;
foreach ($this->lines as $line)
echo $line; echo
nn;
echo $this->footer;
}
}
?>
excel.class.php文件
代码如下 复制代码
filelds_arr1=$fields_arr1;
$this->filelds_arr2=$fields_arr2;
$this->filename=$filename;
}
function putout(){
require 'php-excel.class.php';
$datavalue=$this->filelds_arr2;
$newdata[0]=$this->filelds_arr1;
$arrcount=count($datavalue);
for($i=1;$i$newdata[$i]=array_values($datavalue[$i-1]);
}
$filename=$this->filename;
$xls = new excel_xml('utf-8', false, 'my test sheet');
$xls->addarray($newdata);
$xls->generatexml($filename);
}
}
/*
**用法示例(注意导出的文件名只能用英文)
$asdasd=new myexcel(array('姓名','电话'),array(array('贾新明','13521530320')),'abc');
$asdasd->putout();
*/
?>
注意,我们把上面的代码分别保存成两个文件再进行操作。