I am trying to generate a report in an Excel spreadsheet. I looked up and read about using PHPSpreadsheet
so, I downloaded and installed Composer in my XAMPP project directory in order to use it.
I got the code working far enough to generate the file as I need to. However, there is no data being obtained from the file other than the headers I define in the code. Here is an example:
First, here is my current code as it stands:
<?php
require_once('../../vendor/autoload.php');
require_once('../dbconnect.php');
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$excel_writer = new Xlsx($spreadsheet);
$spreadsheet->setActiveSheetIndex(0);
$active_sheet = $spreadsheet->getActiveSheet();
$active_sheet->setCellValue('A1', 'Precinct');
$active_sheet->setCellValue('B1', 'Member Name');
$active_sheet->setCellValue('C1', 'Residential Address');
$active_sheet->setCellValue('D1', 'Member Email');
$active_sheet->setCellValue('E1', 'Phone Number');
//$active_sheet->setCellValue('F1', 'Ethnicity');
//$active_sheet->setCellValue('G1', 'Gender');
//$active_sheet->setCellValue('H1', 'Party');
$sql = "SELECT precinct, name, residential_address, member_email, member_phone, present, alternate FROM attendance INNER JOIN members ON members.name = attendance.member_id WHERE present = 1 ORDER BY members.precinct";
$stmt = $pdo->prepare($sql);
$stmt->execute();
if($stmt->num_rows > 0) {
$i = 2;
while($row = $stmt->fetch()) {
$active_sheet->setCellValue('A' . $i, $row['precinct']);
$active_sheet->setCellValue('B' . $i, $row['member_name']);
$active_sheet->setCellValue('C' . $i, $row['residential_address']);
$active_sheet->setCellValue('D' . $i, $row['member_email']);
$active_sheet->setCellValue('E' . $i, $row['member_phone']);
//$active_sheet->setCellValue('A' . $i, $row['precinct']);
//$active_sheet->setCellValue('A' . $i, $row['precinct']);
//$active_sheet->setCellValue('A' . $i, $row['precinct']);
$i++;
}
}
$filename = date("Y") . ' ' . 'County Information Spreadsheets.xlsx';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $filename);
header('Cache-Control: max-age=0');
$excel_writer->save('php://output');
/*
$sql = "SELECT precinct, name, residential_address, member_email, member_phone, present, alternate FROM attendance INNER JOIN members ON members.id = attendance.member_id WHERE present = 1 ORDER BY members.precinct";
$stmt = $pdo->prepare($sql);
$stmt->execute();
*/
?>
My current table structures are as follows:
Members:
Attendance:
Am I doing something wrong?