Mehul Harry's DevExpress Blog

This Blog

News


Mehul Harry is the DevExpress Web Program Manager. Follow him on twitter: @Mehulharry

Subscribe (RSS, Email)

Archives

How to Open a Microsoft Office Document with DevExpress ASP.NET Office Controls

Getting started with the DevExpress ASP.NET Office Controls is easy. In a few of the upcoming blogs, I'll highlight some of the common getting started tasks with DevExpress ASP.NET RichEdit and Spreadsheet controls.

One of the first things you'll likely want to do after adding a DevExpress ASP.NET Office control to your web project is to open a document.

File System or Database

There are two ways to load a document into a DevExpress ASP.NET office control. You can use the file system or load it from document storage (database).

Note, the code example below reference the DevExpress ASP.NET Spreadsheet control, however, the same code applies to the DevExpress ASP.NET RichEdit control.

Load from File System

The DevExpress Office controls support various formats including docx, csv, pdf, xlsx, and more.

To load or open a document (docx, pdf, xlsx, etc.) using the Server-side API, call the Open method:

void ASPxSpreadsheet.Open(string pathToDocument)

The pathToDocument) parameter represents the full the document path and filename in the File System. The pathToDocument also plays role of the document identifier - the DocumentID.

The Open method also provides several overloads:

DevExpress ASP.NET Spreadsheet - Open method

Because these are server-side ASP.NET controls, the file system mentioned above is the one on your web server. End-users can upload a file to the working directory that you specify for the control to use. Then, call the open method and load the corresponding file into the DevExpress ASP.NET Office control.

Take a look at this 'Custom Document Management' demo which has integrated the versatile DevExpress ASP.NET File Manager control. The File Manager control gives your end-users a visual layout of the server-side file system and allows them to upload files too.

Load from Document Storage (Database)

To load a document from the database, the DevExpress Office Controls provide stream and byte array parameters in the Open method:

void ASPxSpreadsheet.Open(string documentId, DocumentFormat format, Func<Stream> contentAccessor)

void ASPxSpreadsheet.Open(string documentId, DocumentFormat format, Func<byte[]> contentAccessor)

Let's take a look at the parameters needed to retrieve a file (aka binary object) from your database.

DocumentID

The DocumentID parameter is simply a unique string and is used to load and save documents. The DocumentID is necessary when dealing with ASP.NET documents programmatically.

If you've previously saved your DocumentID parameter to the database then you can use this ID for the open method.

Or you can also generate a new unique ID using the following approach:

private void CustomDocumentOpening() {
    var newUniqueDocumentId = Guid.NewGuid().ToString();

DocumentFormat

The DocumentFormat specifies the file format you want to open (xlsx, docx, etc.).

Load Blob from Database

After getting your DocumentID, you'll need to write some custom code that extracts the document (binary object) from your database.

For example, here is how to retrieve it via a stream:

    // Open document from content in stream
    using(var stream = GetStreamFromCustomDocumentStorage()) {
        ASPxSpreadsheet1.Open(newUniqueDocumentId, DocumentFormat.Xlsx, () => stream);
    }
}

private FileStream GetStreamFromCustomDocumentStorage() {
    throw new NotImplementedException();
}

And here is how to retrieve it via a byte array:

    // Or, open document from content in byte array
    byte[] documentContentAsByteArray = GetByteArrayFromCustomDocumentStorage();
    ASPxSpreadsheet1.Open(newUniqueDocumentId, DocumentFormat.Xlsx, () => documentContentAsByteArray);

    // TODO save somewhere the newUniqueDocumentId if needed
}

private byte[] GetByteArrayFromCustomDocumentStorage() {
    throw new NotImplementedException();
}

Once the document is retrieved from the database, it can be passed to one of the open method overloads as a Stream or array of bytes (along with the DocumentID and DocumentFormat).

Feedback

I would love to hear your feedback on the DevExpress ASP.NET Office Controls.

Leave me a comment below or email me directly: mharry@devexpress.com

Thanks!


Your Next Great .NET App Starts Here

Year after year, .NET developers such as yourself consistently vote DevExpress products #1.

Experience the DevExpress difference for yourself and download a free 30-day trial of all our products today: DevExpress.com/trial (free support is included during your evaluation).

Published Oct 05 2016, 08:00 AM by
Filed under: , ,
Bookmark and Share

Comments

James S K Makumbi

Awesome post. It doesn't mention which versions of dx it works with. Not all of us are on 16.1 like you are Mehul ;-)

This post answers an issue we had two or three years ago in XAF and there being no document viewer control in ASP .Net other than the HTMLWriter control.

Thanks very much for this. Either you are an awesome writer (obviously) or my skill level has improved that I understood the explanation better than I did two years ago.

October 5, 2016 11:45 PM

Mehul Harry (DevExpress)

Hi James,

Thanks! It works with last two major versions. I'll post some blogs posts related to this and we're working on making some great documentation too.

October 6, 2016 12:08 AM

Atul Nighot

Its great.but it is applicable to power point also.

October 7, 2016 11:23 AM

Mehul Harry (DevExpress)

Hi Atul,

This blog post relates the DevExpress ASP.NET Office controls (RichEdit and Spreadsheet). We do not have a powerpoint control for ASP.NET, sorry.

October 8, 2016 10:11 AM

Groupcare A/S

Hi Mehul,

Looks really great!

How well does these controls support simultaneous editing of the same document? - "unfortunately" our webpage is visited by more than one person at the time :-)

Are there any built-in support for this, or any suggenstions on how to prevent simultaneous editing.

October 9, 2016 10:06 PM

Stason (DevExpress Support)

@Groupcare,

Thank you for your interest. It is possible to prevent simultaneous editing in our Spreadsheet. Please refer to www.devexpress.com/.../T396648 for details and a sample.

October 11, 2016 12:02 AM

About Mehul Harry (DevExpress)

Mehul Harry is an ASP.NET technical evangelist at Developer Express. You can reach him directly at mharry@DevExpress.com. You can also follow him on Twitter: http://twitter.com/mehulharry
LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners