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');
?>
导出结果如下图:
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!