Imports DevExpress.XtraSpreadsheet.Services
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.XtraSpreadsheet.Commands
Imports DevExpress.ExpressApp

Public Class CustomCommandFactoryServise
    Inherits SpreadsheetCommandFactoryServiceWrapper
    Private Property Control() As SpreadsheetControl
        Get
            Return m_Control
        End Get
        Set(value As SpreadsheetControl)
            m_Control = value
        End Set
    End Property
    Private Property View As DevExpress.ExpressApp.View
        Get
            Return m_View
        End Get
        Set(value As DevExpress.ExpressApp.View)
            m_View = value
        End Set
    End Property
    Private m_Control As SpreadsheetControl
    Private m_View As DevExpress.ExpressApp.View
    Public Sub New(service As ISpreadsheetCommandFactoryService, _control As SpreadsheetControl, _view As DevExpress.ExpressApp.View)
        MyBase.New(service)
        Control = _control
        View = _view
    End Sub

    Public Overrides Function CreateCommand(id As SpreadsheetCommandId) As SpreadsheetCommand
        Dim command As SpreadsheetCommand = MyBase.CreateCommand(id)
        If id = SpreadsheetCommandId.FileSave Then
            Return New SaveCommand(Control, View)
        End If
        Return command
    End Function
End Class

Public Class SaveCommand
    Inherits SaveDocumentCommand
    Private m_View As DevExpress.ExpressApp.View
    Private m_Control As SpreadsheetControl
    Private Property SpreadControl() As SpreadsheetControl
        Get
            Return m_Control
        End Get
        Set(value As SpreadsheetControl)
            m_Control = value
        End Set
    End Property
    Private Property View As DevExpress.ExpressApp.View
        Get
            Return m_View
        End Get
        Set(value As DevExpress.ExpressApp.View)
            m_View = value
        End Set
    End Property
    Public Sub New(control As ISpreadsheetControl, _view As DevExpress.ExpressApp.View)
        MyBase.New(control)
        SpreadControl = control
        View = _view
    End Sub
    Public Overrides Sub Execute()
        Dim _OpenSpreadDocument_Popup = TryCast(View.SelectedObjects(0), OpenSpreadDocument_Popup)
        Dim _BusinessDirectory As BusinessDirectory = _OpenSpreadDocument_Popup.BusinessDirectory
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _OpenSpreadDocument_Popup.BusinessDirectory.FileData.FileName)
        Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
        SpreadControl.Document.SaveDocument(_Stream, DevExpress.Spreadsheet.DocumentFormat.OpenXml)
        _Stream.Close()

        Dim _StreamFile = New System.IO.FileStream(_FilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

        _OpenSpreadDocument_Popup.BusinessDirectory.FileData.LoadFromStream(_OpenSpreadDocument_Popup.BusinessDirectory.FileData.FileName, _StreamFile)
        _ocur.CommitChanges()

    End Sub
End Class
