«
用php实现生成Excel文件范例(1):服务端

时间:2008-5-31    作者:Deri    分类: 分享


   <code><?php<br />  ob_start();<br />?><br /><html xmlns:o="urn:schemas-microsoft-com:office:office"<br />xmlns:x="urn:schemas-microsoft-com:office:excel"<br />xmlns="http://www.w3.org/TR/REC-html40"><br /><head><br /><meta http-equiv=Content-Type content="text/html; charset=gb2312"><br /><meta name=ProgId content=Excel.Sheet><br /><meta name=Generator content="Microsoft Excel 10"><br /><link rel=File-List href="Page.files/filelist.xml"><br /><style id="Book1_10519_Styles"><br /><!--table<br />  {mso-displayed-decimal-separator:"\.";<br />  mso-displayed-thousand-separator:"\,";}<br />.font510519<br />  {color:windowtext;<br />  font-size:9.0pt;<br />  font-weight:400;<br />  font-style:normal;<br />  text-decoration:none;<br />  font-family:宋体;<br />  mso-generic-font-family:auto;<br />  mso-font-charset:134;}<br />.xl1510519<br />  {padding-top:1px;<br />  padding-right:1px;<br />  padding-left:1px;<br />  mso-ignore:padding;<br />  color:windowtext;<br />  font-size:12.0pt;<br />  font-weight:400;<br />  font-style:normal;<br />  text-decoration:none;<br />  font-family:宋体;<br />  mso-generic-font-family:auto;<br />  mso-font-charset:134;<br />  mso-number-format:General;<br />  text-align:general;<br />  vertical-align:middle;<br />  mso-background-source:auto;<br />  mso-pattern:auto;<br />  white-space:nowrap;}<br />.xl2210519<br />  {padding-top:1px;<br />  padding-right:1px;<br />  padding-left:1px;<br />  mso-ignore:padding;<br />  color:windowtext;<br />  font-size:12.0pt;<br />  font-weight:400;<br />  font-style:normal;<br />  text-decoration:none;<br />  font-family:宋体;<br />  mso-generic-font-family:auto;<br />  mso-font-charset:134;<br />  mso-number-format:General;<br />  text-align:general;<br />  vertical-align:middle;<br />  border:.5pt solid windowtext;<br />  background:silver;<br />  mso-pattern:auto none;<br />  white-space:nowrap;}<br />.xl2310519<br />  {padding-top:1px;<br />  padding-right:1px;<br />  padding-left:1px;<br />  mso-ignore:padding;<br />  color:windowtext;<br />  font-size:12.0pt;<br />  font-weight:400;<br />  font-style:normal;<br />  text-decoration:none;<br />  font-family:宋体;<br />  mso-generic-font-family:auto;<br />  mso-font-charset:134;<br />  mso-number-format:General;<br />  text-align:general;<br />  vertical-align:middle;<br />  border:.5pt solid windowtext;<br />  mso-background-source:auto;<br />  mso-pattern:auto;<br />  white-space:nowrap;}<br />ruby<br />  {ruby-align:left;}<br />rt<br />  {color:windowtext;<br />  font-size:9.0pt;<br />  font-weight:400;<br />  font-style:normal;<br />  text-decoration:none;<br />  font-family:宋体;<br />  mso-generic-font-family:auto;<br />  mso-font-charset:134;<br />  mso-char-type:none;}<br />--><br /></style><br /></head><br /><body><br /><!--[if !excel]>  <![endif]--><br /><!--下列信息由 Microsoft Excel 的“发布为 Web 页”向导生成。--><br /><!--如果同一条目从 Excel 中重新发布,则所有位于 DIV 标记之间的信息均将被替换。--><br /><!-----------------------------><br /><!--“从 EXCEL 发布 Web 页”向导开始--><br /><!-----------------------------><br /><div id="Book1_10519" align=center x:publishsource="Excel"><br /><?php<br />  if(false==isset($_POST["data"]))<br />  {<br />    echo  "没有要显示的数据"  ;<br />    exit;<br />  }<br />?><br /><table x:str border=0 cellpadding=0 cellspacing=0 width=519 style='border-collapse:<br />collapse;table-layout:fixed;width:389pt'><br /><col width=72 span=2 style='width:54pt'><br /><col width=163 style='mso-width-source:userset;mso-width-alt:5216;width:122pt'><br /><col width=87 style='mso-width-source:userset;mso-width-alt:2784;width:65pt'><br /><col width=125 style='mso-width-source:userset;mso-width-alt:4000;width:94pt'><br /><tr height=19 style='height:14.25pt'><br /> <td height=19 class=xl1510519 width=72 style='height:14.25pt;width:54pt'></td><br /> <td class=xl1510519 width=72 style='width:54pt'></td><br /> <td class=xl1510519 width=163 style='width:122pt'></td><br /> <td class=xl1510519 width=87 style='width:65pt'></td><br /> <td class=xl1510519 width=125 style='width:94pt'></td><br /></tr><br /><tr height=19 style='height:14.25pt'><br /> <td height=19 class=xl1510519 style='height:14.25pt'></td><br /> <td class=xl2210519>名称</td><br /> <td class=xl2210519 style='border-left:none'>地址</td><br /> <td class=xl2210519 style='border-left:none'>QQ号</td><br /> <td class=xl2210519 style='border-left:none'>电子邮件</td><br /></tr><br /><?php<br />  foreach($_POST["data"] as $val)<br />  {<br />    extract($val)  ;<br />?><br /><tr height=19 style='height:14.25pt'><br /> <td height=19 class=xl1510519 style='height:14.25pt'></td><br /> <td class=xl2310519 style='border-top:none'><?php echo $username ; ?></td><br /> <td class=xl2310519 style='border-top:none;border-left:none'><?php echo $add ; ?></td><br /> <td class=xl2310519 style='border-top:none;border-left:none'><?php echo $qq ; ?> </td><br /> <td class=xl2310519 style='border-top:none;border-left:none'><?php echo $email ; ?></td><br /></tr><br /><?php<br />  }<br />?><br /><![if supportMisalignedColumns]><br /><tr height=0 style='display:none'><br /> <td width=72 style='width:54pt'></td><br /> <td width=72 style='width:54pt'></td><br /> <td width=163 style='width:122pt'></td><br /> <td width=87 style='width:65pt'></td><br /> <td width=125 style='width:94pt'></td><br /></tr><br /><![endif]><br /></table><br /></div><br /><!-----------------------------><br /><!--“从 EXCEL 发布 Web 页”向导结束--><br /><!-----------------------------><br /></body><br /></html><br /><?php<br />  $outStr=ob_get_contents();<br />  ob_end_clean();<br />  &#160;<br />    header("Content-type: application/octet-stream");<br />    Header("Accept-Ranges: bytes");<br />    Header("Accept-Length: ".strlen($outStr));<br />    Header("Content-Disposition: attachment; filename=test.Xls");<br />    // 输出文件内容    &#160;<br />    echo $outStr  ;<br />?></code></p>