def menuExportToExcel(self, event): self.commit() iSelection = self.notebook.GetSelection() page = self.pages[iSelection] try: grid = page.getGrid() except: return try: pageTitle = self.pages[iSelection].getTitle() except: pageTitle = self.attrClassName[iSelection][2] if not self.fileName or len(self.fileName) < 4: Utils.MessageOK(self, 'You must Save before you can Export to Excel', 'Excel Write') return pageTitle = Utils.RemoveDisallowedFilenameChars( pageTitle.replace('/', '_')) xlfileName = self.fileName[:-4] + '-' + pageTitle + '.xls' dlg = wx.DirDialog(self, 'Folder to write "%s"' % os.path.basename(xlfileName), style=wx.DD_DEFAULT_STYLE, defaultPath=os.path.dirname(xlfileName)) ret = dlg.ShowModal() dName = dlg.GetPath() dlg.Destroy() if ret != wx.ID_OK: return xlfileName = os.path.join(dName, os.path.basename(xlfileName)) title = self.getTitle() wb = xlwt.Workbook() sheetName = self.attrClassName[self.notebook.GetSelection()][2] sheetCur = wb.add_sheet(sheetName) export = ExportGrid(title, grid) export.toExcelSheet(sheetCur) try: wb.save(xlfileName) webbrowser.open(xlfileName, new=2, autoraise=True) Utils.MessageOK(self, 'Excel file written to:\n\n %s' % xlfileName, 'Excel Export') except IOError: Utils.MessageOK( self, 'Cannot write "%s".\n\nCheck if this spreadsheet is open.\nIf so, close it, and try again.' % xlfileName, 'Excel File Error', iconMask=wx.ICON_ERROR)
def menuExportToExcel( self, event ): self.commit() iSelection = self.notebook.GetSelection() page = self.pages[iSelection] try: grid = page.getGrid() except: return try: pageTitle = self.pages[iSelection].getTitle() except: pageTitle = self.attrClassName[iSelection][2] if not self.fileName or len(self.fileName) < 4: Utils.MessageOK(self, 'You must Save before you can Export to Excel', 'Excel Write') return pageTitle = Utils.RemoveDisallowedFilenameChars( pageTitle.replace('/', '_') ) xlFName = self.fileName[:-4] + '-' + pageTitle + '.xls' dlg = wx.DirDialog( self, 'Folder to write "%s"' % os.path.basename(xlFName), style=wx.DD_DEFAULT_STYLE, defaultPath=os.path.dirname(xlFName) ) ret = dlg.ShowModal() dName = dlg.GetPath() dlg.Destroy() if ret != wx.ID_OK: return xlFName = os.path.join( dName, os.path.basename(xlFName) ) title = self.getTitle() wb = xlwt.Workbook() sheetName = pageTitle sheetName = re.sub('[+!#$%&+~`".:;|\\/?*\[\] ]+', ' ', sheetName)[:31] sheetCur = wb.add_sheet( sheetName ) export = ExportGrid( title, grid ) export.toExcelSheet( sheetCur ) try: wb.save( xlFName ) webbrowser.open( xlFName, new = 2, autoraise = True ) Utils.MessageOK(self, 'Excel file written to:\n\n %s' % xlFName, 'Excel Export') except IOError: Utils.MessageOK(self, 'Cannot write "%s".\n\nCheck if this spreadsheet is open.\nIf so, close it, and try again.' % xlFName, 'Excel File Error', iconMask=wx.ICON_ERROR )