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

조회 수: 1 (최근 30일)
Sai Ganesh  Erva Ram
Sai Ganesh Erva Ram 2016년 8월 3일
답변: meghannmarie 2019년 9월 30일
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);

답변 (2개)

Pruthvi G
Pruthvi G 2019년 9월 30일

meghannmarie
meghannmarie 2019년 9월 30일
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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by