您的位置:首页 > 其它

一个很好用excel导出类

2013-11-27 13:35 330 查看
<?php

class PHPExcel
{

/**
* Header of excel document (prepended to the rows)
*/
public $header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?\>
<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\">";

/**
* Footer of excel document (appended to the rows)
*/
public $footer = "</Workbook>";

public $lines = array();

public $worksheet_title = "Table1";

/**
* Add a single row to the $document string
*/
public function addRow($array)
{
$cells = "";

foreach ($array as $k => $v)
{
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
/**continue;
if (is_numeric($v)) {
// 防止首字母为 0 时生成 excel 后 0 丢失
if (substr($v, 0, 1) == 0) {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
} else {
$cells .= "<Cell><Data ss:Type=\"Number\">" . $v . "</Data></Cell>\n";
}
} else {
$cells .= "<Cell><Data ss:Type=\"String\">" . $v . "</Data></Cell>\n";
}*/
}

$this->lines[] = "<Row>\n" . $cells . "</Row>\n";

}

/**
* Add an array to the document
*/
public function addArray($array)
{
foreach ($array as $k => $v):
$this->addRow($v);
endforeach;

}

/**
* Set the worksheet title
* @access public
* @param string $title Designed title
*/
public function setWorksheetTitle($title)
{
// strip out special chars first
$title = preg_replace("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);

$title = substr($title, 0, 31);

$this->worksheet_title = $title;

}

/**
* Generate the excel file
*/
public function generateXML($filename)
{
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");

echo stripslashes($this->header);
echo "\n<Worksheet ss:Name=\"" . $this->worksheet_title . "\">\n<Table>\n";
echo "<Column ss:Index=\"1\" ss:AutoFitWidth=\"0\" ss:Width=\"110\"/>\n";
echo implode("\n", $this->lines);
echo "</Table>\n</Worksheet>\n";
echo $this->footer;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐