用PHP生成Excel文件
简要解释:因为excel的特殊性,简单的写进excel不行,所以用了excel里的“保存为web”的功能先导出,然后用php将excel生成的web代码写进到缓冲区,同时插入递交过来的表单内容项,最后从所有代码从缓冲区里输出,并用head头提供给出下载
相关链接:http://www.bettina-attack.de/jonny/view.php/projects/php_writeexcel/demo/这里有老外已写好的专门用php来操作excel的类
Code: [Copy to clipboard]
<?
ob_start();
?>
<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=Content-Type content="text/html; charset=GB2312">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<link rel=File-List href="./users.files/filelist.xml">
<link rel=Edit-Time-Data href="./users.files/editdata.mso">
<link rel=OLE-Object-Data href="./users.files/oledata.mso">
<!--[if gte mso 9]><xml>
<o:DocumentProperties>
<o:LastAuthor>friday</o:LastAuthor>
<o:Created>2003-06-12T23:15:28Z</o:Created>
<o:LastSaved>2003-06-12T23:15:28Z</o:LastSaved>
<o:Version>9.2812</o:Version>
</o:DocumentProperties>
</xml><![endif]-->
<style>
<!--table
{mso-displayed-decimal-separator:"\.";
mso-displayed-thousand-separator:"\,";}
@page
{margin:1.0in .75in 1.0in .75in;
mso-header-margin:.5in;
mso-footer-margin:.5in;}
tr
{mso-height-source:auto;
mso-ruby-visibility:none;}
col
{mso-width-source:auto;
mso-ruby-visibility:none;}
br
{mso-data-placement:same-cell;}
.style0
{mso-number-format:General;
text-align:general;
vertical-align:bottom;
white-space:nowrap;
mso-rotate:0;
mso-background-source:auto;
mso-pattern:auto;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
border:none;
mso-protection:locked visible;
mso-style-name:常规;
mso-style-id:0;}
td
{mso-style-parent:style0;
padding-top:1px;
padding-right:1px;
padding-left:1px;
mso-ignore:padding;
color:windowtext;
font-size:12.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-number-format:General;
text-align:general;
vertical-align:bottom;
border:none;
mso-background-source:auto;
mso-pattern:auto;
mso-protection:locked visible;
white-space:nowrap;
mso-rotate:0;}
.xl24
{mso-style-parent:style0;
vertical-align:middle;}
.xl25
{mso-style-parent:style0;
font-size:10.0pt;
font-weight:700;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:silver;
mso-pattern:auto none;}
.xl26
{mso-style-parent:style0;
font-size:10.0pt;
font-weight:700;
font-family:Verdana, sans-serif;
mso-font-charset:0;
text-align:center;
vertical-align:middle;
border:.5pt solid windowtext;
background:silver;
mso-pattern:auto none;}
.xl27
{mso-style-parent:style0;
font-size:9.0pt;
font-family:Verdana, sans-serif;
mso-font-charset:0;
border:.5pt solid windowtext;}
.xl28
{mso-style-parent:style0;
font-size:9.0pt;
font-family:Verdana, sans-serif;
mso-font-charset:0;
vertical-align:middle;
border:.5pt solid windowtext;}
ruby
{ruby-align:left;}
rt
{color:windowtext;
font-size:9.0pt;
font-weight:400;
font-style:normal;
text-decoration:none;
font-family:宋体;
mso-generic-font-family:auto;
mso-font-charset:134;
mso-char-type:none;
display:none;}
-->
</style>
<!--[if gte mso 9]><xml>
<x:ExcelWorkbook>
<x:ExcelWorksheets>
<x:ExcelWorksheet>
<x:Name>users</x:Name>
<x:WorksheetOptions>
<x:DefaultRowHeight>285</x:DefaultRowHeight>
<x:Selected/>
<x:DoNotDisplayGridlines/>
<x:Panes>
<x:Pane>
<x:Number>3</x:Number>
<x:ActiveRow>8</x:ActiveRow>
<x:ActiveCol>3</x:ActiveCol>
</x:Pane>
</x:Panes>
<x:ProtectContents>False</x:ProtectContents>
<x:ProtectObjects>False</x:ProtectObjects>
<x:ProtectScenarios>False</x:ProtectScenarios>
</x:WorksheetOptions>
</x:ExcelWorksheet>
</x:ExcelWorksheets>
<x:WindowHeight>6585</x:WindowHeight>
<x:WindowWidth>11505</x:WindowWidth>
<x:WindowTopX>360</x:WindowTopX>
<x:WindowTopY>285</x:WindowTopY>
<x:ProtectStructure>False</x:ProtectStructure>
<x:ProtectWindows>False</x:ProtectWindows>
</x:ExcelWorkbook>
</xml><![endif]-->
</head>
<body>
<!--下列信息由 Microsoft Excel 的“发布为 Web 页”向导生成。-->
<!--如果同一条目从 Excel 中重新发布,则所有位于 DIV 标记之间的信息均将被替换。-->
<!----------------------------->
<!--“从 EXCEL 发布 Web 页”向导开始-->
<!----------------------------->
<table x:str border=0 cellpadding=0 cellspacing=0 width=673 style='border-collapse:
collapse;table-layout:fixed;width:506pt'>
<col width=72 style='width:54pt'>
<col width=85 style='mso-width-source:userset;mso-width-alt:2720;width:64pt'>
<col width=165 style='mso-width-source:userset;mso-width-alt:5280;width:124pt'>
<col width=125 span=2 style='mso-width-source:userset;mso-width-alt:4000;
width:94pt'>
<col width=101 style='mso-width-source:userset;mso-width-alt:3232;width:76pt'>
<tr height=19 style='mso-height-source:userset;height:14.25pt'>
<td height=19 class=xl24 width=72 style='height:14.25pt;width:54pt'></td>
<td class=xl25 width=85 style='width:64pt'>名称</td>
<td class=xl25 width=165 style='border-left:none;width:124pt'>地址</td>
<td class=xl26 width=125 style='border-left:none;width:94pt'>Email</td>
<td class=xl25 width=125 style='border-left:none;width:94pt'>主页</td>
<td class=xl26 width=101 style='border-left:none;width:76pt'>QQ</td>
</tr>
<?
foreach($_POST["data"] as $var){
extract($var);
?>
<tr height=19 style='mso-height-source:userset;height:14.25pt'>
<td height=19 class=xl24 style='height:14.25pt'></td>
<td class=xl27 style='border-top:none'><?=$name?></td>
<td class=xl27 style='border-top:none;border-left:none'><?=$add?></td>
<td class=xl27 style='border-top:none;border-left:none'><?=$email?></td>
<td class=xl27 style='border-top:none;border-left:none'><?=$page?></td>
<td class=xl27 style='border-top:none;border-left:none'><?=$qq?></td>
</tr>
<?
}
?>
<!----------------------------->
<!--“从 EXCEL 发布 Web 页”向导结束-->
<!----------------------------->
<![if supportMisalignedColumns]>
<tr height=0 style='display:none'>
<td width=72 style='width:54pt'></td>
<td width=85 style='width:64pt'></td>
<td width=165 style='width:124pt'></td>
<td width=125 style='width:94pt'></td>
<td width=125 style='width:94pt'></td>
<td width=101 style='width:76pt'></td>
</tr>
<![endif]>
</table>
</body>
</html>
<?
$output=ob_get_contents();
ob_end_clean();
header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length: ".strlen($output));
Header("Content-Disposition: attachment; filename=users.xls");
echo $output;
?>