為了將 PHP 的資料轉存成 Excel 格式~
搞了一陣子的 new COM("Excel.sheet");
結果今天才發現原來可以將資料轉存成符合 Excel 格式的 XML 檔~
真是白忙一場~
這種方式不但快速又不耗資源且不受限於 Windows 的主機~
開發程式也不受限~ 只要能產生文字檔儲存即可~
也可線上動態產生供人下載~
以下是個簡單的範例:
下載 XML.xls
//文件宣告
<?xml version="1.0" encoding="utf-8" ?>
<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">
//樣式
<Styles>
//所有儲存格的樣式(全域樣式)
<Style ss:ID="Default" ss:Name="Normal">
<Font ss:FontName="Arial" x:Family="Roman" ss:Size="10" />
<NumberFormat ss:Format="#,##0_ " />
</Style>
//文字居中
<Style ss:ID="s10">
<Alignment ss:Horizontal="Center" />
</Style>
//數值千位字元+小數點2位
<Style ss:ID="s11">
<NumberFormat ss:Format="#,##0.00_ " />
</Style>
//紅字
<Style ss:ID="s12">
<Font ss:Color="#FF0000" />
</Style>
//文字居中+粗+底色(表頭用)
<Style ss:ID="s13">
<Alignment ss:Horizontal="Center" />
<Font ss:Bold="1" />
<Interior ss:Color="#97C6F2" ss:Pattern="Solid" />
</Style>
//數值千位加小數1位+粗+底色(表尾用)
<Style ss:ID="s14">
<NumberFormat ss:Format="#,##0.0_ " />
<Font ss:Bold="1" />
<Interior ss:Color="#CCCCCC" ss:Pattern="Solid" />
</Style>
//文加居中+粗+底色(表尾用)
<Style ss:ID="s15">
<Alignment ss:Horizontal="Center" />
<Font ss:Bold="1" />
<Interior ss:Color="#CCCCCC" ss:Pattern="Solid" />
</Style>
</Styles>
//工作表一 (可增加)
<Worksheet ss:Name="範例一">
<Table ss:DefaultRowHeight="16">
//列樣式
<Column ss:StyleID="s10" ss:AutoFitWidth="1" />
<Column ss:AutoFitWidth="1" />
<Column ss:AutoFitWidth="1" />
//第一行(表頭)
<Row>
<Cell ss:StyleID="s13"><Data ss:Type="String">月份</Data></Cell>
<Cell ss:StyleID="s13"><Data ss:Type="String">收入</Data></Cell>
<Cell ss:StyleID="s13"><Data ss:Type="String">支出</Data></Cell>
<Cell ss:StyleID="s13"><Data ss:Type="String">結餘</Data></Cell>
</Row>
//第二行(內文)
<Row>
<Cell><Data ss:Type="String">101年1月</ss:Data></Cell>
<Cell><Data ss:Type="Number">12345</Data></Cell>
<Cell><Data ss:Type="Number">23456</Data></Cell>
<Cell ss:StyleID="s12" ss:Formula="=RC[-2]-RC[-1]"><Comment><ss:Data>12345-23456=-11111</ss:Data></Comment></Cell>
</Row>
//第三行(內文)
<Row>
<Cell><Data ss:Type="String">101年2月</ss:Data></Cell>
<Cell><Data ss:Type="Number">34566</Data></Cell>
<Cell><Data ss:Type="Number">23456</Data></Cell>
<Cell ss:Formula="=RC[-2]-RC[-1]"><Comment><ss:Data>34566-23456=11110</ss:Data></Comment></Cell>
</Row>
//第四行(表尾)
<Row>
<Cell ss:StyleID="s15"><Data ss:Type="String">平均</Data></Cell>
<Cell ss:StyleID="s14" ss:Formula="=AVERAGE(R[-2]C:R[-1]C)"></Cell>
<Cell ss:StyleID="s14" ss:Formula="=AVERAGE(R[-2]C:R[-1]C)"></Cell>
<Cell ss:StyleID="s14" ss:Formula="=AVERAGE(R[-2]C:R[-1]C)"></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>
