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

9 ビュー (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by