![]() But now, instead of making it downloadable, we will save the file in the directory and then send it as an attachment. The previous code download Excel/CSV file automatically. ![]() If you are going with the Gmail SMTP server read our article Send Email Using Gmail SMTP Server from PHP Script which explains the configuration needed for it. You may use your hosting SMTP server, AWS SES, or Gmail SMTP server. Let’s see how to achieve it.įirst, install the PHPMailer library using the command: composer require phpmailer/phpmailerĪfter installing the library, you can use any SMTP server to send an email. Some users may want to send the exported file as an attachment in the email. We have seen how to download the file with data in Excel/CSV format. $Excel_writer->save('php://output') Send an Email with File as an Attachment Header('Content-Type: application/text-csv') If someone is looking to export data in a CSV file then you need to change a few lines in the above code.Įxport-to-csv.php setActiveSheetIndex(0) In the previous section, we exported data to an Excel file. When you run this code on the browser, an Excel file will be downloaded automatically and the Excel sheet will have the following entries.Įxport MySQL Database Data to CSV Using PHP Header('Content-Disposition: attachment filename='. Header('Content-Type: application/vnd.ms-excel') $activeSheet->setCellValue('C'.$i, $row) $activeSheet->setCellValue('B'.$i, $row) ![]() $activeSheet->setCellValue('A'.$i, $row) $query = $db->query("SELECT * FROM products") $activeSheet->setCellValue('C1', 'Product Price') $activeSheet->setCellValue('B1', 'Product SKU') $activeSheet->setCellValue('A1', 'Product Name') $activeSheet = $spreadsheet->getActiveSheet() In this file, I will write a SQL query to fetch data from the database, write this data to Excel, and finally send the Excel file to the browser to download automatically.Įxport-to-excel.php setActiveSheetIndex(0) Now you can go ahead and write the actual code which will export an Excel file with data filled in it.Ĭreate export-to-excel.php file in the root directory. You have installed the library and also have a database table with few entries. Export MySQL Database Data to Excel Using PHP In the next part of the tutorial, I am allocating a separate section for exporting data to Excel, exporting data to CSV, and sending the email with an exported file as an attachment. This file will be included later to fetch records from the ‘products’ table. Make sure to replace the placeholders with the actual values in the above code. INSERT INTO `products` (`product_name`, `product_sku`, `product_price`) VALUESĪfter this create a config.php file where we write the code for database connection.ĭie("Unable to connect database: ". Next, add some dummy entries in the table so you will see a few records in the exported file. As an example, I am creating the ‘products’ table by running the below SQL query. For this, we require a few entries in our database. Our end goal is to export database table records to the Excel/CSV file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |