This article describes how to use MySQL to export data to Excel. Share with you for your reference, the details are as follows: It is very simple to export MySQL into an excel file. Just execute a command like this: select * from a table into outfile 'd:/filename.xls'; If you execute the above command on the server, it will be exported to the server's D: disk. If you execute it in the client command line mode, it will be exported to the client's D: disk. If you execute it in the client through POST mode, it will be exported to the server's D: disk. The prerequisite for successful import is that you have login permission, select permission, and file permission. If you do not have file permission, you will not be able to perform operations such as select....into outfile and load data infile.... As for adding a button that prompts you to save as an excel file, I think you may mean on the web page? If the operation of MySQL is generally blocked on the web page, MySQL is usually operated through server-side scripting languages (PHP, JSP, ASP, etc.). The following uses PHP and JavaScript as examples to save the database query results as xls (I have tried both methods and they are feasible): 1.PHP method: Call header("content-type:application/msexcel"); header("content-disposition:filename=file001.xls"); These two functions are placed before executing database query operations in the PHP file. Generally, after the database query operation is completed, the query results can be displayed to the client through functions such as PHP is a server-side script. Using the above method, the same data result is actually transmitted from the server to the client twice. The first time is used for display, and the second time is used to package it into an xls file for download. 2. JavaScript method: First, define a JS function in the <head> section of the page where the data is located: <head> <script language="javascript"> function xSaveAsxls( xResult , xFileName ){ var ow=window.open(); with(ow){ document.write(Result); document.execCommand('Saveas', true, xFileName); close(); } } </script> </head> Then put <div id=div_id> before the <table> that displays the data, and put </div> after </table>. Then make a button below: <input type="button" value="Save as Excel file" onclick=xSaveAsxls (document.all.div_id.innerHTML,'filename.xls')> That's it. This is displayed as "Save as Excel file". When you click it, a save dialog box will pop up. The saved content is the data result set in the xls format. JS is a browser-side script. This method is used to save the displayed results as xls. The data is only transmitted once from the server to the client, saving transmission resources. Another way is to consider passing the query result set from PHP to a JS array variable, which is brought to the client, and then calling FileObjectSystem to operate. This is just an idea and I have not put it into practice. Please point out any inappropriateness. If there is a garbled problem: you can use the following statement select convert(dname using gb2312),email from d_dealerinfo into outfile 'd:/d.xls'; Where dname is in Chinese Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL transaction operation skills", "MySQL stored procedure skills", "MySQL database lock related skills summary" and "MySQL common function summary" I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: jQuery plugin to achieve seamless carousel
>>: Zabbix's psk encryption combined with zabbix_get value
1|0MySQL (MariaDB) 1|11. Description MariaDB data...
Table of contents Causes of MySQL Table Fragmenta...
1. Force no line break and end with an ellipsis. C...
This article shares MYSQL logs and backup and res...
1. What is Parallax scrolling refers to the movem...
This article example shares the specific code for...
The function has been implemented a long time ago...
Detailed explanation of replace into example in m...
Configure Tomcat First install Tomcat Installing ...
Table of contents Vue2.x Usage Global Registratio...
1. Environmental Preparation 1.1 Basic Environmen...
Ubuntu Server 16.04 MySQL 8.0 installation and co...
Tomcat itself optimization Tomcat Memory Optimiza...
Recently, I needed to test the zoom video confere...
A simple example of how to use the three methods ...