Set default file type to xlsx when saving a spreadsheet generated from stream

1 Answer 47 Views
Spreadsheet
Alessandro
Top achievements
Rank 1
Iron
Iron
Iron
Alessandro asked on 15 Jan 2024, 05:19 PM

Hi,

I can generate a spreadsheet from stream then visualize it with a radspreadsheet and related ribbon bar, but when I save it, the default extension is always .xls, all I want is to save as default in xlsx (and maybe remove the other file type filters).

Also I was unable to set a default name for the file, how can I do that?

Thank you.

Alessandro

Alessandro
Top achievements
Rank 1
Iron
Iron
Iron
commented on 15 Jan 2024, 05:29 PM

I use C#, I forget to tell

1 Answer, 1 is accepted

Sort by
0
Accepted
Nadya | Tech Support Engineer
Telerik team
answered on 16 Jan 2024, 12:49 PM

Hello, Alessandro,

Currently, the possible solution that I can suggest is to detect when the open file command is executed by handling the SpreadsheetElement.ActiveSheetEditor.UICommandExecuted event. Then you can intercept the default logic for the Save file dialog and introduce a custom one where you can specify the default file name and desired format. Please refer to the following code snippet:

this.radSpreadsheet1.SpreadsheetElement.ActiveSheetEditor.UICommandExecuting += ActiveSheetEditor_UICommandExecuting;
private void ActiveSheetEditor_UICommandExecuting(object sender, Telerik.WinForms.Controls.Spreadsheet.Commands.UICommandExecutingEventArgs e)
{
    if (e.Command == this.radSpreadsheet1.SpreadsheetElement.CommandDescriptors.SaveFile.Command)
    {
        e.Cancel();
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        var dlg = new System.Windows.Forms.SaveFileDialog();
        dlg.Filter = "Xlsx Files (*.xlsx)|*.xlsx";
        dlg.DefaultExt = "xlsx";
        dlg.FileName = "Test.xlsx";
        dlg.AddExtension = true;
        dlg.RestoreDirectory = true;
        dlg.InitialDirectory = desktopPath;
        if (dlg.ShowDialog() == DialogResult.OK)
        {
            var formatProvider = new XlsxFormatProvider();
            using (Stream output = new FileStream(dlg.FileName, FileMode.Create))
            {
                formatProvider.Export(this.radSpreadsheetRibbonBar1.AssociatedSpreadsheet.Workbook, output);
            }
        }
    }
}

We already have logged a feature request about this: RadSpreadsheet: Add public API for specifying default file names for the Open/Save functionality in RadSpreadsheetRibbonBar (telerik.com). Feel free to subscribe for the item or vote for it in order to increase its priority. 

I hope this information helps. Please let me know if you have any other questions.

Regards,
Nadya | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Alessandro
Top achievements
Rank 1
Iron
Iron
Iron
commented on 16 Jan 2024, 03:22 PM

ok, I tryed the code, it works very well, thank you!
Tags
Spreadsheet
Asked by
Alessandro
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Nadya | Tech Support Engineer
Telerik team
Share this question
or