Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following example would probably help you:</p> <pre><code> 1 &lt;?php 2 header('Content-type: application/excel'); 3 header('Content-Disposition: attachment; filename="test.xls"'); 4 ?&gt; 5 6 &lt;table&gt; 7 &lt;tr&gt;&lt;th&gt;Column 1&lt;/th&gt;&lt;th&gt;Column 2&lt;/th&gt;&lt;/tr&gt; 8 &lt;tr&gt;&lt;td style="font-size:200%"&gt;Answer 1&lt;/td&gt;&lt;td style="color:#f00"&gt;Answer 2&lt; /td&gt;&lt;/tr&gt; 9 &lt;tr&gt;&lt;td colspan="2" style="font-weight:bold"&gt;Answer 3 with 2 columns&lt;/td&gt;&lt;/t r&gt; 10 &lt;/table&gt; </code></pre> <p>Save this as a .php file on your server.</p> <p>What it does is: add headers to force the browser thing you're delivering an Excel file. And then returning a table inside of it. My Excel picks this up perfectly.</p> <p>Note: the sample HTML comes from the answer that you had already found. I have just added the headers to the beginning.</p> <p>Note: You just changed your question and added an example. Your example does NOT use html! You are delivering a TAB-delimited file where each line ends with a newline. </p> <p>The proper way to do this is to really generate HTML, ie, use a <code>&lt;table&gt;</code> at the beginning, use <code>&lt;tr&gt;</code> and <code>&lt;/tr&gt;</code> for each row, and put the fields in <code>&lt;td&gt;</code> tags, and then finish with the closing <code>&lt;/table&gt;</code>, so basically:</p> <pre><code>$data='&lt;table&gt;'; // excel content with overloaded terminals if (mssql_num_rows($resCollection)&gt;0) { while ($rowCollection = mssql_fetch_object($resCollection)) { $data.='&lt;tr&gt;'; $data .= '&lt;td&gt;'.$rowCollection-&gt;Name."&lt;/td&gt;&lt;td&gt;".$rowCollection-&gt;Address."&lt;/td&gt;&lt;td&gt;" .$rowCollection-&gt;TerminalNotes."&lt;/td&gt;&lt;td&gt;".$rowCollection-&gt;TerminalAmount ."&lt;/td&gt;&lt;td&gt;".$rowCollection-&gt;DayAmountAll."&lt;/td&gt;&lt;td&gt;" .$rowCollection-&gt;LastPaymentTime."&lt;/td&gt;"; $data.='&lt;/tr&gt;'; } } $data.='&lt;/table&gt;'; </code></pre> <p>Then change your last line to:</p> <pre><code>echo iconv('utf-8', 'cp1251', "$data"); </code></pre> <p>If that works I suggest you change the output to have <code>&lt;th&gt;</code> tags around your table headers, instead of the div that is in your example.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload