MATLAB Answers

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

27 views (last 30 days)
Sai Ganesh  Erva Ram
Sai Ganesh Erva Ram on 3 Aug 2016
Answered: meghannmarie on 30 Sep 2019
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
% Initiates Excel COM Object
Excel = actxserver ('Excel.Application');
set(Excel, 'Visible', 1);
% 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));
% Saves the template to the user selected output file
invoke(template, 'Close');
% Quit Excel
invoke(Excel, 'Quit');
% End process

Answers (2)

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;
Excel.Quit; % Quit excel application
delete(Excel); % Delete the handle to the application

Community Treasure Hunt

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

Start Hunting!