Export MySQL query to Excel file

From LemonWiki共筆
Revision as of 14:51, 15 November 2021 by Unknown user (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Export MySQL query to Excel file (XLS or XLSX file)


One step conversion: ex MySQL query -> Excel file[edit]

Methods of exporting MySQL query -> XLS/XLSX file: PHP scripts or other MySQL client softwares e.g.

  • PHPExcel: PHP scripts
  • Toad for MySQL for Win Os windows.png
  • Full Convert Enterprise: allows easy copying of tables and data between 40 or so supported databases. It will create all the tables, copy all of your data, then create indexes and foreign keys. 30 days trial
  • $ Navicat GUI for Win Os windows.png & Mac icon_os_mac.png

Notice the limit of Excel XLS file format:

  1. "Total number of characters that a cell can contain: 32,767 characters"
    • Using MySQL SUBSTRING() function ex: SUBSTRING(string_column, 1, 32767) for EXCEL 97[1]-2007[2] (XLS 檔)
    • validate the count of characters: MySQL CHAR_LENGTH or Excel LEN functions
  2. "Length of formula contents: 8,192 characters" If the content start with the symbol = or -, Excel will consider the content is formula.
    • Using CONCAT ' symbol ex: CONCAT('\'', string_column) If the content is not formula.
  3. "Worksheet size: 1,048,576 rows":
    • Using LIMIT 1048576 for EXCEL 2007

integrated solution for Excel limit 1 ~ limit 3

  • SELECT SUBSTRING(IF(LEFT(string_column, 1) = '-' OR LEFT(string_column, 1) = '=', CONCAT('\'', string_column), string_column), 1, 32767) FROM <table> LIMIT 1048576


Two steps conversion ex: MySQL query -> CSV file -> Excel file[edit]

Step 1: Export the MySQL query -> CSV file[edit]


mysqldump: Got error: 1045: Access denied for user 'user_name'@'localhost' (using password: YES) when executing 'SELECT INTO OUTFILE'


  • Condition 1: If the rows count of MySQL query is larger than 65,535 rows. You can use MS Excel.
  • Condition 2: If the rows count of MySQL query is less than 65,535 rows. You can use LibreOffice Calc & MS Excel.
  • Condition 3: Replace the double quote with double double quote if the enclosure character is double quote.
  • Condition 4: (optional) Replace the return character with space character or other character e.g. (U+21B5). SQL query syntax:
REPLACE(REPLACE(REPLACE(column_name, '\r\n', ''), '\n', ''), '\r', '') AS column_name

Validation: Use the editor supported Regular expression,

  • Search ^[^"] in the csv files to search the rows which contains return symbols. The " symbol is the "text qualifier" what I specified before export the MySQL query.

Step 2: Convert CSV to XLSX/XLS file format.[edit]

Step 3: Data verification[edit]

  1. Verify the data rows count: Equal to the rows count of MySQL query. Not equal to 65,535 rows or less than the rows count of MySQL query.
  2. Verify the data column is in correct position
    • Icon_exclaim.gif If the CSV is not well-formatted, sometimes the data column after imported to Excel is in wrong position.
  3. Verify the rows count
    • If the data size is less than 65,535 rows, convert CSV to Excel/XLS by LibreOffice Calc is recommended.
    • If the data size is larger than 65,535 rows, Adjust the MySQL query to avoid the column value contains the
      • enclosure character e.g. Tab key
      • escape the enclosure character e.g. " become ""
      • return symbol to separate different data row.

Troubleshooting of technical issues[edit]

If it costs too much time to convert to Excel file[edit]

The important steps before generating CSV file. These steps reduce the problems when the CSV file was converted to Excel file.

  • Add delimiter character between different column ex: comma symbol (,) or tab symbol
  • Add enclosure character ex: double quote symbol (") or single quote symbol (')
  • Add escape character escape the column value if contains the enclosure character
  • Remove return symbol[3]:
    SELECT REPLACE(REPLACE(REPLACE(`my_column`, '\r\n', '   '), '\n', '   '), '\r', '   ') 

Data format in result set became string after exported as Excel file[edit]

  • Number data types in result set became string after exported as XLSX file [4]
  • Date time data types in result set became string after exported as XLSX file[5]

Database management tools[edit]

Database management tools

Microsoft Excel (XLS or XLSX) & OpenDocument Spreadsheet (ODS) file limits[edit]

Microsoft Excel#Microsoft Excel (XLS or XLSX) & OpenDocument Spreadsheet (ODS) file limits