Load txt data into Excel with QueryTables.Add

13 views (last 30 days)
I try to load data from a txt file into Excel via Matlab code. I try to call the Excel text file import wizard from Matlab using COM objects. The following code runs without any errors, but the data is not importet to Excel. Does anybody has an idea where I made the mistake?
Matlab-Code:
% Get handle to Excel COM Server
Excel = actxserver('Excel.Application');
% Set Excel to invisible
set(Excel,'Visible',0);
% Open a Workbook
WB=Excel.Workbooks.Open('C:\xlsfile.xls');
% select Worksheet
WS = WB.Worksheets;
Sheet = get(WS,'item',1);
Sheet.Activate;
% Select range in Worksheet
Rng = get(Sheet,'Range','$A$1');
% Import txt file into xls file
Connection = ['TEXT;' 'C:\textfile.txt'];
QT = Sheet.QueryTables.Add(Connection,Rng);
QT.Name = 'C:\textfile.txt';
% Save Workbook
WB.Save();
% Quit Excel
Excel.Quit();

Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!