Here is code to add Php mysql data in Excel file:
require_once 'PHPExcel\Classes\PHPExcel.php';
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "oops");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
$objPHPExcel = new PHPExcel();
$row = 1;
while($subrow = $result->fetch_array())
{
$row++;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $subrow['name']);
$objPHPExcel->getActiveSheet()->setCellValue('c'.$row, $subrow['phone']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $subrow['email']);
}
$excelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$excelWriter->save('userdata.xlsx');
require_once 'PHPExcel\Classes\PHPExcel.php';
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = new mysqli($servername, $username, $password, "oops");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$sql = "SELECT * FROM user";
$result = $conn->query($sql);
$objPHPExcel = new PHPExcel();
$row = 1;
while($subrow = $result->fetch_array())
{
$row++;
$objPHPExcel->getActiveSheet()->setCellValue('A'.$row, $subrow['id']);
$objPHPExcel->getActiveSheet()->setCellValue('B'.$row, $subrow['name']);
$objPHPExcel->getActiveSheet()->setCellValue('c'.$row, $subrow['phone']);
$objPHPExcel->getActiveSheet()->setCellValue('D'.$row, $subrow['email']);
}
$excelWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$excelWriter->save('userdata.xlsx');
No comments:
Post a Comment