php產生excel檔案原始碼
php教程產生excel檔案原始碼
class excel{
/**
*頭的excel檔案(首碼的行)
*
*從excel複製的xml規格。
*
* @訪問私人
* @無功串
*/
var $header = "
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">";
/**
*頁尾的excel檔案(附加到行)
*
*從excel複製的xml規格。
*
* @訪問私人
* @無功串
*/
var $footer = "";
/**
* document lines (rows in an array)
*
* @access private
* @var array
*/
var $lines = array ();
/**
工作表名稱
*
*包含一個單一的工作表名稱
*
* @訪問私人
* @無功串
*/
var $worksheet_title = "table1";
/**
添加一個單行的文檔字串$
*
* @訪問私人
* @帕拉姆庫馬拉陣列一維陣列
* @待辦事項行創造應做減本-> addarray
*/
function addrow ($array) {
// initialize all cells for this row
$cells = "";
// foreach key -> write value into cells
foreach ($array as $k => $v):
// 加個字串與數位判斷 避免產生的 excel 出現數字以字串儲存的警告
if(is_numeric($v)) {
// 防止首字母為 0 時產生 excel 後 0 丟失
if(substr($v, 0, 1) == 0) {
$cells .= "" . $v . "n";
} else {
$cells .= "" . $v . "n";
}
} else {
$cells .= "" . $v . "n";
}
endforeach;
// transform $cells content into one row
$this->lines[] = "n" . $cells . "n";
}
/**
*添加一個數組到文檔
*
*這應該是唯一的方法需要產生一個excel
*檔案。
*
* @訪問公開
* @帕拉姆庫馬拉數組二維數組
* @待辦事項可以轉移到__construct()稍後
*/
function addarray ($array) {
// run through the array and add them into rows
foreach ($array as $k => $v):
$this->addrow ($v);
endforeach;
}
/**
設定工作表名稱
*
*檢查的字串不允許字元(: /?*),
*削減它的最大31個字元,並設定標題。該死
*為何未允許字元無處可尋?視窗
*協助沒有協助...
*
* @訪問公開
* @帕拉姆庫馬拉字串$標題設計標題
*/
function setworksheettitle ($title) {
// strip out special chars first
$title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);
// now cut it to the allowed length
$title = substr ($title, 0, 31);
// set title
$this->worksheet_title = $title;
}
/**
*產生excel檔案
*
*最後產生的excel檔案,並使用header()函數
*提供給瀏覽器。
*
* @訪問公開
* @帕拉姆庫馬拉字串$檔案名稱名稱的excel檔案來產生(... xls)中
*/
function generatexml ($filename) {
// deliver header (as recommended in php manual)
header("content-type: application/vnd.ms-excel; charset=utf-8");
header("content-disposition: inline; filename="" . $filename . ".xls"");
// print out document to the browser
// need to use strips教程lashes for the damn ">"
echo stripslashes ($this->header);
echo "nworksheet_title . "">n
n"; echo "n"; echo implode ("n", $this->lines); echo "
nn";
echo $this->footer;
}
}
/**
* cakephp中使用方法
* 注意 ** cakephp 設定檔 define('debug', 0);
*
* vendor ('excel');
* $doc = array (
* 0 => array ('中國', '中國人', '中國人民', '123456');
* );
* $xls = new excel;
* $xls->addarray ( $doc );
* $xls->generatexml ("mytest");
*/
/**
* 非架構使用方法
*
* require_once('excel.php');
* $doc = array (
* 0 => array ('中國', '中國人', '中國人民', '123456');
* );
* $xls = new excel;
* $xls->addarray ( $doc );
* $xls->generatexml ("mytest");
*/
http://www.bkjia.com/PHPjc/630838.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/630838.htmlTechArticlephp產生excel檔案原始碼 php教程產生excel檔案原始碼 class excel{ /** *頭的excel檔案(首碼的行) * *從excel複製的xml規格。 * * @訪問私人 * @無功串...