Bu yüzden Excel'in anlayacağı tarzda dosya üreten basit bir sınıf yazmam gerekti. Aşağıda verdiğim koddaki sınıfı kullanarak Xml Spreadsheet dosyaları oluşturabilirsiniz. Bu sayede Excel'in anlayacağı tarzda biçimli tablolara sahip olacaksınız.
Aşağıdaki kodu, kendinize göre düzenleyebilirsiniz. GetHeader metodunda değişiklikler yaparak daha fazla sitil ve özellik ekleyebilirsiniz. Ya da yapmak istediğiniz sitil ve biçimleri ilk başta Excel ile yapıp, Xml SpreadSheet olarak kaydettikten sonra, GetHeader metodunu yaptığınız bu dosyaya göre düzenleyebilirsiniz. Örnek kullanım, sınıf kodlarının altındadır.
<?php
/**
* ExcelWriter 1.0
*
* Programmed by Fatih Tolga Ata
* fatih at diyezon dot com
* 13.01.2007
*/
Class ExcelWriter
{
var $output = "";
var $state;
var $ColumnWidths = array();
var $Rows = "";
var $DefaultWidth = 65;
function ExcelWriter($author, $sheettitle)
{
$this->state = "BEGIN";
$this->output .= $this->GetHeader($author, $sheettitle);
}
function Generate()
{
if ($this->state != "END")
{
$this->output .= $this->GetColumnWidths();
$this->output .= $this->Rows;
$this->output .= $this->GetFooter();
$this->state = "END";
}
return $this->output;
}
function GetHeader($author, $sheettitle)
{
$date = date("d-m-Y");
$time = date("H:i:s");
return chr(0xEF).chr(0xBB).chr(0xBF).<<<EOH
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
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">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author>$author</Author>
<LastAuthor>$author</LastAuthor>
<Created>{$date}T{$time}Z</Created>
<Version>12.00</Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>11985</WindowHeight>
<WindowWidth>19095</WindowWidth>
<WindowTopX>120</WindowTopX>
<WindowTopY>45</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Arial" x:CharSet="162" x:Family="Swiss" ss:Size="11"
ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<NumberFormat ss:Format="Fixed"/>
</Style>
<Style ss:ID="s63">
<NumberFormat ss:Format="#,##0.00 "YTL""/>
</Style>
<Style ss:ID="s64">
<Font ss:FontName="Arial" x:CharSet="162" x:Family="Swiss" ss:Size="11"
ss:Color="#000000" ss:Bold="1"/>
</Style>
</Styles>
<Worksheet ss:Name="$sheettitle">
<Table ss:DefaultColumnWidth="$this->DefaultWidth" ss:DefaultRowHeight="15">
EOH;
}
function GetFooter()
{
return <<<EOH
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
</Print>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveCol>3</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>
EOH;
}
function GetColumnWidths()
{
foreach($this->ColumnWidths as $width)
{
if ($width == 0)
$width = $this->DefaultWidth;
$str .= " <Column ss:AutoFitWidth="1" ss:Width="$width"/>n";
}
return $str;
}
function AddRow($data_arr, $type_arr)
{
$this->state = "PROCESS";
$this->Rows .= " <Row>n";
for ($i=0; $i <count($data_arr); $i++)
{
$type = "String";
$style = "";
switch($type_arr[$i])
{
case "num":
$style = " ss:StyleID="s62"";
$type = "Number";
break;
case "cur":
$style = " ss:StyleID="s63"";
$type = "Number";
break;
case "bold":
$style = " ss:StyleID="s64"";
}
$this->Rows .= " <Cell$style>";
$this->Rows .= "<Data ss:Type="$type">".$data_arr[$i]."</Data>";
$this->Rows .= "</Cell>n";
}
$this->Rows .= " </Row>n";
}
}
?> Bu da örnek kullanım:
<?
//Bu header fonksiyonları dosyayı download ettirebilmek içindir
header("Pragma: no-cache");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename="exceldosyasi.xml"");
header("Content-Transfer-Encoding: binary");
/////////////////////////////////////////////////////
//sınıfı oluşturalım
$writer = new ExcelWriter("Yazar ismi","Sheet adı");
//sütun genişliklerini belirleyelim.
$writer->ColumnWidths = array(165,0,50,0); //Sıfırın manası varsayılan değerdir.
$veriler = array("deneme",15,20,"Bold yazı");
$tipler = array("", "num", "cur", "bold"); //""=> String, "num"=>Sayı, "cur"=>Para, "bold"=>Bold String
$writer->AddRow($veriler, $tipler);
$excel = $writer->Generate();
header("Content-Length: ".strlen($excel)."");
echo $excel;
?>







