PHP写出Excel文件的操作类

<?php

//查询结果
$db->query($sQueryList);
$numRows = $db->num_rows();
$DataRows = $db->cdata();
if ($numRows != 0) {

$xls = new Exel(true);
//写标题栏
$xls->WriteLabel(0,0,"A");
$xls->WriteLabel(0,1,"B");
$xls->WriteLabel(0,2,"C");
$xls->WriteLabel(0,3,"D");
$xls->WriteLabel(0,4,"E");
$xls->WriteLabel(0,5,"F");
$xls->WriteLabel(0,6,"G");
$xls->WriteLabel(0,7,"H");
$xls->WriteLabel(0,8,"J");
$xls->WriteLabel(0,9,"K");
//写数据区
for ($i=0;$i $n = $i + 1;
$xls->WriteLabel($n,0,$n);
$xls->WriteLabel($n,1,"data1");
$xls->WriteLabel($n,2,"data3");
$xls->WriteLabel($n,3,"data4");
$xls->WriteLabel($n,4,"data4");
$xls->WriteLabel($n,5,"data5");
$xls->WriteLabel($n,6,"data6");
$xls->WriteLabel($n,7,"data7");
$xls->WriteLabel($n,8,"data8");
$xls->WriteLabel($n,9,"data9");
}

}

?>

[@more@]

<?php
// +----------------------------------------------------------------------+
// | PHP Class for generating Exel binary data. (PHP 4 >= 4.0b1) |
// +----------------------------------------------------------------------+
// | Copyright (c) 2001 by KVN |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | [url]http://www.php.net/license/2_02.txt.[/url] |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | [email]license@php.net[/email] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Vitaliy N. Kravchenko <kvn@phbme.ntu-kpi.kiev.ua> |
// +----------------------------------------------------------------------+
//
// $Id$ Thu Jul 11 EEST 2002 version 2.5

/*
Usage Example:


$xls = new Exel(false); // begin Excel stream, start Exel and dont ask "Save"..
while($row = $res->fetchRow())
{
$line++;
for ($i=0; $i{
$xls->WriteLabel($line,$i,$row[$i]);
}
}
$xls->SendFile("moe.xls"); // close the stream


Also present Function to write a Number (double) into Row, Col:
$xls->WriteNumber($Row,$Col,$Number);

Example2:

$str = Exel::WriteLabel(1,1,"Hello World!");
$str .= Exel::WriteNumber(1,2,671);
header(..);
echo Exel::Begin();
echo $str;
echo Exel::EOF();


Examle3:

header(....)
$xls = new Exel();
$xls->.... WriteNumber/Label..
echo $xls->GetData();

*/

class Exel {
var $xls_data = "";
var $error = "";
var $download = "true";

function Exel($download = "true") {
$this->download = $download;
$this->Begin();
}

function _Header($filename) {
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
if ($this->download) {
header ("Content-type: application/x-msexcel");
header ("Content-Disposition: attachment; filename=$filename" );
} else {
header ("Content-type: application/x-msexcel");
}

header ("Content-Description: PHP Generated Data" );
}

function Begin() {
$str = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
$this->xls_data = $str;
return $str;
}

function EOF() {
$str = pack("ss", 0x0A, 0x00);
$this->xls_data .= $str;
return $str;
}

function WriteNumber($Row, $Col, $Value) {
$str = pack("sssss", 0x203, 14, $Row, $Col, 0x0);
$str .= pack("d", $Value);
$this->xls_data .= $str;
return $str;
}

function WriteLabel($Row, $Col, $Value ) {
$L = strlen($Value);
$str = pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
$str .= $Value;
$this->xls_data .= $str;
return $str;
}

function SendFile($filename = "test.xls") {
//$this->_Header($filename);
//echo $this->xls_data;
echo $this->EOF();
}

function SendFileDown($filename = "test.xls") {
//$this->_Header($filename);
//echo $this->xls_data;
//echo $this->EOF();
$this->toFile($filename);
}

function GetData($eof = "true") {
if ($eof)
return $xls_data . $this->EOF();
else
return $xls_data;
}

/**
* Function to write Excel-data to $file
* Example:
* if ($xls->toFile("/path/to/file.xls")) {
* echo "OK";
* } else {
* echo $xls->getLastError();
* }
*
*
* @param $efile string - File name.
*
*/
function toFile($file = "test.xls") {
$fp = @fopen($file,"w");
if (is_resource($fp)) {
fwrite($fp, $this->xls_data);
fclose($fp);
return true;
} else {
$this->error = "Can't access file '$file' for writting!";
return false;
}
}

function getLastError() {
return $this->error;
}

function _Exel() {
unset($this->xls_data);
unset ($this);
}
} // End of class Exel.

?>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/9812031/viewspace-918666/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/9812031/viewspace-918666/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值