In the previous article, we introduced how to import data from an Excel file using the phpoffice/phpspreadsheet library. Today, let’s look at how to export data from a table to an Excel file. Let’s get started!
For those who want to review how to import data from Excel, you can click this link to read the article How to Import Data from Excel with PHP using phpoffice/phpspreadsheet.
<script type="text/javascript" src="css_js_xlsx/xlsx.core.min.js"></script>
<script type="text/javascript" src="css_js_xlsx/FileSaver.min.js"></script>
<script type="text/javascript" src="css_js_xlsx/tableExport.js"></script>
<table id="id_table" >
<tr>
<td>>First name/td>
<td>>Last name/td>
<td>>Sex/td>
<td>>Mobile/td>
</tr>
<tr>
<td>Mr. A</td>
<td>AAA</td>
<td>Male</td>
<td>123456</td>
</tr>
<tr>
<td>Mr. B</td>
<td>BBB</td>
<td>Male</td>
<td>78910</td>
</tr>
<tr>
<td>Mr. C</td>
<td>CCC</td>
<td>Female</td>
<td>111213</td>
</tr>
<tr>
<td>Mr. D</td>
<td>DDD</td>
<td>Male</td>
<td>141516</td>
</tr>
<tr>
<td>Mr. E</td>
<td>EEE</td>
<td>Female</td>
<td>171819</td>
</tr>
</table>
<button onClick="doExport()" >Click to export to Excel file</button>
<script>
function doExport() {
$('#id_table').tableExport({
type: 'excel',
fileName: 'File_name_here',
date: {html: 'mm/dd/yyyy'},
mso: {
fileFormat: 'xlsx',
}
});
}
</script>
Now combine all parts of the code together.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Export Excel File Data with PHP</title>
<script> type="text/javascript" src="css_js_xlsx/xlsx.core.min.js"></script>
<script> type="text/javascript" src="css_js_xlsx/FileSaver.min.js"></script>
<script> type="text/javascript" src="css_js_xlsx/tableExport.js"></script>
</head>
<body>
<button onClick="doExport()" >Click to export to Excel file</button>
<table id="id_table" border="1" >
<tr>
<td>First name</td>
<td>Last name</td>
<td>Sex</td>
<td>Mobile</td>
</tr>
<tr>
<td>Mr. A</td>
<td>AAA</td>
<td>Male</td>
<td>123456</td>
</tr>
<tr>
<td>Mr. B</td>
<td>BBB</td>
<td>Male</td>
<td>78910</td>
</tr>
<tr>
<td>Mr. C</td>
<td>CCC</td>
<td>Female</td>
<td>111213</td>
</tr>
<tr>
<td>Mr. D</td>
<td>DDD</td>
<td>Male</td>
<td>141516</td>
</tr>
<tr>
<td>Mr. E</td>
<td>EEE</td>
<td>Female</td>
<td>171819</td>
</tr>
</table>
<script>>
function doExport() {
$('#id_table').tableExport({
type: 'excel',
fileName: 'File_name_here',
date: {html: 'mm/dd/yyyy'},
mso: {
fileFormat: 'xlsx',
}
});
}
</script>
</body>
</html>
The result on the screen will allow you to export the data to an Excel file by simply pressing the export button. Try using this method, and there are additional features for customization. Check the TableExport website for more details.
Touring Arte Museum Gangneung, South Korea (11/11/2024 19:00)
...Read moreThe history and legend of the origin of Phaya Krut (Garuda) (26/04/2025 15:08)
...Read moreHistory and Legend of Mae Nak Phra Khanong (26/04/2025 15:35)
...Read more