aspnet-117676-components-spreadsheet-document-management-save-a-document.md
Call the Save() method to initiate a save operation for the active document. The method invokes the Saving event, which allows you to implement custom saving logic. If you do not handle the event (the e.Handled property is not set to true), the control processes the document in the following ways:
Handle the Saving event to save a document in a custom manner. The event fires when you call the Save() method or when a user clicks the Save or SaveAs ribbon command.
View Example: How to save/load documents to/from a database
A custom implementation is required when ASPxSpreadsheet does not have enough information to complete the document save operation automatically. Such situations occur when the control is about to save a new document or a document opened from a custom document storage by the Open(String, DocumentFormat, Func<Stream>) or Open(String, DocumentFormat, Func<Byte[]>) method.
The event argument’s e.DocumentID property identifies the document to be saved. Implement custom logic and set the e.Handled property to true to disable the built-in save routine. If it is disabled but the handler includes no custom save procedure implementation, the save operation throws an exception.
using DevExpress.Web.Office;
protected void ASPxSpreadsheet1_Saving(object source, DocumentSavingEventArgs e) {
byte[] documentContentAsByteArray = ASPxSpreadsheet1.SaveCopy(DocumentFormat.Xlsx);
// Implement your custom logic to save a document to a custom storage
e.Handled = true;
}
Imports DevExpress.Web.Office
Protected Sub ASPxSpreadsheet1_Saving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
Dim documentContentAsByteArray As Byte() = ASPxSpreadsheet1.SaveCopy(DocumentFormat.Xlsx)
' Implement your custom logic to save a document to a custom storage
e.Handled = True
End Sub
using DevExpress.Web.Office;
protected void ASPxSpreadsheet1_Saving(object source, DocumentSavingEventArgs e) {
using (MemoryStream documentContentAsStream = new MemoryStream()) {
ASPxSpreadsheet1.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx);
// Your custom logic to save a document to a custom storage
}
e.Handled = true;
}
Imports DevExpress.Web.Office
Protected Sub ASPxSpreadsheet1_Saving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
Using documentContentAsStream As MemoryStream = New MemoryStream()
ASPxSpreadsheet1.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx)
' Implement your custom logic to save a document to a custom storage
End Using
e.Handled = True
End Sub
You can declare the Saving event handler as a globally available static method to be able to use it with the autosave feature.
Save operation conflicts may arise when a user calls the SaveAs UI command to replace another document that is already opened by DocumentManager and contains unsaved changes made by another user.
Handle the Saving event to resolve multi-user conflicts during save operations. The event argument exposes the following properties:
MultiUserConflictIdentifies the conflict’s reason.MultiUserConflictResolveSpecifies how to resolve the conflict.
The code sample below prevents a document override:
using DevExpress.Web.Office;
//...
public static void OnSaving(object source, DocumentSavingEventArgs e) {
if (e.MultiUserConflict == MultiUserConflict.OtherUserDocumentOverride)
e.MultiUserConflictResolve = MultiUserConflictResolve.Persist;
e.Handled = true;
}
Imports DevExpress.Web.Office
' ...
Public Shared Sub OnSaving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
If e.MultiUserConflict = MultiUserConflict.OtherUserDocumentOverride Then
e.MultiUserConflictResolve = MultiUserConflictResolve.Persist
e.Handled = True
End Sub
Call the following methods to save a copy of the active document to a file system, stream, or byte array:
These methods export the document with the current changes, but do not affect the original document.
Set the AutoSaveMode property to On to make the ASPxSpreadsheet control save the active document automatically on each timeout specified by the AutoSaveTimeout property. Note, to improve control performance, the DocumentManager processes multiple documents in parts with a 20-second interval. This behavior can cause a difference between the specified and the actual autosave timeout. For instance, when the specified timeout is short and the document manager processes a lot of documents, the actual timeout duration can increase.
If a document is opened by multiple ASPxSpreadsheet controls with the autosave feature enabled, the timeout is set to the smallest value among all these controls.
<dx:ASPxSpreadsheet ID="ASPxSpreadsheet1" runat="server" WorkDirectory="~/App_Data/WorkDirectory"
AutoSaveMode="On" AutoSaveTimeout="00:10:00" >
</dx:ASPxSpreadsheet>
The autosave feature invokes the AutoSaving event that allows you to implement custom autosave logic. If you do not handle the event (the e.Handled property is set to false), the control processes the document in the following ways:
Handle the AutoSaving event to process custom save operations triggered by the autosave timer (AutoSaveTimeout).
A custom implementation is required under the same circumstances as the custom save functionality. We strongly recommend that you declare this event handler as a globally available static method and assign it to both the AutoSaving and Saving events.
The following code sample assigns a handler to the AutoSaving event in the global.asax file’s Application_Start method.
using DevExpress.Web.Office;
void Application_Start(object sender, EventArgs e) {
DocumentManager.AutoSaving += DocumentManager_AutoSaving;
}
static void DocumentManager_AutoSaving(IDocumentInfo documentInfo, DocumentSavingEventArgs e) {
SpreadsheetDocumentInfo info = documentInfo as SpreadsheetDocumentInfo;
byte[] documentContentAsByteArray = info.SaveCopy(DocumentFormat.Xlsx);
SaveHelper.SaveToCustomStorage(documentContentAsByteArray, e.DocumentID);
e.Handled = true;
}
Imports DevExpress.Web.Office
Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
DocumentManager.AutoSaving += AddressOf DocumentManager_AutoSaving
End Sub
Private Shared Sub DocumentManager_AutoSaving(ByVal documentInfo As IDocumentInfo, ByVal e As DocumentSavingEventArgs)
Dim info As SpreadsheetDocumentInfo = TryCast(documentInfo, SpreadsheetDocumentInfo)
Dim documentContentAsByteArray As Byte() = info.SaveCopy(DocumentFormat.Xlsx)
SaveHelper.SaveToCustomStorage(documentContentAsByteArray, e.DocumentID)
e.Handled = True
End Sub
using DevExpress.Web.Office;
public static class SaveHelper {
private static void SaveToCustomStorage(byte[] documentContent, string p) {
// Your custom logic to save a document to a custom storage
}
}
protected void ASPxSpreadsheet1_Saving(object source, DocumentSavingEventArgs e) {
byte[] documentContentAsByteArray = ASPxSpreadsheet1.SaveCopy(DocumentFormat.Xlsx);
SaveHelper.SaveToCustomStorage(documentContentAsByteArray, e.DocumentID);
e.Handled = true;
}
Imports DevExpress.Web.Office
Public NotInheritable Class SaveHelper
Private Shared Sub SaveToCustomStorage(ByVal documentContent As Byte(), ByVal p As String)
' Your custom logic to save a document to a custom storage
End Sub
End Class
Protected Sub ASPxSpreadsheet1_Saving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
Dim documentContentAsByteArray As Byte() = ASPxSpreadsheet1.SaveCopy(DocumentFormat.Xlsx)
SaveHelper.SaveToCustomStorage(documentContentAsByteArray, e.DocumentID)
e.Handled = True
End Sub
using DevExpress.Web.Office;
void Application_Start(object sender, EventArgs e) {
DocumentManager.AutoSaving += DocumentManager_AutoSaving;
}
static void DocumentManager_AutoSaving(IDocumentInfo documentInfo, DocumentSavingEventArgs e) {
SpreadsheetDocumentInfo info = documentInfo as SpreadsheetDocumentInfo;
using (MemoryStream documentContentAsStream = new MemoryStream()) {
info.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx);
SaveHelper.SaveToCustomStorage(documentContentAsStream, e.DocumentID);
}
e.Handled = true;
}
Imports DevExpress.Web.Office
Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
DocumentManager.AutoSaving += AddressOf DocumentManager_AutoSaving
End Sub
Private Shared Sub DocumentManager_AutoSaving(ByVal documentInfo As IDocumentInfo, ByVal e As DocumentSavingEventArgs)
Dim info As SpreadsheetDocumentInfo = TryCast(documentInfo, SpreadsheetDocumentInfo)
Using documentContentAsStream As MemoryStream = New MemoryStream()
info.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx)
SaveHelper.SaveToCustomStorage(documentContentAsStream, e.DocumentID)
End Using
e.Handled = True
End Sub
using DevExpress.Web.Office;
public static class SaveHelper {
private static void SaveToCustomStorage(MemoryStream ms, string p) {
// Your custom logic to save a document to a custom storage
}
}
protected void ASPxSpreadsheet1_Saving(object source, DocumentSavingEventArgs e) {
using (MemoryStream documentContentAsStream = new MemoryStream()) {
ASPxSpreadsheet1.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx);
SaveHelper.SaveToCustomStorage(documentContentAsStream, e.DocumentID);
}
e.Handled = true;
}
Imports DevExpress.Web.Office
Public NotInheritable Class SaveHelper
Private Shared Sub SaveToCustomStorage(ByVal ms As MemoryStream, ByVal p As String)
' Your custom logic to save a document to a custom storage
End Sub
End Class
Protected Sub ASPxSpreadsheet1_Saving(ByVal source As Object, ByVal e As DocumentSavingEventArgs)
Using documentContentAsStream As MemoryStream = New MemoryStream()
ASPxSpreadsheet1.SaveCopy(documentContentAsStream, DocumentFormat.Xlsx)
SaveHelper.SaveToCustomStorage(documentContentAsStream, e.DocumentID)
End Using
e.Handled = True
End Sub
See Also
Save a Document