Hi @Yiling,
Let me address your query regarding, “I just want Matlab to create a temp Excel file, open it, write Matlab table data to it, let it hang on the screen, but without saving it to some location. Then user could manually save this Excel file to the desired location.This is a question related to the base Matlab. But eventually I would like to realize this on Matlab App Server - when users click download data from the server, an Excel object is opened with the data on it, and users can then manually save the file to some folder.”
Follow the steps mentioned below.
Step#1: I will start by preparing the generic data to export to Excel.
% Sample data data = table([1; 2; 3], {'A'; 'B'; 'C'}, 'VariableNames', {'ID', 'Name'});
Step#2: Then, use actxserver to create an instance of Excel that will allow you to manipulate the application programmatically.
% Create an ActiveX server for Excel excelApp = actxserver('Excel.Application');
For more information on actxserver function, please refer to
https://www.mathworks.com/help/matlab/ref/actxserver.html
Step#3: Generate a new Excel workbook where you will write the data.
% Add a new workbook workbook = excelApp.Workbooks.Add();
Step#4: Transfer the MATLAB table data into the active worksheet.
% Access the first worksheet sheet = workbook.Sheets.Item(1);
% Write the table data to the Excel sheet % Convert the table to a cell array for easier writing dataCell = [data.Properties.VariableNames; table2cell(data)]; sheet.Range('A1').Resize(size(dataCell, 1), size(dataCell, 2)).Value = dataCell;
Step#5: Now, make sure that the Excel application is visible to the user, allowing them to interact with it.
% Make Excel visible to the user excelApp.Visible = true;
At this point, the temporary file will remain open, so users can save it wherever they choose. To guide users on how to save the file, you can use a message box before opening Excel:
% Display instruction message msgbox('The data is now open in Excel. To save the file, go to File > Save As,and choose your desired location.', 'Instructions', 'modal');
If you're implementing this in a MATLAB App Server context, make sure that the server environment has the necessary permissions to launch Excel and that the users have Excel installed on their local machines. If you have any further questions, please let me know.