php 导出Excle类文件
strongphp 导出EXcle 类库/strong
php 导出EXcle 类库
<?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_HTML
{
/**
* Header (of document)
* @var string
*/
private $header =
'<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<style type="text/css">
<!--
.global {
font-family: "宋体";
font-size: 14px;
font-style: normal;
line-height: normal;
font-weight: normal;
color: #996633;
}
.biaoti {
font-family: "宋体";
font-size: 18px;
font-weight: bold;
color: #666666;
}
td {
border: 1px solid #000000;
text-align:left;
}
th {
background-color: #CCCCCC;
}
table {
border: 1px solid #000000;
}
-->
</style>
</head>
<body class="global">';
/**
* Footer (of document)
* @var string
*/
private $footer = "</body></html>";
/**
* Lines to output in the excel document
* @var array
*/
private $lines = array();
/**
* Used encoding
* @var string
*/
private $sEncoding;
/**
* Convert variable types
* @var boolean
*/
private $bConvertTypes;
/**
* Worksheet title
* @var string
*/
private $sWorksheetTitle;
/**
* Constructor
*
* The constructor allows the setting of some additional
* parameters so that the library may be configured to
* one's needs.
*
* On converting types:
* When set to true, the library tries to identify the type of
* the variable value and set the field specification for Excel
* accordingly. Be careful with article numbers or postcodes
* starting with a '0' (zero)!
*
* @param string $sEncoding Encoding to be used (defaults to UTF-8)
* @param boolean $bConvertTypes Convert variables to field specification
* @param string $sWorksheetTitle Title for the worksheet
*/
public function __construct()
{
}
/**
* Set encoding
* @param string Encoding type to set
*/
public function setEncoding($sEncoding)
{
$this->sEncoding = $sEncoding;
}
/**
* Set encoding
* @param string Encoding type to set
*/
public function initExcel($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
{
$this->bConvertTypes = $bConvertTypes;
$this->setEncoding($sEncoding);
$this->setWorksheetTitle($sWorksheetTitle);
}
/**
* 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):
$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
$cells .= "<Td>" . $v . "</Td>\n";
endforeach;
$this->lines[] = "<Tr>\n" . $cells . "</Tr>\n";
}
/**
* Add th
*
* 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 addTh ($array)
{
$cells = "";
foreach ($array as $k => $v):
$v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
$cells .= "<Th>" . $v . "</Th>\n";
endforeach;
$this->lines[] = "<Tr>\n" . $cells . "</Tr>\n";
}
/**
* Add an array to the document
* @param array 2-dimensional array
*/
public function addArray ($array)
{
foreach ($array as $k => $v) :
if ($k == 'title') :
$this->addTh($v);
else :
$this->addRow ($v);
endif;
endforeach;
}
/**
* 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 "\n<Table>\n<caption class=\"biaoti\">".$this->sWorksheetTitle."</caption>\n";
foreach ($this->lines as $line)
echo $line;
echo "</Table>\n";
echo $this->footer;
}
}
?>
- 上一篇:发博时间计算
- 下一篇:phpcms2008增加浏览次数插件
精彩图集
精彩文章






