Summary
This question is a continuation of the desire to architecture a simple table API, while maintaining its convenience for those who know Excel well.
To summarize, this question is related to these two below:
1. How to implement self-name columns from your index? ;
2. How to make this normal sheet initialization faster? .
purpose
Provide a simplified Excel API used as a wrapper for neuralgic components such as Application , Workbook , Worksheet and Range classes / interfaces when exposing only the most commonly used object properties for each of them.
Usage example
This use case is inspired by unit tests, which allowed me to bring this solution to where it stands.
Dim file as String = "C:\Temp\WriteTest.xls" Using mgr As ISpreadsheetManager = New SpreadsheetManager() Dim wb as IWorkbook = mgr.CreateWorkbook() wb.Sheets("Sheet1").Cells("A1").Value = 3.1415926 wb.SaveAs(file) End Using
And now we open it:
Dim file as String = "C:\Temp\WriteTest.xls" Using mgr As ISpreadsheetManager = New SpreadsheetManager() Dim wb as IWorkbook = mgr.OpenWorkbook(file)
Discussion
When creating an instance of an Excel workbook:
- A worksheet instance is automatically initialized in the Workbook.Sheets collection;
- After initialization, the worksheet initializes its cells through the
Range object, which can represent one or more cells.
These cells are immediately available with all their properties as soon as the worksheet exists.
My desire is to reproduce this behavior so that
- The constructor of the Workbook class initializes the property of the Workbook.Sheets collection with its own sheets;
- The constructor of the Worksheet class initializes the property of the Worksheet.Cells collection using its own cells.
My problem arises from the constructor of the Worksheet class when initializing the property of the Worksheet.Cells collection shown in # 2.
Thoughts
Following these related issues, there are problems, I want to find out another architecture that would allow me:
- If necessary, use the special
Range cell function; - Provide the most commonly used properties through my
ICell interface; - Access to all
Range cells of the worksheet from its initialization.
Remembering that access to the Range.Value property is the fastest possible interaction with the base instance of Excel using Interop.
So, I thought about initializing my ReadonlyOnlyDictionary(Of String, ICell) name of the cells without immediately wrapping an instance of the Range interface, so that I simply generate row and column indices along with the cell name to index my dictionary, then assigning the Cell.NativeCell property only then when you want to access or format a specific range of cells or cells.
Thus, the data in the dictionary will be indexed with the name of the cells obtained from the column indices generated in the constructor of the Worksheet class. Then when you do this:
Using mgr As ISpreadsheetManager = New SpreadsheetManager() Dim wb As IWorkbook = mgr.CreateWorkbook() wb.Sheet(1).Cells("A1").Value = 3.1415926
# 1: This will allow me to use indexes from my Cell class to write this value to a specific cell, which faster uses its name directly for Range .
Questions and concerns
Also, when working with UsedRange.get_Value() or Cells.get_Value() , this returns arrays of Object (,).
1. So should I be happy to work with Object(,) arrays for cells, without being able to somehow format it?
2. How to archive these Worksheet and Cell classes so that I have better performance when working with Object(,) arrays, while still maintaining the possibility that a Cell instance can represent or wrap a single cell range?
Thanks to any of you who take the time to read my post and my sincere thanks to those who respond.