MATLAB Answers

saving excel file as a new sheet in another excel file using matlab

26 views (last 30 days)
Sai Ganesh  Erva Ram
Sai Ganesh Erva Ram on 3 Aug 2016
Answered: meghannmarie on 30 Sep 2019
Hi
How can i save a excel file (which contains only 1 sheet,i.e template with chart and formatting) as a sheet in the existing excel file using matlab
I tried some examples using activex but it copies only the cells from the file,
The following code is from Matlab forums which saves a copy of the same excel file with different name
I am expecting that my file is copied into a different excel file as a sheet with all the formatting and charts
close all
clear all
% Opens the data analysis template
Template='C:\Users\*****\Documents\misc\Analysis_template.xlsx';
% Initiates Excel COM Object
Excel = actxserver ('Excel.Application');
set(Excel, 'Visible', 1);
template=invoke(Excel.Workbooks,'Open',Template);
% Allows user to select output file
Exportdir=uigetdir('','Please select export directory');
prompt = {'Please enter EXCEL File name:'};
name = 'Data Output';
numlines = 1;
defaultanswer = {['fianl','_','report']};
options.Resize = 'on';
options.WindowStyle = 'normal';
options.Interpreter = 'tex';
FileName = cell2mat(inputdlg(prompt,name,numlines,defaultanswer,options));
File=fullfile(Exportdir,FileName);
% Saves the template to the user selected output file
invoke(template,'SaveAs',File);
invoke(template, 'Close');
% Quit Excel
invoke(Excel, 'Quit');
% End process
delete(Excel);

  0 Comments

Sign in to comment.

Answers (2)


meghannmarie
meghannmarie on 30 Sep 2019
This will copy workseet from InputExcel and paste it after last sheet in OutputExcel:
InputExcel = 'D:\Answers\input.xlsx';
OutputExcel = 'D:\Answers\output.xlsx';
Excel = actxserver ('Excel.Application');
InputExcelWorkbook = Excel.workbooks.Open(InputExcel); % Excel 2010+
OutputExcelWorkbook = Excel.workbooks.Open(OutputExcel);
count = OutputExcelWorkbook.Sheets.Count;
InputExcelWorkbook.Worksheets.Item(1).Copy([],OutputExcelWorkbook.Worksheets.Item(count));
OutputExcelWorkbook.Save;
OutputExcelWorkbook.Close;
InputExcelWorkbook.Close;
Excel.Quit; % Quit excel application
delete(Excel); % Delete the handle to the application

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by