php + download to excel --> worksheet name
<p>I am currently exporting data from php to excel using the code as below:</p>

<pre><code>include("dbconnect.php");

$query = $_POST['query'];
$result = odbc_exec($conn,$query);

$count = odbc_num_fields($result);
//Define Variable For ODBC
$data = "";
//Field Name Data
for ($i = 1; $i &lt;= $count; $i++)
    {
        $data .= odbc_field_name($result, $i)."t";
    }

$data .= "n";    
//Row Data
while(odbc_fetch_row($result))
    {
        for ($j = 1; $j &lt;= $count; $j++)
            {
                $data .= odbc_result($result, $j)."t";
            }
        $data .= "n";
    }

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header("Pragma: no-cache");
header("Expires: 0");
echo $data;   
odbc_close($conn);
</code></pre>

<p>This all works fine, but the generated excel file has a sheetname of: ".xls]ExcelFile(1)" , and when you try to rename the sheet it causes an error in excel (unless you save the file first).</p>

<p>How can I define the sheetname in my php file?</p>

<p>Thanks in advance!</p>

<p>Have a nice weekend:-)</p>
