Excel-Export Read-only problem

8 views (last 30 days)
Hello,
my created excel file is read-only, how i can change it? Here is my code:
function params = create_log_file(params)
if isfolder(params.savepath_log)
savepath_log = params.savepath_log;
else
savepath_log = pwd;
end
filename = params.logf;
filename = [savepath_log '/' filename];
c = cell(6, 2);
c{1,1} = 'Block-Nr';
c{2,1} = 'Gravur';
c{3,1} = 'Foliendicke';
c{4,1} = 'Anzahl Blätter (SOLL)';
c{5,1} = 'Aktuelle Anzahl Blätter:';
c{6,1} = 'Durchschnittliche Zeit pro Blatt (s)';
c{1,2} = params.bnr;
c{2,2} = params.inputs.bandtype;
c{3,2} = params.ft;
c{4,2} = params.nst;
c{5,2} = num2str(params.ns);
c{6,2} = sprintf('%.2f', 0.00);
writecell(c, filename,'Range', 'A1:B6');
c1 = cell(1,7);
c1{1,1} = 'Blatt-Nr.';
c1{1,2} = 'Datum/Uhrzeit ';
c1{1,3} = 'Zeit pro Blatt (s)';
c1{1,4} = 'Linienbreite';
c1{1,5} = 'Original Bild (Link)';
c1{1,6} = 'Berechnungsergebnis Kantenerkennung (Link)';
c1{1,7} = 'Bild Positionierung (Link)';
writecell(c1, filename,'Range', 'A8:G8');
sp = ' ';
sp = [sp sp sp];
writecell({sp, sp, sp}, filename,'Range', 'E7:G7');
% - Create an Excel object.
params.ex = actxserver('excel.application');
% - workbook.
params.ew = params.ex.Workbooks;
% - open file
params.ef = params.ew.Open(filename);
% - get sheet
params.es = params.ef.Sheets.Item('Sheet1');
function params = reopen_log_file(params)
if isfolder(params.savepath_log)
savepath_log = params.savepath_log;
else
savepath_log = pwd;
end
filename = params.logf;
filename = [savepath_log '/' filename];
% - reopen the Excel object.
params.ex = actxserver('excel.application');
% - workbook.
params.ew = params.ex.Workbooks;
% - open file
params.ef = params.ew.Open(filename);
% - get sheet
params.es = params.ef.Sheets.Item('Sheet1');
  3 Comments
Tommy Schumacher
Tommy Schumacher on 9 May 2021
mmmh the first saved excel file is always read only, the next files are ok. Dont know why.
So my code is ok ?
Walter Roberson
Walter Roberson on 9 May 2021
Any time you invoke Excel directly using ActiveX, you run into the risk that a file you worked with earlier might still be open in MATLAB due to buffering for efficiency. There is not a lot you can do about it other than not using MS Windows.
However, since MATLAB knows about it own buffering, a potential workaround would be to copyfile() the file and work with the copied version in Excel.

Sign in to comment.

Accepted Answer

Aleksei Kukin
Aleksei Kukin on 1 Jun 2021
When you open the workbook use additional parameters.
Try change "params.ew.Open(filename)" by "params.ew.Open(filename, 0, false)"

More Answers (0)

Community Treasure Hunt

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

Start Hunting!