When using RadSpreadsheet how do I get filename that was loaded when user uses File Open?

1 Answer 50 Views
Spreadsheet
Roger
Top achievements
Rank 2
Iron
Iron
Iron
Roger asked on 11 Sep 2023, 07:42 PM
When using RadSpreadsheet how do I get filename that was loaded when user uses File Open?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 13 Sep 2023, 01:33 PM

Hello, Roger,

It is possible to detect when the open file command is executed by handling the SpreadsheetElement.ActiveSheetEditor.UICommandExecuted event: 

        private void ActiveSheetEditor_UICommandExecuted(object sender, UICommandExecutedEventArgs e)
        {
            if (e.Command == this.radSpreadsheet1.SpreadsheetElement.CommandDescriptors.OpenFile.Command)
            {

            }
        }

However, there is no public access to the file path that is opened. A possible solution that I can suggest is to create your own logic for opening the file in order to have access to the file dialog and the selected file path accordingly. I have prepared a sample code snippet for your reference:

 

        public RadForm1()
        {
            InitializeComponent();

            
            BackstageButtonItem myOpen = new BackstageButtonItem();
            myOpen.Click += MyOpen_Click;
            this.radSpreadsheetRibbonBar1.BackstageControl.Items.Insert(0, myOpen);
            myOpen.Image = ((BackstageButtonItem)this.radSpreadsheetRibbonBar1.BackstageControl.Items["backstageButtonOpen"]).Image;
          this.radSpreadsheetRibbonBar1.BackstageControl.Items["backstageButtonOpen"].Visibility = ElementVisibility.Collapsed;
        }

        private void MyOpen_Click(object sender, EventArgs e)
        {

            RadOpenFileDialog openFileDialog = new RadOpenFileDialog();
            DialogResult dr = openFileDialog.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = openFileDialog.FileName;
                 
                var formatProvider = new XlsxFormatProvider();
                using (Stream input = new FileStream(fileName, FileMode.Open))
                {
                    radSpreadsheet1.Workbook = formatProvider.Import(input);
                }
            }
        }

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
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.

Tags
Spreadsheet
Asked by
Roger
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or