用Simple Excel导出xls实现方法
程序员文章站
2023-01-05 22:30:01
因为前几天写了篇文章,用,顺便说些excel导出问题,我用的是simple excel,一个很简单的导出xls类,特好用! simple excel源码如下: 复制代码 代...
因为前几天写了篇文章,用,顺便说些excel导出问题,我用的是simple excel,一个很简单的导出xls类,特好用!
simple excel源码如下:
<?php
/**
* simple excel generating from php5
*
* @package utilities
* @license http://www.opensource.org/licenses/mit-license.php
* @author oliver schwarz <oliver.schwarz@gmail.com>
* @version 1.0
*/
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://www.w3.org/tr/rec-html40\">";
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;
}
}
?>
使用php案例如下:
<?php
/**
* @author mckee
* @blog www.phpddt.com
*/
require_once 'excel.class.php';
$xls = new excel_xml('utf-8',false,'测试');
$data = array(
1 => array('名称','地址'),
2 => array('php点点通','www.phpddt.com'),
3 => array('百度','www.baidu.com')
);
$xls->addarray($data);
$xls->generatexml('name4test');
?>
导出结果如下图:
simple excel源码如下:
复制代码 代码如下:
<?php
/**
* simple excel generating from php5
*
* @package utilities
* @license http://www.opensource.org/licenses/mit-license.php
* @author oliver schwarz <oliver.schwarz@gmail.com>
* @version 1.0
*/
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://www.w3.org/tr/rec-html40\">";
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;
}
}
?>
使用php案例如下:
复制代码 代码如下:
<?php
/**
* @author mckee
* @blog www.phpddt.com
*/
require_once 'excel.class.php';
$xls = new excel_xml('utf-8',false,'测试');
$data = array(
1 => array('名称','地址'),
2 => array('php点点通','www.phpddt.com'),
3 => array('百度','www.baidu.com')
);
$xls->addarray($data);
$xls->generatexml('name4test');
?>
导出结果如下图: