Copy an Excel worksheet from one workbook to another with Matlab

123 ビュー (過去 30 日間)
Franziska
Franziska 2019 年 9 月 4 日
回答済み: Pruthvi G 2019 年 9 月 30 日
Hi everyone,
I hope you can help me with this problem. I need to copy a worksheet of workbook1 to another workbook2 with exact the same structure/format. There are empty cells and labels in between which I need to copy as well. I tried to save the range in a variable, but that doesn't work for me. I would like to do this via an ActiveX server. Is this possible and what would the code look like? Thanks in advance!
Greetings

採用された回答

Guillaume
Guillaume 2019 年 9 月 4 日
You may as well write a macro in excel because you'd be using the same code in matlab:
excel = actxserver('Excel.Application'); %start excel
wbsource = workbooks.Open('C:\somefolder\somexcelfile.xlsx');
wbdest = workbooks.Open('C:\somefolder\someotherexcelfile.xlsx');
ws = wbsource.Worksheets.Item('NameOfSheet'); %or you can you the sheet index: ws = wbsource.Worksheets.Item(index);
ws.Copy(wdest.Worksheets.Item(1)); %copy worksheet before 1st worksheet of destination workbook
wdest.Save %save destination workbook
excel.Quit %quit excel
I've just written this on the fly. It's untested there may be bugs.Use MSDN to find the right functions to call.
  1 件のコメント
Franziska
Franziska 2019 年 9 月 5 日
Thank you very much! This works perfectly! :)

サインインしてコメントする。

その他の回答 (2 件)

Pruthvi G
Pruthvi G 2019 年 9 月 30 日

Neuropragmatist
Neuropragmatist 2019 年 9 月 4 日
Why does readtable followed by writetable not work?:
If the spreadsheets dont have to be in the same file you could also use movefile to just duplicate the original:
Sorry I don't really know about activeX servers.
Hope this helps,
M.
  2 件のコメント
Image Analyst
Image Analyst 2019 年 9 月 4 日
Does readtable retain formatting? Like font size, color, background fill, border sizes, etc.? I'd doubt it.
Neuropragmatist
Neuropragmatist 2019 年 9 月 6 日
No you are right it doesn't...

サインインしてコメントする。

カテゴリ

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