示例#1
0
        /// <summary>
        /// Imports the data of this <see cref="DbView"/> to a <see cref="ExcelInterop.Worksheet"/>.
        /// </summary>
        /// <returns>A <see cref="Tuple"/> containing a <see cref="MySqlDataTable"/> filled with data for this <see cref="DbView"/> and either a <see cref="ExcelInterop.ListObject"/> or a <see cref="ExcelInterop.Range"/> where the data was imported to.</returns>
        public Tuple <MySqlDataTable, object> ImportData()
        {
            Tuple <MySqlDataTable, object> retTuple;
            var activeWorkbook = Globals.ThisAddIn.ActiveWorkbook;

            try
            {
                // Create the MySqlDataTable that holds the data to be imported to Excel
                var    mySqlTable        = GetMySqlDataTable();
                object excelTableOrRange = null;
                if (mySqlTable == null)
                {
                    return(null);
                }

                if (!ImportParameters.ForEditDataOperation)
                {
                    // Create a new Excel Worksheet and import the table/view data there
                    if (ImportParameters.IntoNewWorksheet)
                    {
                        var currentWorksheet = activeWorkbook.CreateWorksheet(mySqlTable.TableName, true);
                        if (currentWorksheet == null)
                        {
                            return(null);
                        }
                    }
                    else
                    {
                        // Check if the data being imported does not exceed the column available space
                        var exceedColumnsLimit = ExcelUtilities.CheckIfColumnsExceedWorksheetLimit(mySqlTable.Columns.Count);
                        var collides           = DetectDataForImportPossibleCollisions(mySqlTable);
                        if (exceedColumnsLimit || collides)
                        {
                            var infoProperties = InfoDialogProperties.GetYesNoDialogProperties(
                                InfoDialog.InfoType.Warning,
                                Resources.ImportOverWorksheetColumnsLimitErrorTitle,
                                Resources.ImportOverWorksheetColumnsLimitErrorDetail,
                                Resources.ImportOverWorksheetColumnsLimitErrorSubDetail);
                            if (exceedColumnsLimit && InfoDialog.ShowDialog(infoProperties).DialogResult == DialogResult.No)
                            {
                                return(null);
                            }

                            infoProperties.TitleText     = Resources.ImportOverExcelObjectErrorTitle;
                            infoProperties.DetailText    = Resources.ImportOverExcelObjectErrorDetail;
                            infoProperties.DetailSubText = Resources.ImportOverExcelObjectErrorSubDetail;
                            if (collides && InfoDialog.ShowDialog(infoProperties).DialogResult == DialogResult.No)
                            {
                                return(null);
                            }

                            var newWorkSheet = activeWorkbook.CreateWorksheet(mySqlTable.TableName, true);
                            if (newWorkSheet == null)
                            {
                                return(null);
                            }
                        }
                    }

                    excelTableOrRange = Settings.Default.ImportCreateExcelTable
            ? mySqlTable.ImportDataIntoExcelTable(ImportParameters.CreatePivotTable, ImportParameters.PivotTablePosition, ImportParameters.AddSummaryRow)
            : mySqlTable.ImportDataIntoExcelRange(ImportParameters.CreatePivotTable, ImportParameters.PivotTablePosition, ImportParameters.AddSummaryRow);
                }

                retTuple = new Tuple <MySqlDataTable, object>(mySqlTable, excelTableOrRange);
            }
            catch (Exception ex)
            {
                retTuple = null;
                MiscUtilities.ShowCustomizedErrorDialog(string.Format(Resources.UnableToRetrieveData, this is DbTable ? "table" : "view", Name), ex.Message);
                MySqlSourceTrace.WriteAppErrorToLog(ex);
            }

            return(retTuple);
        }