Home ExcelSheet How to save php mysql data in Excel sheet?

How to save php mysql data in Excel sheet?

by therichpost
2 comments
Php Preg_Match Pattern for Week Days Name

Hello, welcome to therichpost.com. In this post, I will tell you How to save php mysql data in Excel sheet? I personally like php ver much. PHP is the best web language. There are many cms and frameworks in PHP and WordPress is the best example. facebook also used PHPin it.

In this post, I am getting data from php mysql user table and store that data in userdata.xlsx file with php.
You also need to download PHPExcel library from online.

Here is the working example and you need to put this code into your php file:

<?php
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’);
?>

There are so many coding tricks in PHP and i will let you know all. Please do comment if you any query related to this post. Thank you. Therichpost.com

You may also like

2 comments

majibullah January 24, 2018 - 8:10 pm

Sir, I want integrate fingerprint reader with school management system to auto update student and staff attendance
Please how would i do this ?

Reply
Ajay Malhotra January 25, 2018 - 6:14 pm

majibullah let me explain you the basic concept. For external devices to co-ordinates, you need a device driver in the PC and a software that co-ordinates with the device driver to receive input and send outputs to the device.

Thank you

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.