<?
class Xls{
/**
* The clasee of Spreadsheet_Excel_Writer's object
* @var Spreadsheet_Excel_Writer
*/
protected $xls;
/**
* The clasee of Spreadsheet_Excel_Writer_Worksheet's object
* @var Spreadsheet_Excel_Writer_Worksheet
*/
protected $sheet;
/**
* The clasee of Spreadsheet_Excel_Writer_Format's object
* @var Spreadsheet_Excel_Writer_Format
*/
protected $format;
/**
* The data will be used to create file
* the data must be an tow levels array,
* for example:
* array(array('title_a'=>'a','title_b'=>'b'),array('title_a'=>'a','title_b'=>'b') )
* @var array
*/
protected $data;
/**
* The file's rows
* @var int
*/
protected $rowCount;
/**
* The titles of the sheet
* @var array
*/
protected $title;
/**
* The construct method
* initialize the xls,sheet,
* format and rowCount.
*/
public function __construct()
{
$this->xls = &new Spreadsheet_Excel_Writer();
$this->xls->setVersion(8);
$this->sheet = & $this->xls->addWorksheet('sheet1');
$this->sheet->setInputEncoding('utf-8');
$formats = $this->getFormat();
$this->sheet->format = &$this->xls->addFormat($formats);
$this->rowCount = 0;
}
/**
* Initialize data.
* @param array $data
*/
public function setData(array $data)
{
$this->data=$data;
}
/**
* Send file to the browser.
*
* @param string $filename
* @param array $title
*
*/
public function sendFile($filename,$title = array())
{
if(empty ($filename))
{
$filename = data('Y-m-d');
}
$this->xls->send($filename);
if(!empty ($title))
{
$this->title = $title;
$this->writeTitle();
}
$this->writeRows();
$this->setColumns();
$this->setRows();
$this->xls->close();
}
/**
* Write rows to the file.
*/
public function writeRows()
{
$COUNTROW = count($this->data);
for($i =0 ;$i < $COUNTROW;$i++)
{
$this->writeRow($this->rowCount++,0,$this->data[$i],$this->format);
}
}
/**
* Wirte row to the file.
*
* @param int $row
* @param int $col
* @param array $data
*
*/
protected function writeRow($row,$col,$data)
{
if(empty ($this->title))
{
$this->sheet->writeRow($row,$col,$data,$this->format);
return;
}
foreach ($this->title as $key => $val)
{
$this->sheet->write($row, $col,$data[$key] ,$this->format);
$col++;
}
}
/**
* Return the array to create Spreadsheet_Excel_Writer_Format object.
*
* @param string $style
* @return array
*/
protected function getFormat($style = 'body')
{
$style = strtolower($style);
switch ($style) {
case 'body':
$formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center');
break;
case 'title':
$formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center','Bold'=>true);
break;
default:
$formats =array('textwrap'=>1,'HAlign'=>'center','VAlign'=>'center');
break;
}
return $formats;
}
/**
* Set all columns width with data's string lenth.
*/
protected function setColumns()
{
$width = 10;
$col = 0;
foreach ($this->title as $key =>$val)
{
$this->sheet->setColumn($col, $col, strlen($this->data[0][$key])>$width?strlen($this->data[0][$key]):$width);
$col++;
}
}
/**
* Set all rows hight.
*/
protected function setRows()
{
$hight = 20;
for($i=0;$i<$this->rowCount;$i++){
$this->sheet->setRow($i, $hight);
}
}
/**
* Write titles to file.
* @param array $title
*/
public function writeTitle(array $title=array())
{
if(empty ($title))
{
$title = $this->title;
}
$formats = $this->getFormat('title');
$this->sheet->writeRow($this->rowCount++,0,$title,$this->xls->addFormat($formats));
}
}
基于Spreadsheet的php生成excel
最新推荐文章于 2023-12-18 15:35:12 发布