PHP导出excel类完整实例程序
本文章来给各位同学详细介绍关于PHP导出excel类完整实例程序代码,这里我们使用了phpExcel插件,大家使用前先去下载一个phpExcel插件.
php exeel.class.php文件,代码如下:
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class Excel_XML
- {
-
-
-
-
-
- private $header = "<?xml version="1.0" encoding="%s"?>n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http:
-
-
-
-
-
- private $footer = "</Workbook>";
-
-
-
-
-
- private $lines = array();
-
-
-
-
-
- private $sEncoding;
-
-
-
-
-
- private $bConvertTypes;
-
-
-
-
-
- private $sWorksheetTitle;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
- {
- $this->bConvertTypes = $bConvertTypes;
- $this->setEncoding($sEncoding);
- $this->setWorksheetTitle($sWorksheetTitle);
- }
-
-
-
-
-
- public function setEncoding($sEncoding)
- {
- $this->sEncoding = $sEncoding;
- }
-
-
-
-
-
-
-
-
-
- public function setWorksheetTitle ($title)
- {
- $title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);
- $title = substr ($title, 0, 31);
- $this->sWorksheetTitle = $title;
- }
-
-
-
-
-
-
-
-
-
-
- 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 .= "<Cell><Data ss:Type="$type">" . $v . "</Data></Cell>n";
- endforeach;
- $this->lines[] = "<Row>n" . $cells . "</Row>n";
- }
-
-
-
-
-
- public function addArray ($array)
- {
- foreach ($array as $k => $v)
- $this->addRow ($v);
- }
-
-
-
-
-
-
- public function generateXML ($filename = 'excel-export')
- {
-
- $filename = preg_replace('/[^aA-zZ0-9_-]/', '', $filename);
-
-
- header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
- header("Content-Disposition: inline; filename="" . $filename . ".xls"");
-
-
- echo stripslashes (sprintf($this->header, $this->sEncoding));
- echo "n<Worksheet ss:Name="" . $this->sWorksheetTitle . "">n<Table>n";
- foreach ($this->lines as $line)
- echo $line;
-
- echo "</Table>n</Worksheet>n";
- echo $this->footer;
- }
-
- }
- ?>
excel.class.php文件,代码如下:
- <?php
-
-
-
-
- class myexcel{
- private $filelds_arr1=array();
- private $filelds_arr2=array();
- private $filename;
-
- function __construct($fields_arr1,$fields_arr2,$filename='test'){
- $this->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<=$arrcount;$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);
- }
- }
-
-
-
-
-
-
-
- ?>
注意,我们把上面的代码分别保存成两个文件再进行操作.