Exemple #1
0
        public static ExcelWorkbookWeakReference Instantiate(string path)
        {
            if (!File.Exists(path))
            {
                throw new ApplicationException($"Workbook filename does not exist={path}");
            }

            var reference = new ExcelWorkbookWeakReference()
            {
                Path            = path,
                IsAlreadyOpened = ExcelOperations.IsWorkbookAlreadyOpen(path),
                WorkbookName    = ExcelOperations.WorkbookName(path)
            };

            if (reference.IsAlreadyOpened)
            {
                reference.Workbook = ExcelOperations.GetAlreadyOpenedWorkbook(path);

                return(reference);
            }

            reference.Workbook = ExcelOperations.OpenWorkbook(path);

            return(reference);
        }
        public static List <string> Names(string workbookFilename)
        {
            if (workbookFilename == null)
            {
                throw new ApplicationException($"Open workbook - workbook name is null: {workbookFilename}");
            }
            var workbook = ExcelOperations.OpenWorkbookWithRetry(workbookFilename);

            return(workbook.WorksheetNames());
        }
        public static object[,] GetEntireSheet(string fullyQualifiedWorkbookName, string worksheetName = null)
        {
            using (var reference = ExcelWorkbookWeakReferenceFactory.Instantiate(fullyQualifiedWorkbookName))
            {
                var range = WorksheetUsedRange.GetUsedWorksheetRange(reference.Workbook, worksheetName);

                if (range.Row != 1 || range.Column != 1)
                {
                    var newRows = range.Row - 1 + range.RowCount;
                    var newCols = range.Column - 1 + range.ColumnCount;

                    range = range.DisplaceAndResize(-range.Row + 1, -range.Column + 1, newRows, newCols);
                }

                var matrix = (object[, ])range.Value2;

                if (ExcelOperations.IsWorkbookAlreadyOpen(fullyQualifiedWorkbookName))
                {
                    range.CloseWorkbook();
                }

                return(matrix);
            }
        }