2011-06-29 34 views
0

所以,我有一些問題導出到excel表格。導出html表格以便下載excel與PHP問題

它已生成,但只出現第一組標題。

我有這樣的一個表:

<table>   
     <thead> 
      <tr> 
       <th colspan="5">Incoming</th> 
      </tr> 
      <tr> 
       <th>From</th> 
       <th>To</th> 
       <th>Date</th> 
       <th>Duration</th> 
       <th>Status</th> 
      </tr> 
     </thead> 
     <tbody> 
      <tr> 
       <td style="text-align: center;">942367233</td> 
       <td style="text-align: center;">-</td> 
       <td style="text-align: center;">15-06-2011 08:24</td> 
       <td style="text-align: center;">00:00</td> 
       <td style="text-align: center;">Abandoned</td> 
      </tr> 
      <tr> 
       <td style="text-align: center;">935761500</td> 
       <td style="text-align: center;">1956</td> 
       <td style="text-align: center;">15-06-2011 09:20</td> 
       <td style="text-align: center;">00:00</td> 
       <td style="text-align: center;">Answered</td> 
      </tr> 
      <tr> 
       <td style="text-align: center;">942367233</td> 
       <td style="text-align: center;">1957</td> 
       <td style="text-align: center;">15-06-2011 09:21</td> 
       <td style="text-align: center;">02:16</td> 
       <td style="text-align: center;">Answered</td> 
      </tr> 
     </tbody> 
</table> 

,右滿桌子形式

<form action="toexcel.php" method="post" target="_blank" 
     onsubmit=\'$("#datatodisplay").val($("#data").html())\'> 
     <input type="image" src="/images/icons/Floppy-48x48.png" width="12" height="12"> 
     <input type="hidden" id="datatodisplay" name="datatodisplay" /> 
     </form> 

,然後在其中它應該產生了Excel的PHP:

header("Content-type: application/vnd.ms-excel; name='excel'"); 
header("Content-Disposition: filename=ficheroExcel.xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
echo $_POST['datatodisplay']; 

但只顯示第一組標題。我試圖刪除標題,它也不工作。

怎麼了?

回答

0

儘管聊天,我能解決這個問題。看來,轉義字符沒有在JavaScript中逃脫,所以我已經添加以下,解決這個問題:

echo str_replace('\"','"', $_POST['datatodisplay']);; 
0

您的HTML錯誤。在HTML DTD指出,一個表可能只包含一個THEAD和TFOOT一個,而是多個TBODY標籤:

<!ELEMENT table 
(caption?, (col*|colgroup*), thead?, tfoot?, (tbody+|tr+))> 

這是你的主要問題;該HTML無效。

除此之外:

  • 你告訴瀏覽器(以及Excel)中的HTML文件是.xls文件,這是完全錯誤的。試用.html。
  • 驗證與W3C HTML驗證表HTML頁面,並確保它驗證
  • 如果你不關心的佈局,使用CSV文件,而不是HTML文件
+0

我不知道我已經做的,但固定THEAD部分後,問題堅持。 PHP腳本應該生成一個.xls文件。我之前完成了這個工作,它工作。我不知道爲什麼現在不工作。 –

+0

HTML驗證嗎? – cweiske

+0

是的,除了未指明的文檔類型。 –