Posted inMysql / Laravel / Mysqli / php / Programming / Technology

How to Export MySQL Database data to Excel in PHP MySQLi

Today, We want to share with you export to excel in php.In this post we will show you create excel file in php source code, hear for how to export mysql data to excel using php? we will give you demo and example for implement. In this post, we will learn about php export to excel xlsx with an example.

Database Data Export to Excel File using PHP

Learn to export to excel in php Database data export is a popular CRUD operation for many of us. here simple you can open MySQL phpMyAdmin is the go to choice for a database client in server side PHP. It supported database administration platforms as well as also allows exporting the product, users, employees or many more data. The exported all the data can be in different formats like as a SQL, CSV, text as selected.

When you need a PHP dynamics script, that can export database data (rows) to an excel file, then this article will help you. It has complete information step by step description with example full source code. It is compact with lightweight source code for php download excel file from server that can be latest module integrated in your PHP MySQL web applications.

We have already seen several example source code for implementing database export using PHP.

How to export MySQL data to excel sheet in PHP

index.php

 
 

Export MySQL data to Excel in PHP

In this simple bellow example I have learn step by step regarding how to export data to Excel in PHP. If you have developed any large web based project then that web application you have to need this service like Exporting Data to Excel, CSV or PDF Sheet. So I have developed this best Articles, in which I have create a simple PHP with MySQLi Script for Export Data from mysql database to Excel.
index.php


  
   
  Export MySQL data to Excel in PHP - www.pakainfo.com  
    
    
    
   
   
  



free code - Export MySQL data to Excel in PHP - www.pakainfo.com


'; } ?>
ProductName descrptions brand p Code price
'.$rowData["ProductName"].' '.$rowData["descrptions"].' '.$rowData["brand"].' '.$rowData["pcode"].' '.$rowData["price"].'

export-to-excel.php

 0)
 {
  $result .= '
   
  ';
  while($rowData = mysqli_fetch_array($setRec))
  {
   $result .= '
    
   ';
  }
  $result .= '
Name descrptions brand p Code price
'.$rowData["ProductName"].' '.$rowData["descrptions"].' '.$rowData["brand"].' '.$rowData["pcode"].' '.$rowData["price"].'
'; header('Content-Type: application/xls'); header('Content-Disposition: attachment; filename=download.xls'); echo $result; } } ?>
Web Programming Tutorials Example with Demo

how to export mysql data to excel using php?

To export MySQL data to an Excel file using PHP, you can use a combination of the mysqli extension for connecting to the MySQL database, the PHPExcel library for generating the Excel file, and the header function in PHP to send the appropriate headers to the browser. Here’s an example code snippet:

// Load the PHPExcel classes
require_once 'PHPExcel.php';
require_once 'PHPExcel/IOFactory.php';

// Create a new PHPExcel object
$objPHPExcel = new PHPExcel();

// Connect to the MySQL database
$mysqli = new mysqli('localhost', 'username', 'password', 'database');

// Select data from the MySQL database
$result = $mysqli->query('SELECT * FROM my_table');

// Loop through the data and add it to the PHPExcel object
$row = 1;
while ($data = $result->fetch_assoc()) {
    $col = 0;
    foreach ($data as $value) {
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
        $col++;
    }
    $row++;
}

// Set the headers for the Excel file
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="data.xlsx"');
header('Cache-Control: max-age=0');

// Write the PHPExcel object to a file
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

In the above code, replace username, password, database, and my_table with your own values. The script will connect to the MySQL database, select data from the specified table, loop through the data, and add it to a new PHPExcel object. It will then send the appropriate headers to the browser and write the PHPExcel object to a file, which will be downloaded by the user as an Excel file.

Note that the PHPExcel library has been deprecated in favor of the PhpSpreadsheet library, which provides similar functionality.

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about export to excel in php.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype