Chapter 5 Import MySQL Data into Excel

Table of Contents

5.1 Choosing Columns To Export
5.2 Importing a Table
5.3 Import: Advanced Options
5.4 Importing a View or Procedure
5.5 Adding Summary Fields
5.6 Creating PivotTables

Data can be imported from MySQL into a Microsoft Excel spreadsheet by using the Import MySQL Data option after selecting either a table, view, or procedure to import.

5.1 Choosing Columns To Export

By default, all columns are selected and will be imported. Specific columns may be selected (or unselected) using the standard Microsoft Windows method of either Control + Mouse click to toggle the selection of individual columns, or Shift + Mouse click to select a range of columns.

The background color of a column shows the status of each column. The color white means that the column has been selected, and therefore it will be imported. Conversely, a gray background means that the column will not be imported.

Right-clicking anywhere in the preview grid opens a context-menu with either a Select None or Select All option, depending on the current status.

5.2 Importing a Table

The dialog while importing a table includes the following options:

  • [ ] Include Column Names as Headers: Enabled by default, this inserts the column names at the top of the Microsoft Excel spreadsheet as a "headers" row.

  • [ ] Limit to ___ Rows and Start with Row ___: Disabled by default, this limits the range of imported data. The Limit to option defaults to 1, and defines the number of rows to import. The Start with Row option defaults to 1 (the first row), and defines where the import begins. Each option has a maximum value of COUNT(rows) in the table.

  • [ ] Add Summary Fields: Disabled by default, this option adds a summary field to each column. For additional information, see Section 5.5, “Adding Summary Fields”.

5.3 Import: Advanced Options

Figure 5.1 Importing table data with MySQL for Excel: Advanced options

Importing table data with MySQL for Excel: Advanced options

General Options:

  • Use the first [10] rows to preview the MySQL tables data. This affects the preview step in the import process, and defaults to 10.

    Figure 5.2 MySQL for Excel: Preview

    MySQL for Excel: Preview

  • [ ] Escape text values that start with "=" so Excel does not treat them as formulas, and is enabled by default. This option may not reflect any differences in the preview because it is only applied after the data is imported into the Excel Worksheet.

    Figure 5.3 MySQL for Excel: Escape "=" (formulas)

    MySQL for Excel: Escape "=" (formulas)

Excel Table Options:

  • [ ] Create an Excel table for the imported MySQL table data. Enabled by default.

  • Use style [ ] for the new Excel table. Defaults to MySqlDefault.

  • [ ] Prefix Excel tables with the following text: _______. Disabled by default.

Importing a table displays a dialog similar to the following:

Figure 5.4 Importing table data with MySQL for Excel

Importing table data with MySQL for Excel

5.4 Importing a View or Procedure

Importing a view or procedure displays a similar dialogue, but with the following options:

  • Include Column Names as Headers: Enabled by default, this will insert the column names at the top of the Excel spreadsheet as a "headers" row.

  • Import: Because a procedure might return multiple result sets, the import options include:

    • Selected Result Set: Imports the selected tab sheet. This is the default behavior.

    • All Result Sets - Arranged Horizontally: Imports all result sets into the Excel Worksheet horizontally, and inserts one empty column between each result set.

    • All Result Sets - Arranged Vertically: Imports all result sets into the Excel Worksheet vertically, and inserts one empty row between each result set.

For example, a dialogue like the following is displayed after importing a procedure and pressing the Call button to invoke the stored procedure:

Figure 5.5 Importing called stored procedure data with MySQL for Excel

Importing called stored procedure data with MySQL for Excel

5.5 Adding Summary Fields

Summary fields are calculated fields, and this option adds summary related functions to each of the imported columns. These fields are added to the last row of the imported table data, and the dropdown of choices includes Average, Sum, Min, and Max.

Note

This feature was added in MySQL for Excel 1.3.0.

The Add Summary Fields option (disabled by default) is listed on the import dialog:

Figure 5.6 The 'Add Summary Fields' option

The 'Add Summary Fields' option

Enabling this option adds a row of summary fields for the appropriate columns in your imported data. Notice the newly created row on the bottom:

Figure 5.7 The new 'Add Summary Fields': the new row

The new 'Add Summary Fields': the new row

Select the row to reveal a down arrow, and click it to display a set of summary options:

Figure 5.8 The 'Add Summary Fields' row: choices

The 'Add Summary Fields' row: choices

For example, choosing Sum:

Figure 5.9 The 'Add Summary Fields' row: sum example

The 'Add Summary Fields' row: sum example

Adjust each summary field accordingly.

5.6 Creating PivotTables

A PivotTable can be created from imported MySQL tables, views, stored procedures, or the entire Excel Data Model.

Note

This feature was added in MySQL for Excel 1.3.0.

An Excel PivotTable report summarizes and provides a visual representation of data in many different ways. It is a native Excel feature, see PivotTable reports 101 for additional information about Excel PivotTables.

Our example covers a simple use case where an empty PivotTable is created from an imported MySQL table. This example uses the "film" table of the "Sakila" database. To create the PivotTable, select the "film" table from the database object's selection panel and then click Import MySQL Data. On the Import Data dialog, check the Create a PivotTable before pressing OK to execute the operation.

Figure 5.10 Option: Create a PivotTable with the imported data

Option: Create a PivotTable with the imported data

When the Create a PivotTable with the imported data option is checked, an empty PivotTable (or a PivotTable placeholder) is inserted just to the right of the imported data. The PivotTable name follows the same naming rules used for Excel tables created from tje imported data, but PivotTables can be created with or without enabling the Create an Excel table for the imported MySQL data advanced option. That means a PivotTable can be created from an imported Excel range (if the aforementioned advanced option is off), or from an imported Excel table (if the option is on).

Click Import to dump the film table's data to an active Excel Spreadsheet, and this also creates a PivotTable for that data.

Figure 5.11 PivotTable Example: Empty PivotTable

PivotTable Example: Empty PivotTable

Clicking the PivotTable opens a PivotTable Fields panel to next to the MySQL for Excel panel, and from here you can select fields you want to summarize in the PivotTable report. Drag and drop fields from the list to any of the FILTERS, COLUMNS, ROWS, or VALUES areas, depending on the visualizations you want in the report. The report is completely dynamic, meaning that you can change the views by moving fields around the areas until you see the visualization you need for your PivotTable report.

Below is an example PivotTable report using the sakila.film table we imported in our previous example. This report includes a filter by release_year, and it summarizes the rental_rate values while also grouping the data by values in the rating column.

Figure 5.12 PivotTable Example: Film Ratings

PivotTable Example: Film Ratings

Expanding one of the groups reveals its values from the title and description columns.

Figure 5.13 PivotTable Example: Expanded Group

PivotTable Example: Expanded Group

We can also do this with data coming from a MySQL view or stored procedures. The only difference is that for stored procedures we can create a PivotTable for each of the imported result sets returned by the procedure's call. Take the following screenshot as an example where we have the film_in_stock stored procedure selected, we configured its input parameter values, and we called the procedure. You can see the procedure returned one result set (Result1) and the OutAndReturnValues table (always present if the procedure has output parameters or a return value).

Figure 5.14 PivotTable Example: Stored Procedure

PivotTable Example: Stored Procedure

In our example, we selected to import All Result Sets - Arranged Horizontally. Because the Create a PivotTable with the imported data option was also checked, a PivotTable was created for each returned result set.

Figure 5.15 PivotTable Example: Arranged Horizontal

PivotTable Example: Arranged Horizontal

An important use case for PivotTables is when we create it for multiple related tables as typically a single table does not contain all of the data needed by a PivotTables report. You can create a single PivotTable tied to the data in the current Excel Data Model that contains fields from several related tables. That way you can use the data in a single report for an entire MySQL schema if needed. However, you can only do this in Excel 2013 (and later) where the Excel Data Model is available.

In Excel versions before Excel 2013, only a PivotTable for each imported table or view can be created. This is because a single PivotTable for the entire Excel Data Model requires that the tables are related to each other. If Excel relationships cannot be created, then this type of PivotTable cannot be created. So in these cases, the Import Data dialog looks like the following sample screenshot:

Figure 5.16 Disabled 'Create Excel relationships' option before Excel 2013

Disabled 'Create Excel relationships' option before Excel 2013

Clicking Why is this option disabled? displays an information dialog with an explanation of the disabled controls.

Figure 5.17 Disabled 'Create Excel relationships' option description

Disabled 'Create Excel relationships' option description

Our next example uses all tables in our schema. You can manually choose each table or use Control + A in the database objects list to select them all. When clicking Import Multiple Tables and Views, the Import Data dialog appears as shown below. We need to check the Create a PivotTable option, which by default its drop-down is set to for all the tables in the data model. Keep that value.

Figure 5.18 Importing All Tables and Views

Importing All Tables and Views

When clicking Import, the data in all of the selected tables is imported to Excel, its Data Model and Excel relationships are created, and a new worksheet is created that contains a PivotTable with all of the tables that were imported. This is demonstrated in the screenshot below, and notice that all tables are listed in the PivotTable Fields panel.

Figure 5.19 Importing All Tables and Views: Listing

Importing All Tables and Views: Listing

You can also configure the Create a PivotTable drop-down list the for each imported table or view, which in turn will create a PivotTable for each of the imported tables or views, as opposed to creating a single PivotTable for all of them.

Figure 5.20 Importing Each Imported Table or View

Importing For Each Imported Table or View