Open a Document

  • 4 minutes to read

The Open method overloads allow you to open documents from a file system, stream, or byte array.

Open a Document from a File System

Call the Open(String) method to open a document from the server’s file system. When you open a document for the first time, the control loads it to the server’s memory and generates a DocumentId from the pathToDocument parameter value.

If the document is already open, the method activates the document stored in the server’s memory.

string pathToDocument = Server.MapPath("~/App_Data/Documents/doc.xlsx");
ASPxSpreadsheet1.Open(pathToDocument);

Specify the WorkDirectory property to enable the Open and Save As UI commands. Users cannot access the file system outside the specified directory.

Open a Document from a Stream

Call the Open(String, DocumentFormat, Func<Stream>) method to load a document from a stream.

Specify the documentId parameter to identify the document. If the specified value is not unique within all open documents, the method activates the previously loaded document with the same documentId and ignores the stream data.

private void CustomDocumentOpening() {
    var uniqueDocumentId = Guid.NewGuid().ToString();
    using (var stream = GetStreamFromCustomDocumentStorage()) {
        ASPxSpreadsheet1.Open(uniqueDocumentId, DocumentFormat.Xlsx, () => stream);
    }
}
private FileStream GetStreamFromCustomDocumentStorage() {
    // Provide your custom logic to obtain a document (for instance, from a database) 
}

Open a Document from a Byte Array

Call the Open(String, DocumentFormat, Func<Byte[]>) method to load a document from a byte array.

Specify the documentId parameter to identify the document. If the specified value is not unique within all open documents, the method activates the previously loaded document with the same documentId and ignores the byte array.

private void CustomDocumentOpening() {
    var uniqueDocumentId = Guid.NewGuid().ToString();
    byte[] documentContentAsByteArray = GetByteArrayFromCustomDocumentStorage();
    using (var stream = GetStreamFromCustomDocumentStorage()) {
        ASPxSpreadsheet1.Open(uniqueDocumentId, DocumentFormat.Xlsx, () => documentContentAsByteArray);
    }
}
private byte[] GetByteArrayFromCustomDocumentStorage() {
    // Provide your custom logic to obtain a document (for instance, from a database) 
}

Open a Document as a Template

ASPxSpreadsheet allows users to open their own copies of a document in its initial state. In this case, the original version of a document is used as a template. Each user can obtain a copy and modify it independently from other users who open the same template.

To implement this behavior, you need a template document. Pass it as a parameter to one of the Open method overloads listed below. Each method has the documentId parameter, so you can specify unique documentId values for each user. A called method opens an independent copy of the template document for each user as a result.

  • Open(String, DocumentFormat, Func<Stream>)

    var templatePath = Server.MapPath("~/App_Data/WorkDirectory/BreakevenAnalysis.xlsx");
    var documentId = Guid.NewGuid().ToString();
    using (var stream = new FileStream(templatePath, FileMode.Open)) {
        ASPxSpreadsheet1.Open(documentId, DocumentFormat.Xlsx, () => stream);
    }
    
  • Open(String, DocumentFormat, Func<Byte[]>)

    var templatePath = Server.MapPath("~/App_Data/WorkDirectory/BreakevenAnalysis.xlsx");
    var documentId = Guid.NewGuid().ToString();
    ASPxSpreadsheet1.Open(documentId, DocumentFormat.Xlsx, () => File.ReadAllBytes(templatePath));
    

Do not use the Open(String) method for this scenario, because the method always opens a single document that has the same DocumentId equal to the pathToDocument parameter.

Switch Between Open Documents

When a user opens a document, the ASPxSpreadsheet retains the previous document in the server memory (except for a new document). The DocumentManager maintains information about every open document. Call the following methods to access this information:

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

Call the Open(SpreadsheetDocumentInfo) method to activate a previously opened document.

var documentInfo = (SpreadsheetDocumentInfo)DocumentManager.FindDocument("myDocumentId");
ASPxSpreadsheet1.Open(documentInfo);

Refresh a Document

When you open a document from a file system and then try to reopen it, the ASPxSpreadsheet activates the previously opened document from the document storage.

If the document was modified by a third-party tool (Microsoft Excel, for instance), the ASPxSpreadsheet does not reflect these changes. Call the CloseDocument(String) method to explicitly unload the document from the document storage. Then load the modified version of the document.

var filePath = ASPxSpreadsheet1.DocumentId;
DocumentManager.CloseDocument(filePath);
ASPxSpreadsheet1.Open(filePath);