-
-
Save 9nix00/d6cc9e18ce2606bc51213e0d4860a908 to your computer and use it in GitHub Desktop.
MySQL Workbench Plugin: Export Result Set As Excel Xlsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| # Export Result Set As Excel Xlsx File | |
| # | |
| # Usage: | |
| # 1. Install Python(2.7) Module "XlsxWriter", See http://xlsxwriter.readthedocs.org/getting_started.html | |
| # 2. In MySQL Workbench | |
| # Scripting => Install Plugin/Module... => <select this script> => <restart workbench> | |
| # | |
| # Author: zhwei | |
| # | |
| # MySQL Workbench Python script | |
| # Written in MySQL Workbench 6.2.5 | |
| import time | |
| from os.path import expanduser | |
| import grt | |
| from wb import * | |
| import xlsxwriter | |
| # define this Python module as a GRT module | |
| ModuleInfo = DefineModule(name= "SQLIDEUtils", author= "zhwei", version="0.1") | |
| @ModuleInfo.plugin("wb.sqlide.exportExcelXlsx", caption= "Export Result Set As Excel Xlsx", input= [wbinputs.currentQueryEditor()], pluginMenu= "SQL/Utilities") | |
| @ModuleInfo.export(grt.INT, grt.classes.db_query_QueryBuffer) | |
| def exportExcelXlsx(query_editor): | |
| resultset = query_editor.activeResultPanel.resultset | |
| cursor = resultset.goToFirstRow() | |
| column_count = len(resultset.columns) | |
| data = [] | |
| # Row Header | |
| data.append([i.name for i in resultset.columns]) | |
| # Data Rows | |
| while cursor: | |
| data.append([resultset.stringFieldValue(i) for i in range(column_count)]) | |
| cursor = resultset.nextRow() | |
| # Initialize Excel Workbook and Worksheet | |
| path = expanduser("~/Desktop") | |
| filename = '{}/{}-{}.xlsx'.format(path, query_editor.name, time.strftime('%Y-%m-%d.%H%M%S')) | |
| workbook = xlsxwriter.Workbook(filename) | |
| worksheet = workbook.add_worksheet() | |
| for i, row in enumerate(data): | |
| for j, item in enumerate(row): | |
| worksheet.write(i, j, item.decode('utf-8') if item else item) | |
| workbook.close() | |
| print 'Saved to ' + filename |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working with v8.0.20, 64bits, any update here?