Exporting table to csv works in the admin but exports HTML data when used on the front end

Well this was a journey! The solution was ‘die’ and not exit AND use the add_action call that resulted in a notification PRIOR to any header call being called. The die forced the system to quit before attempting to write out the headers at the end of file.

The final code that works:

add_action( 'template_redirect', 'ShowResults', 10); 
function ShowResults($Name)
{
 global $post;
 $title = get_the_title($post);

if($title == "Export")
   OutputCSV();
}

function ExportCSV()
{
$rows = mmd_GetProducts($Name);

header('Pragma: public');
header( 'Expires: 0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv');
header( 'Content-Disposition: attachment; filename="'. 'Results.csv"');
$fp = fopen('php://output', 'w');

$header_row = array(
        0 => '#',
        1 => 'ID',
        2 => 'Product',
        3 => 'Price',
    );
fputcsv($fp, $header_row);

foreach($rows as $Record)
  { 
     if($Record['bDoNotDisplay'] == 1 )
        continue;
  
  $OutputRecord = array($cnt
                        $Record['id'], 
                        $Record['product'],  
                        $Record['price']);
    $cnt ++;
    fputcsv($fp, $OutputRecord);         
    }
unset($rows);

fclose( $fp );
die;                  <<<=========== die NOT exit.
}