Back to Devexpress

WorkbookSaveOptions.DefaultFileName Property

officefileapi-devexpress-dot-xtraspreadsheet-dot-workbooksaveoptions-0d29714b.md

latest7.1 KB
Original Source

WorkbookSaveOptions.DefaultFileName Property

Gets or sets the default file name used for saving a new document.

Namespace : DevExpress.XtraSpreadsheet

Assembly : DevExpress.Spreadsheet.v25.2.Core.dll

NuGet Package : DevExpress.Spreadsheet.Core

Declaration

csharp
public string DefaultFileName { get; set; }
vb
Public Property DefaultFileName As String

Property Value

TypeDescription
String

A String which represents the default file name (including the path).

|

Property Paths

You can access this nested property as listed below:

Object TypePath to DefaultFileName
DocumentOptions

.Save .DefaultFileName

|

Remarks

The DefaultFileName property specifies a file name to be suggested in the Save As dialog invoked for saving a document newly created in the SpreadsheetControl. The WorkbookSaveOptions.DefaultFormat property specifies a format for saving a new document. The file extension for the specified file format is added automatically.

To specify a name that is suggested in the SaveAs file dialog invoked by the SaveDocumentAsCommand command execution, use a Custom SaveAs Command technique illustrated in the following code snippet. The “C:\SavedDocument.xlsx” name is the default file name specified for the Save As dialog.

Example: How to Substitute the Standard Command with Custom Command

csharp
using DevExpress.XtraSpreadsheet.Commands;
using DevExpress.XtraSpreadsheet;
using DevExpress.Spreadsheet;
//...

public class CustomSaveDocumentAsCommand : SaveDocumentAsCommand {
    public CustomSaveDocumentAsCommand(ISpreadsheetControl control)
        : base(control) { }

    public override void Execute()
    {
        SaveFileDialog dialog = new SaveFileDialog
        {
            Filter = "Spreadsheet Format Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*",
            FileName = "SavedDocument.xlsx",
            RestoreDirectory = true,
            CheckFileExists = false,
            CheckPathExists = true,
            OverwritePrompt = true,
            DereferenceLinks = true,
            ValidateNames = true,
            AddExtension = false,
            FilterIndex = 1
        };
        dialog.InitialDirectory = "C:\\";
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            ((SpreadsheetControl)this.Control).SaveDocument(dialog.FileName, DocumentFormat.Xlsx);
        }
    }
}
vb
Imports DevExpress.XtraSpreadsheet.Commands
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.Spreadsheet
'...

Public Class CustomSaveDocumentAsCommand
  Inherits SaveDocumentAsCommand

  Public Sub New(ByVal control As ISpreadsheetControl)
    MyBase.New(control)
  End Sub

  Public Overrides Sub Execute()
    Dim dialog As SaveFileDialog = New SaveFileDialog With {
      .Filter = "Spreadsheet Format Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*",
      .FileName = "SavedDocument.xlsx",
      .RestoreDirectory = True,
      .CheckFileExists = False,
      .CheckPathExists = True,
      .OverwritePrompt = True,
      .DereferenceLinks = True,
      .ValidateNames = True,
      .AddExtension = False,
      .FilterIndex = 1
    }
    dialog.InitialDirectory = "C:\"
    If dialog.ShowDialog() = DialogResult.OK Then
      CType(Me.Control, SpreadsheetControl).SaveDocument(dialog.FileName, DocumentFormat.Docx)
    End If
  End Sub
End Class
csharp
using DevExpress.XtraSpreadsheet.Services;
using DevExpress.XtraSpreadsheet;
using DevExpress.Spreadsheet;
//...

public class CustomService : SpreadsheetCommandFactoryServiceWrapper {
    public CustomService(ISpreadsheetCommandFactoryService service)
        : base(service)
    {
    }
    public SpreadsheetControl Control { get; set; }

    public override SpreadsheetCommand CreateCommand(SpreadsheetCommandId id)
    {
        if (id == SpreadsheetCommandId.FormatClearContents || id == SpreadsheetCommandId.FileSaveAs)
            return new CustomSaveDocumentAsCommand(Control);

        return base.CreateCommand(id);
    }
}
vb
Imports DevExpress.XtraSpreadsheet.Services
Imports DevExpress.XtraSpreadsheet
Imports DevExpress.Spreadsheet
'...

Public Class CustomService
  Inherits SpreadsheetCommandFactoryServiceWrapper

  Public Sub New(ByVal service As ISpreadsheetCommandFactoryService)
    MyBase.New(service)
  End Sub
  Public Property Control() As SpreadsheetControl

  Public Overrides Function CreateCommand(ByVal id As SpreadsheetCommandId) As SpreadsheetCommand
    If id Is SpreadsheetCommandId.FormatClearContents OrElse id Is SpreadsheetCommandId.FileSaveAs Then
      Return New CustomSaveDocumentAsCommand(Control)
    End If

    Return MyBase.CreateCommand(id)
  End Function
End Class
csharp
public partial class Form1 : RibbonForm {
    public Form1()
    {
        InitializeComponent();
        SubstituteService();
    }

    private void SubstituteService() {
        ISpreadsheetCommandFactoryService service = (ISpreadsheetCommandFactoryService)spreadsheetControl.GetService(typeof(ISpreadsheetCommandFactoryService));
        CustomService customService = new CustomService(service);
        spreadsheetControl.ReplaceService<ISpreadsheetCommandFactoryService>(customService);
        customService.Control = spreadsheetControl;
    }

}
vb
Partial Public Class Form1
  Inherits RibbonForm

  Public Sub New()
    InitializeComponent()
    SubstituteService()
  End Sub

  Private Sub SubstituteService()
    Dim service As ISpreadsheetCommandFactoryService = DirectCast(spreadsheetControl.GetService(GetType(ISpreadsheetCommandFactoryService)), ISpreadsheetCommandFactoryService)
    Dim customService As New CustomService(service)
    spreadsheetControl.ReplaceService(Of ISpreadsheetCommandFactoryService)(customService)
    customService.Control = spreadsheetControl
  End Sub
End Class

See Also

CurrentFileName

WorkbookSaveOptions Class

WorkbookSaveOptions Members

DevExpress.XtraSpreadsheet Namespace