chriltola / mail

0 stars 0 forks source link

Re #24

Open chriltola opened 1 year ago

chriltola commented 1 year ago

Here is a sample PHP code that retrieves data from a database and exports it to an Excel file with fixed headers:

// Connect to database $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

// Query data from database $sql = "SELECT id, name, email, phone FROM users"; $result = $conn->query($sql);

// Create Excel file require_once 'PHPExcel/Classes/PHPExcel.php'; $objPHPExcel = new PHPExcel();

// Set headers $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID'); $objPHPExcel->getActiveSheet()->setCellValue('B1', 'Name'); $objPHPExcel->getActiveSheet()->setCellValue('C1', 'Email'); $objPHPExcel->getActiveSheet()->setCellValue('D1', 'Phone');

// Set data $i = 2; while ($row = $result->fetch_assoc()) { $objPHPExcel->getActiveSheet()->setCellValue('A'.$i, $row['id']); $objPHPExcel->getActiveSheet()->setCellValue('B'.$i, $row['name']); $objPHPExcel->getActiveSheet()->setCellValue('C'.$i, $row['email']); $objPHPExcel->getActiveSheet()->setCellValue('D'.$i, $row['phone']); $i++; }

// Set filename and header $filename = 'users.xlsx'; header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'"'); header('Cache-Control: max-age=0');

// Save file to output stream $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->save('php://output');

// Close database connection $conn->close(); ?>

Note: This code assumes that you have PHPExcel library installed in your project. You can download it from here: https://github.com/PHPOffice/PHPExcel

chriltola commented 1 year ago

request Campu Customer Service Department to inform province branch about update EPP as following schedule provided in attachment