隨筆-16  評論-8  文章-30  trackbacks-0

          <?   //生成EXCEL文件
                 header("Content-type:application/vnd.ms-excel");
                 header("Content-Disposition:filename=test.xls");

                 echo "test1\t";
                 echo "test2\t\n";
                 echo "test1\t";
                 echo "test2\t\n";
                 echo "test1\t";
                 echo "test2\t\n";
                 echo "test1\t";
                 echo "test2\t\n";
                 echo "test1\t";
                 echo "test2\t\n";
                 echo "test1\t";
                 echo "test2\t\n";
          ?>

          //改動相應文件頭就可以輸出.doc .xls等文件格式了

          在php環境運行上面的代碼,大家就可以看到瀏覽器詢問用戶是否下載excel文檔,點擊保存,硬盤上就多了一個excel的文件,使用excel打開就會看到最終的結果,怎么樣不錯吧。

            其實在做真正的應用的時候,大家可以將數據從數據庫中取出,然后按照每一列數據結束后加\t,每一行數據結束后加\n的方法echo出來,在php的開頭用header("Content-type:application/vnd.ms-excel");表示輸出的是excel文件,用header("Content-Disposition:filename=test.xls");表示輸出的文件名為text.xls。這樣就ok了。

            我們更可以修改header讓他輸出更多格式的文件,這樣php在處理各種類型文件方面就更加方便了


          哈哈,托你的福,我已經知道怎么解決了,你把下面著段代碼存為.xls文件看看,知道該怎么做了吧?


            <html xmlns:o="urn:schemas-microsoft-com:office:office"
                  xmlns:x="urn:schemas-microsoft-com:office:excel"
                  xmlns="  <head>
                  <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
                  <meta http-equiv=Content-Type content="text/html; charset=iso-8859-1">
                  <!--[if gte mso 9]><xml>
                  <x:ExcelWorkbook>
                  <x:ExcelWorksheets>
                          <x:ExcelWorksheet>
                          <x:Name></x:Name>
                          <x:WorksheetOptions>
                                  <x:DisplayGridlines/>
                          </x:WorksheetOptions>
                          </x:ExcelWorksheet>
                  </x:ExcelWorksheets>
                  </x:ExcelWorkbook>
                  </xml><![endif]-->

            </head>

          <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr><td align="center" class="big6" >Users (except the ones disabled)</td></tr>
           
          <tr><td class="greyborder">
            <table border="1" align="center" width="100%" cellpadding="1" cellspacing="1">
            <tr align="center">
                <td class="TableTopHeader" nowrap>
                Login   </td>
             <td class="TableTopHeader" nowrap>
                Name   </td>
             <td class="TableTopHeader" width="3%" nowrap>
                EMail   </td>
             <td class="TableTopHeader" nowrap>
              Department   </td>
             <td class="TableTopHeader" nowrap>
              Position   </td>
             <td class="TableTopHeader" nowrap width="1%">
               Level   </td>
             <td class="TableTopHeader">Address</td>
             <td class="TableTopHeader">Phone</td>
          </tr>
              <tr  class="even">
              
                <td class="mtlist">&nbsp;joesen</td>
                <td class="mtlist">&nbsp;hong joesen</td>
                <td class="mtlist" >allfu@163.net</td>
                <td class="mtlist">&nbsp;Software developments</td>
                <td class="mtlist">&nbsp;System Administrator</td>
                <td class="mtlist">&nbsp;Admin</td>
              
               
                <td class="mtlist">&nbsp;</td>
                <td class="mtlist">&nbsp;</td>
            </tr>
            </table>
          </td></tr> 
          </table>

          主站蜘蛛池模板: 麻栗坡县| 萝北县| 九龙城区| 舒兰市| 时尚| 获嘉县| 大关县| 彰化县| 博野县| 呼伦贝尔市| 新化县| 石泉县| 达州市| 陆丰市| 定安县| 慈利县| 临汾市| 寻甸| 兴文县| 宁化县| 泽州县| 工布江达县| 潢川县| 沙湾县| 信宜市| 吉林省| 崇阳县| 丽江市| 阿拉善右旗| 平昌县| 彭阳县| 三亚市| 丘北县| 房山区| 固阳县| 辽宁省| 宁海县| 寿光市| 武鸣县| 宁国市| 乌海市|