0
<?php
if(isset($_POST['download']))
{
    include_once("dbconnect.php");
    $records = mysqli_query($con,"select * from curricular");
        $delimiter = "\t";
        $filename = "Student-Data_".date('Y-m-d').".xls";

        $f = fopen('php://memory','w');

        $fields = array("ID","NAME");
        fputcsv($f,$fields,$delimiter);

        while($data = mysqli_fetch_array($records))
        {
            $output=array($data['ID'],$data['name']);
            fputcsv($f,$output,$delimiter);
        }

        fseek($f,0);
        
        header('Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment; filename="' . $filename . '";'); 
        
        //header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        //header('Content-Disposition: attachment; filename="' . $filename . '";'); 
        fpassthru($f);
    
    exit;
}
?>

I want to transfer data from MySQL to the Excel but I am not able to set the column and row width of the excel being exported. Please Help.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • 1
    You are _not_ creating an actual Excel sheet here, you are creating a mere CSV file. – CBroe Jan 12 '22 at 15:28
  • Or a `csv` file in a `.xls` container with a excel header the browser interprets, but not an excel file. Might want to look at https://stackoverflow.com/questions/37958282/how-to-generate-an-xlsx-using-php – user3783243 Jan 12 '22 at 15:34

0 Answers0