MySQL导出表格格式为xls

前言

现网现在流行一个技术为将mysql数据库中表格导出给excel去用,那么如何实现呢?

在数据库中导出:

mysql> select name,age from aaa.pp into outfile '/tmp/name.xls';
ERROR 1 (HY000): Can't create/write to file '/tmp/name.xls' (Errcode: 13 - Permission denied)
mysql> set global secure_file_priv='';		# 值为null时,不允许操作,所以应该想要将值改为空
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv | NULL  |
+------------------+-------+
mysql> set global secure_file_priv=''; 	# 想要通过此方式将secure_file_priv设置没有值
ERROR 1238 (HY000): Variable 'secure_file_priv' is a read only variable
[root@client opt]# vi /etc/my.cnf		# 进入主配置文件进行设置
[mysqld]
……
secure_file_priv=''		# 将值设为空
[root@client opt]# systemctl restart mysqld.service	# 重启服务
[root@client opt]# mysql -uroot -p123456		# 进入mysql数据库
mysql> show global variables like '%secure_file_priv%';	# 再次查看值,发现已经发生改变
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| secure_file_priv |       |
+------------------+-------+
mysql> select name,age from aaa.pp into outfile '/tmp/name.xls';		# 此时可以将数据进行导出
Query OK, 3 rows affected (0.01 sec)
[root@client tmp]# cat /tmp/name.xls 	# 查看导出的数据
zhang	18
zhao	21
qian	23

在外面进行数据的导出:

[root@client ~]# mysql -uroot -p123456 -e "select id,name from aaa.pp" > /opt/user1.xls		# 直接在外面进行数据的导出为xls格式
mysql: [Warning] Using a password on the command line interface can be insecure. 
[root@client ~]# cat /opt/user1.xls 
id	name
1	zhang
3	zhao
2	qian

猜你喜欢

转载自blog.csdn.net/weixin_50344820/article/details/111877328