php写数据到excel中,PHP写的从数据库导入到EXCEL

PHP code

class Excel{

var $header = "<?xml version="1.0" encoding="utf-8"?>

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">";

var $footer = "

";

var $lines = array ();

var $worksheet_title = "Table1";

function addRow ($array) {

$cells = "";

foreach ($array as $k => $v):

if(is_numeric($v)) {

if(substr($v, 0, 1) == 0) {

$cells .= "" . $v . "n";

} else {

$cells .= "" . $v . "n";

}

} else {

$cells .= "" . $v . "n";

}

endforeach;

$this->lines[] = "n" . $cells . "n";

unset($arry);

}

function setWorksheetTitle ($title) {

$title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);

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

$this->worksheet_title = $title;

}

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 stripslashes for the damn ">"

echo stripslashes ($this->header);

echo "nworksheet_title . "">n

echo "n";

echo implode ("n", $this->lines);

echo "

nn";

echo $this->footer;

exit;

}

function write ($filename) // 重点

{

$content= stripslashes ($this->header);

$content.= "nworksheet_title . "">n

$content.= "n";

$content.= implode ("n", $this->lines);

$content.= "

nn";

$content.= $this->footer;//EXCEL文件

//error_log($content, 3,$filename);

if (!file_exists($filename))//判断有没有文件

{

fopen($filename,'a');

}

$fp = fopen($filename,'a');

fwrite($fp, $content);//写入文件

fclose($fp);

unset($this->lines);//清空内存中的数据

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值