how to add a new page in an excel sheet and put data in it

35 Ansichten (letzte 30 Tage)
Christophe Nell
Christophe Nell am 22 Sep. 2015
Beantwortet: Rutuja Shirali am 30 Sep. 2015
Hello, I want to select two specific lines from an excel sheet and past then in a new page in an existing excel file but in a new page. My script is:
[Path] = OH_GetPath('ClusteringASS');
Mainfolder = Path.ASCIXLStruct;
%[status,sheets] = xlsfinfo(FileName);
[num txt raw] = xlsread([ Mainfolder filesep BBPFileName]);
Names = raw(1,:);
[token, remain] = strtok(FileName, '_');
[token2, remain] = strtok(remain, '_');
[token3, remain] = strtok(remain, '_');
CellID = [token '_' token2 '_' token3];
for line=2:1:size(raw,1)
if(size(raw{line,2},2) >= size(CellID,2))
if(1 == strcmp(CellID, raw{line,2}(1:size(CellID,2))))
Values = raw(line, :);
end
end
end
% Connect to Excel
Excel = actxserver('excel.application');
% Get Workbook object
WB = Excel.Workbooks.Open(fullfile(pwd, [FileName '.xlsx']), 0, false);
% Get Worksheets object
WS = WB.Worksheets;
% Add after the last sheet
WS.Add([], WS.Item(WS.Count));
WS.Item(WS.Count).Name = 'BBP';
% Save
WB.Save();
% Quit Excel
Excel.Quit();
xlswrite([Mainfolder filesep FileName],Names ,'BBP')
xlswrite([Mainfolder filesep FileName],Values ,'BBP')
The new page is named 'BBP' and the data are Names and Values. But it will create a new excel file with the same name and my data but not use the existing excel file

Antworten (1)

Rutuja Shirali
Rutuja Shirali am 30 Sep. 2015
Hi Christophe,
I tried running your code and it runs fine and creates a new excel sheet in the same workbook. I noticed in your code that while opening the Excel workbook, you open it from "pwd" please make sure when you are using "xlswrite" you are in the same directory as the workbook is or specify the entire path to the workbook (in this case pwd), else it will create a new workbook with the filename and sheetname specified and write to it.
I hope this helps!
-Rutuja

Community Treasure Hunt

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

Start Hunting!

Translated by