Copy a sheet from template excel & fill the data to the copied sheet

7 Ansichten (letzte 30 Tage)
Kavinprasad M
Kavinprasad M am 6 Okt. 2022
Kommentiert: Image Analyst am 10 Okt. 2022
i have created a small matlab app in which i have an issue with copying data
The issue is i have an excel template & i want to copy the excel template , then fill the data in that template, it works within matlab but once i convert it into desktop app, it only copies the template & it doesn't fill in the data, Kindly help me resolve this
Thanks in advance :)
[filename,folder] = uiputfile("SSE_Report.xlsx");
tblRH = table(app.RHFTorque90AngleEditField.Value,app.LHRTorque90AngleEditField.Value,'VariableNames',{'R','L'});
writetable(tblRH,fullfile(folder,filename),'Sheet','Static Steering Effort','Range','F22:G23',AutoFitWidth=false)
tblCentre = table(app.FTorque0AngleEditField.Value,app.RTorque0AngleEditField.Value,'VariableNames',{'R','L'});
writetable(tblCentre,fullfile(folder,filename),'Sheet','Static Steering Effort','Range','F19:G20',AutoFitWidth=false)
tblLH = table(app.RHFTorque100AngleEditField.Value,app.LHRTorque100AngleEditField.Value,'VariableNames',{'R','L'});
writetable(tblLH,fullfile(folder,filename),'Sheet','Static Steering Effort','Range','F25:G26',AutoFitWidth=false)
xlswritefig(app.UIAxes2,fullfile(folder,filename),'Static Steering Effort','C18')
hold("on");
copyfile("Template_Excel.xlsx","SSE_Report.xlsx");
  2 Kommentare
dpb
dpb am 6 Okt. 2022
Bearbeitet: dpb am 6 Okt. 2022
You're writing the updates to whatever is the result of fullfile(folder,filename) but then then you copy over the template file to the equivalent of fullfile(pwd,"SSE_Report.xlsx"). If folder and pwd are the same, you'll have just wiped out everything you just did.
The app almost certainly will start in a different working directory than the one you've been in during development.
In
copyfile("Template_Excel.xlsx","SSE_Report.xlsx");
both filenames should be fully-qualified file names and the copy of the template to the new working file should be before making the updates to not wipe out the changes made.
As one coding stylistic note, I'd suggest to keep with the same input/output paradigm and modern suggested use and replace the xlswrite call with writematrix or writelines.
Also, it would be more efficient to create a single table of the results from F19:G26 and write them once instead of what is now three separate open/write/close cycles. If there's a header in between in the template you could read it on startup and build a cell array of the region and use writecell instead.
Kavinprasad M
Kavinprasad M am 7 Okt. 2022
i understand that and even with copying the template before & writing the data , it doesn't work that way. it only executes writing the data whereas copying the template is missing.
As for creating it in one matrix, i understand but in future it is to be edited & copied in different sheets so i meant to seperate them now to reduce hassle
Is there any other way to make this work?, please let me know

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 7 Okt. 2022
Bearbeitet: Image Analyst am 7 Okt. 2022
You need to have copy file in advance then write to it.
% Ask use for a filename for our output data
[filename,folder] = uiputfile("SSE_Report.xlsx");
outputFile = fullfile(folder, filename)
% Define the tamplate Excel workbook.
templateFile = fullfile(programFolder, 'Template_Excel.xlsx')
% Copy over template file, if it exists, to output file.
if isfile(templateFile)
% Get rid of any existing file. Put it in the recycle bin.
if isfile(outputFile)
recycle on;
delete(outputFile)
end
% Create an output file based on the template.
copyfile(templateFile, outputFile);
else
% Let user know we didn't find the template. You will still get an
% output file though, though not one based on the template.
warningMessage = sprintf('Warning: template file not found:\n%s', templateFile);
uiwait(warndlg(warningMessage));
end
% Now create all your data and write it out in one or more writematrix or
% writetable commands.
writetable(yourDataTable, outputFile);
  12 Kommentare
Image Analyst
Image Analyst am 9 Okt. 2022
Yes, as @dpb says, I'd use a regular .xlsx file as your template, not a .xlt file. Not sure but if you used an .xlt file it may have quirky things about asking you to save to a different filename than the one it has. And it's also a good suggestion to do control-shift-Esc to see task manager and make sure there are not any zombie Excel instances running before you run your program.
As far as pasting into MS Office apps, see the attached two programs. In ExcelDemo you'll find this:
%==========================================================================================================================
% Create a figure window with a plot (axes control) on it and paste it into the Excel worksheet.
% Adapted from https://www.mathworks.com/matlabcentral/answers/91547-how-do-i-insert-a-matlab-figure-into-an-excel-workbook-through-activex
function PasteFigureIntoExcel(Excel)
try
% Create sample image from figure
hFig = figure; % Bring up a new, separate figure.
period = pi;
x = linspace(-2*pi, 2*pi, 400);
mySineWave = 10 * cos(2 * pi * x / period);
% Plot it.
plot(x, mySineWave, 'b-', 'LineWidth', 2);
xlabel('x', 'FontSize', 20);
ylabel('y', 'FontSize', 20);
title('My Sine Wave', 'FontSize', 20);
grid on;
% Create a filename to save the figure window to disk for temporarily.
tempImageFullFileName = fullfile(pwd, 'Delete_me.png');
print('-dpng', tempImageFullFileName); % Save it to disk.
uiwait(msgbox('We saved this plot to disk and will paste it into the workbook.'));
close(hFig); % Close down figure because we don't need it anymore.
% Alternative 1 BEGIN.
% Get handle to Excel COM Server
% Excel = actxserver('Excel.Application');
% % Set it to visible
% set(Excel,'Visible',1);
% Optionally, add a Workbook
% Workbooks = Excel.Workbooks;
% Workbook = invoke(Workbooks, 'Add');
% Get a handle to Sheets and select Sheet 1
% Sheets = Excel.ActiveWorkBook.Sheets;
% Sheet1 = get(Sheets, 'Item', 1);
% Sheet1.Activate;
% Alternative 1 END.
% Alternative 2 BEGIN.
% Use whatever sheet is active at the moment. This should be setup in advance of calling this function.
currentSheet = Excel.ActiveSheet;
% Alternative 2 END.
% Alternative 1 BEGIN.
% Get a handle to Shapes for Sheet 1
Shapes = currentSheet.Shapes;
% Add image by importing one from an image file on disk. Place at a certain position. Last 4 arguments are : x, y, width, height.
Shapes.AddPicture(tempImageFullFileName, 0, 1, 200, 40, 300, 235);
% Alternative 1 END.
xlMoveAndSize = 1;
Selection.Placement = xlMoveAndSize;
% Alternative 2 BEGIN.
% Add image
% Sheet1.invoke('Pictures').Insert(tempImageFullFileName);
% Alternative 2 END.
% Save the workbook and Close Excel
% invoke(Workbook, 'SaveAs', fullfile(pwd, '\myfile.xls'));
% invoke(Excel, 'Quit');
% Delete the temporary image file.
delete(tempImageFullFileName);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
WarnUser(errorMessage);
end
return;
Of course you'll first have to get the Excel object using ActiveX.
FInally you might take a look at the functions copygraphics and exportgraphics.
dpb
dpb am 9 Okt. 2022
"you have write permission to the outputfile location"
I've run into all kinds of OS-set access problems on Win10 that I don't fully understand just what it does and why/how and particularly how to beat it into submission in writing to locations containing executables; it appears by default install on it makes those require Admin privileges. I wonder if the MATLAB distribution of the executables is doing something of that sort as well; I've not tried to deploy a compiled app on this machine; on the uni machine for which I wrote a couple apps I had to have IT log in to be able to even get the executable on the system; it was locked down so tightly couldn't even copy over the testing directory from local machine...

Melden Sie sich an, um zu kommentieren.


Kavinprasad M
Kavinprasad M am 10 Okt. 2022
Even without the xlswritefig, it seems to have some issues with excel as a standalone app in windows 11. i have template file as .xlsx, i'll change the way i aquire data from table & try it again
Thankyou guys >.<

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by