Skip to main content
All docs
V24.1

Open a Document

  • 4 minutes to read

This document lists Spreadsheet methods that allow you to open documents, obtain a list of open documents, and switch between them.

Open a Document from a Byte Array

Call the Open(String, Func<Byte[]>) method overload to open a document from a byte array. This overload determines the document’s format automatically. Pass the document format to the Open(String, DocumentFormat, Func<Byte[]>) method to disable automatic detection and improve performance:

@model SpreadsheetDocumentContentFromBytes

@(Html.DevExpress()
    .Spreadsheet("spreadsheet")
    .Open(@Model.DocumentId, DocumentFormat.Xlsx, @Model.ContentAccessorByBytes)
)

Run Demo: Open a Byte Array

The documentId method parameter identifies the document that the control should open. If the Spreadsheet has an open document with the same identifier, the control loads the document from the web server’s memory and ignores the array data.

Open a Document from a Stream

Call the Open(String, Func<Stream>) method overload to open a document from a stream. This overload determines the document’s format automatically. Pass the document format to the Open(String, DocumentFormat, Func<Stream>) method to disable automatic detection and improve performance:

@model SpreadsheetDocumentContentFromStream

@(Html.DevExpress()
    .Spreadsheet("spreadsheet")
    .Open(@Model.DocumentId, DocumentFormat.Xlsx, @Model.ContentAccessorByStream)
)

Run Demo: Open a Stream

The documentId method parameter identifies the document that the control should open. If the Spreadsheet has an open document with the same identifier, the control loads the document from the web server’s memory and ignores the stream data.

Open a Document from a File System

Call the Open(String) method overload to open a document from the server’s file system. This overload determines the document’s format automatically. Pass the document format as a parameter to the Open(String, DocumentFormat) method to disable automatic detection and improve performance:

@(Html.DevExpress()
    .Spreadsheet("spreadsheet")
    .Open("path-to-your-document", DocumentFormat.Xlsx)
)

Run Demo: Open a File

The path method parameter serves as identifier for the document that the control should open. If the Spreadsheet has an open document with the same identifier, the method loads the document from the web server’s memory. To reload a document to the server (for instance, after your changed this document in the Microsoft Excel), close the document and then reopen it.

Open a Document as a Template

The Spreadsheet allows you to open a copy of a template document. Users can edit their copies independently of each other and not overwrite the original document. To open a new copy of a template document, generate a unique string identifier and pass it to one of the following method overloads:

You cannot use method overloads that open a document from a file system for this scenario. They always open the document with the same identifier as the path to this document.

Switch Between Open Documents

The Spreadsheet stores all open documents in the server memory until the DocumentManager hibernates the document. When the hibernation is disabled, all documents remain in storage until the web server is restarted. Call the following DocumentManager‘s methods to access information about the open documents:

FindDocument(String)
Returns a IDocumentInfo object that contains information about a document with the specified document identifier.
GetAllDocuments()
Returns a collection of IDocumentInfo objects that contain information about every open document.

You can use this information to close open documents or switch between them. The example below demonstrates how to find and load an open document whose identifier contains yesterday’s date:

public ActionResult SwitchToDocument() {
    var spreadsheet = SpreadsheetRequestProcessor.GetSpreadsheetFromState(spreadsheetState);
    SpreadsheetDocumentContent model = new SpreadsheetDocumentContent(spreadsheet.DocumentId);
    var documentInfoCollection = DocumentManager.GetAllDocuments();
    string date = DateTime.Today.AddDays(-1).ToString("dd-MM-yyyy");
    foreach (SpreadsheetDocumentInfo documentInfo in documentInfoCollection)
        if (documentInfo.DocumentId.Contains(date)) {
            model.DocumentId = documentInfo.DocumentId;
            break;
        }
    return PartialView("SpreadsheetPartialView", model);
}

Refer to the following example for more information: How to use AJAX requests to update document content.

GitHub Example

View Example: How to open different files in a popup dialog