Categories

Friday, April 19, 2024
#919814419350 therichposts@gmail.com
ExcelSheetPhp

How to save php mysql data in Excel sheet?

Save php mysql data in Excel sheet

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

2 Comments

    • 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

Leave a Reply

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