How do i export mysql query output to excel?

Contents

  • 1 Introduction
  • 2 The problem
  • 3 Solution
  • 4 Download and Install MySQL ODBC Driver
  • 5 Download and Install ZappyShell Command Line Tools
  • 6 MySQL Excel Export – Single table
  • 7 MySQL Excel Export – Multiple tables
  • 8 MySQL Excel Export – Multiple tables (using pattern)
  • 9 MySQL Excel Export – SQL Query
  • 10 MySQL Excel Export – Split Files and Tabs
    • 10.1 MySQL Excel Export – Split By Row Count
    • 10.2 MySQL Excel Export – Split By File Size
    • 10.3 MySQL Excel Export – Split By Group By Column
    • 10.4 MySQL Excel Export – Split sheets By Group By Column
  • 11 Exporting Compressed Excel files
  • 12 Using date time place holders in Excel file name
  • 13 Conclusion
  • 14 Reference Links
    • 14.1 Share this:
    • 14.2 Related

Introduction

In this article we will learn how to use ZappyShell Command Line Tools for MySQL Excel Export from Table or Query result

The problem

There is no native way of Exporting MySQL Data to Excel. You can use some coding techniques or libraries but when things become complex you will run out of options quickly.

Solution

ZappyShell for DB can export your MySQL data to Excel, PDF, Excel, CSV in one line command. This is just single exe less than 5 MB.. Excel export allows you to export data in existing or new excel file. You can specify sheetname and Cell Location where you want to start writing data. Append mode can automatically add new data in the existing workbook.Download here. Now Lets check how to do this.

Download and Install MySQL ODBC Driver

To export data from MySQL we will need ODBC Driver. In this tutorial we have use MySQL ODBC 5.2 Driver but if you are using different driver then change connection string accordingly.

Download and Install MySQL ODBC Driver from below URL
http://dev.mysql.com/downloads/connector/odbc/

How do i export mysql query output to excel?

MySQL Excel Export – Download MySQL 5.2 ODBC Driver

How do i export mysql query output to excel?

MySQL Excel Export – Install MySQL ODBC Driver

Download and Install ZappyShell Command Line Tools

c:\tools>db.exe export customers --out c:\data\customers.xlsx --excel --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – Multiple tables

In the following example we are exporting 3 tables (seperated by vertical bar)
c:\tools>db.exe export customers|orders|products --out c:\data\customers.xlsx --excel --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – Multiple tables (using pattern)

In below example all tables where name staring with cust will be exported from sales schema.
c:\tools>db.exe export cust% --schema Sales --out c:\data\customers.xlsx --excel --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – SQL Query

In the following example we are exporting output of SQL Query to Excel file. If you have complex query with multi line then you can use Map file with one dataset node.c:\tools>db.exe export "select * from customers where country like 'US%'" --out c:\data\customers.xlsx --excel --overwrite --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – Split Files and Tabs

Sometimes you may have need to split large amount of data into multiple files. With ZappyShell command line you can split data three different ways. Lets check each example.

MySQL Excel Export – Split By Row Count

In below example we are exporting MySQL customer table to Excel files and splitting files with maximum 1000 records per file option.

c:\tools>db.exe export customers --out c:\data\cust.xlsx --split --splitrows 1000 --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – Split By File Size

In below example we are exporting MySQL customer table to Excel files and splitting files with maximum file size = 10MB option. You also use other size specifier like KB, GB, BYTES etc.

c:\tools>db.exe export customers --out c:\data\cust.xlsx --split --splitsize 10MB --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

MySQL Excel Export – Split By Group By Column

In this example we have exported customers in to separate file by country.

c:\tools>db.exe export customers --out c:\data\cust.xlsx --groupby Country --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

How do i export mysql query output to excel?

MySQL Excel Export – Table to Excel, Split files by Country Column

MySQL Excel Export – Split sheets By Group By Column

In this example we have exported customers into separate sheet by country.

c:\tools>db.exe export customers --out c:\data\cust.xlsx --groupby Country --excel-split-ontab --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

How do i export mysql query output to excel?

ZappyShell – Export MySQL to Excel Split on tabs

Exporting Compressed Excel files

If you want to output compressed Excel (GZIP format) then use below command.

c:\tools>db.exe export customers --out c:\data\cust.xlsx --gzip --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

Using date time place holders in Excel file name

If you want to use date time place holders in file name so it automatically uses specified date time parts use below command simply putting specifiers in file name anywhere.

c:\tools>db.exe export customers --out c:\data\cust_{{YYYY}}_{{MM}}_{{DD}}_{{HH24}}{{MI}}{{SS}}.xlsx --connstr "Driver={MySQL ODBC 5.2 ANSI Driver};Server=your_server_name_or_ip;Port=3306;Uid=user1;pwd=SomeP@ssword;Database=your_db_name"

Conclusion

In this article we have gone through few ways to use ZappyShell Command Line Tools to export data from MySQL to Excel. You can also use same command line to export in CSV, JSON, PDF format.

  • Download ZappyShell

Can we export SQL query results to Excel?

In Object Explorer, right-click the required database/table/view and click Export Data on the shortcut menu to invoke the export wizard. On its first page, Export format, select the preferred format: MS Excel (. xls) or MS Excel 2007 (. xlsx).

How do I export MySQL results?

Export Table into CSV Format Using MySQL Workbench.
Run the statement/query and get its result set..
Then, in the result panel, click "export recordset to an external file" option. The recordset is used for the result set..
Finally, a new dialog box will be displayed. Here, we need to provide a filename and its format..

How do I save SQL query output to a file?

To save MySQL query output into a text file, we can use the OUTFILE command.

How do I create a MySQL query in Excel?

Start Excel, click the Data tab. In the appeared ribbon, click From Other Sources, and then click From Microsoft Query. In the next dialog, choose the data source you want to connect to (e.g., using data source name - Devart ODBC MySQL). Uncheck Use the Query Wizard to Create/Edit Queries and click OK.