User Guide > How to Work with... > Formats with Additional Functionality > Office > How To ... > Excel Documents > Excel: Loading Excel Documents |
The process for loading Excel documents with ImageGear is very similar to that of other document formats. The following steps will enable you to view the first page of a worksheet in an Excel document.
Add the Excel format to the list of file format filters ImageGear will use to detect formats of loaded content.
C# Example |
Copy Code |
---|---|
// Add support for Excel files
ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat()); |
VB.NET Example |
Copy Code |
---|---|
' Add support for Excel files
ImGearFileFormats.Filters.Add(ImGearOffice.CreateExcelFormat()) |
Once the desired Excel file has been opened into a System.IO.Stream, the document can be loaded with the LoadDocument method. The startPageNumber and count parameters of the LoadDocument method can be used to load a select few or all of the sheets in the Excel document. The example below will load all available sheets.
The GetPageCount method can be used to get the number of sheets in a Stream that contains a valid Excel document.
ImageGear only supports the popular worksheet sheet type currently. For any other sheet type encountered, like chartsheet and dialogsheet, a blank 8.5” x 11” page will be returned. |
C# Example |
Copy Code |
---|---|
// Call the LoadDocument to load the selected sheets from the Excel document.
ImGearDocument imGearDocument = ImGearFileFormats.LoadDocument(fileContent, 0, -1); |
VB.NET Example |
Copy Code |
---|---|
' Call the LoadDocument to load the selected sheets from the Excel document.
Dim imGearDocument As ImGearDocument = ImGearFileFormats.LoadDocument(fileContent, 0, -1) |
Once the document is loaded, the top-level ImGearExcelBookDocument will contain pages, with each one representing an entire sheet. Retrieve one of the pages to access a sheet in the Excel document that can be displayed or printed as a single page or paginated further with a different page size. The following example paginates the worksheet into pages whose size is determined by that which is defined in the original Excel document. Alternatively, a custom size can be defined for the new pages simply by setting the width and height parameters of the Repaginate method accordingly.
Use caution when displaying or printing a sheet as a single page. Because of the large sizes that are possible with Excel sheets, it’s possible that the memory necessary to render the page will exceed the resources of the system. In general, sheets with less than 20,000 cells are safe for rendering as a single page on a modern system. Though keep in mind, the larger the sheet the more time that will be needed to render the page. It is recommended that the paginated ImGearExcelSheetPage objects be used for displaying or printing by default unless the size of the worksheets are known. |
C# Example |
Copy Code |
---|---|
if (imGearDocument is ImGearExcelBookDocument) { if (imGearExcelBookDocument.Pages.Count > 0) { currSheetNumber = 0; // Set the current sheet number to an ImGearExcelBookPage object ImGearExcelBookPage imGearBookPage = (ImGearExcelBookPage)imGearExcelBookDocument.Pages[currSheetNumber]; imGearDocument = imGearBookPage.Repaginate(0, 0); } } |
VB.NET Example |
Copy Code |
---|---|
If (TypeOf imGearDocument Is ImGearExcelBookDocument) Then
If imGearExcelBookDocument.Pages.Count > 0 Then
currSheetNumber = 0
' Set the current sheet number to an ImGearExcelBookPage object
Dim imGearBookPage As ImGearExcelBookPage = DirectCast(imGearExcelBookDocument.Pages(Me.currSheetNumber), ImGearExcelBookPage)
imGearDocument = imGearBookPage.Repaginate(0, 0)
End If
End If |
Displaying the first one of the pages you created for the sheet is easy using ImageGear’s PageView control. Simply create the control and set the Page property to that of the Excel page. Refer to the Displaying Images topic for more information on displaying images with ImageGear.
C# Example |
Copy Code |
---|---|
// Display the first page for the sheet.
imGearPageView.Page = imGearDocument.Pages[0];
imGearPageView.Update(); |
VB.NET Example |
Copy Code |
---|---|
' Display the first page for the sheet.
imGearPageView.Page = imGearDocument.Pages(0)
imGearPageView.Update() |