如何在php中修复excel中mysql表的下载?

I have a PHP file admindashboardentry.php which shows entries of mysql table in a database, also has functionality that admin can modify or download (using download.php) the entries in excel.

My problem is when I click to download I am not able to download it, it gives me below error with the table shown on the screen.

Warning: Header may not contain more than a single header, new line 
detected in /storage/ssd4/739/9447739/public_html/download.php on line 182

Warning: Cannot modify header information - headers already sent by 
(output started at /storage/ssd4/739/9447739/public_html/download.php:182) in /storage/ssd4/739/9447739/public_html/download.php on line 183

[Table is shown which I need in excel to be downloaded in client system]

I have the below code for admindashboardentry.php which calls download.php.

if(isset($_POST['empidcheck']) and isset($_POST['projectcodecheck']) and isset($_POST['startdatecheck']) and isset($_POST['enddatecheck'])){


    $empid = $_POST['empid'];
    $projectcode = $_POST['projectcode'];
    $startdate = $_POST['startdate'];
    $enddate = $_POST['enddate'];

    //to make database connection
    include('connection.php');  

    echo "<p>Admin Entry Received: $empid, $projectcode, $startdate, $enddate</p>";

    //This calls the download file
    echo "<form action = 'download.php' method = 'post'>";
    echo "<input type ='hidden' name='empid' value= '$empid' >";
    echo "<input type ='hidden' name='projectcode' value= '$projectcode' >";
    echo "<input type ='hidden' name='startdate' value= '$startdate' >";
    echo "<input type ='hidden' name='enddate' value= '$enddate' >";
    echo "<button type='submit' name='allsetdownload' class='btn btn-primary'>Download</button>";
    echo "</form>";

    echo "<br>";

    echo "<p>"."*NB - Not Billable, *B+NI - Billable + Not Invoiced, *B+I - Billable + Invoiced"."<p>";

    $selectSql = "SELECT * FROM `mastertbl` WHERE EmpID = '$empid' AND ProjectCode LIKE CONCAT('%', '$projectcode', '%') AND Date BETWEEN '$startdate' AND '$enddate' ORDER BY Date DESC, ModifiedDate DESC";

    $result = mysqli_query($conn, $selectSql);

    $sumNoOfHours = 0.0;          

    displayTableHeading();

    $i=1;

    while($row = mysqli_fetch_array($result))
    {
        $sumNoOfHours = $sumNoOfHours + $row['NoOfHours'];
        displayTableRow($row, $i, $empid);

        $i++;
    }

    echo "
        <tr>
            <td colspan=10>&nbsp;</td>
            <td><input type='submit'/></td>


        </tr>
    </table>";

    echo "</form>";

    tableEnding($sumNoOfHours);

}

I have the below code from download.php as follows

function downloadExcel(){

if(isset($_POST['allsetdownload']))
{

// If condition to check all the checkbox is checked i.e. Empid, ProjectCode, StartDate, EndDate 
$download = 1;
return $download;

}

}

if(downloadExcel() === 1)
{

$empid = $_POST['empid'];
$projectcode = $_POST['projectcode'];
$startdate = $_POST['startdate'];
$enddate = $_POST['enddate'];

// Connection 
include('connection.php');

$date = date('Y-m-d h:i:s A');

$empName = getEmpNameById($empid);


$filename = $empName." - ".$date.".xls"; // File Name

// Download file

header("Content-Disposition: attachment; filename=\"$filename\"");
header("Content-Type: application/vnd.ms-excel");

$user_query = "SELECT * FROM `mastertbl` WHERE `EmpID` = '$empid' AND `ProjectCode` LIKE CONCAT('%', '$projectcode' ,'%') AND `Date` BETWEEN '$startdate' AND '$enddate'";

$result = $conn->query($user_query);

tableHeading();

//loop the query data to the table in same order as the headers
while ($row = $result->fetch_assoc()){

    $status = statusOfEntry($row['Status']);

    tableRow($row, $status);

}
echo '</table>';
}

plz help