Read-only Excel problems

6 Ansichten (letzte 30 Tage)
Mash
Mash am 30 Mär. 2020
Bearbeitet: Mash am 31 Mär. 2020
Hello,
I got a few code lines from this forum to the following thing:
  • Delete a everything a certain range on sheet 3 (works)
  • define a certain range for writing (works)
  • write on sheet 3 (works)
  • adjust column width on sheet 3 (not working)
I do end up with Read-only documents and wirting problems. Also the column width does not work. File is the parth and document. The last bit on the end, what is marked as a commit, does not work. I got it from this thread. The Code:
% Alte Daten in Excel löschen
t1 = readtable(file, "Sheet",3);
Matrix_size = size(t1);
firstRow = 5; % Zeile 1 bis 3 sind Header
lastRow = Matrix_size(1,1) +1; % Zahl der Datenreihen, +1 gegen das Löschen der Zeile 4
% file = 'C:\Users\Frieder\Nextcloud\IFB_Studienarbeit\Matlab\EinAusgabewerte.xlsx';
sheet = 'Zwischenwerte'; %can be name or numeric index
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(file);
worksheet = workbook.Worksheets.Item(sheet);
% opts = spreadsheetImportOptions("C:\Users\Frieder\Nextcloud\IFB_Studienarbeit\Matlab\EinAusgabewerte.xlsx", 'PreserveVariableNames', false );
cellRange = [num2str(firstRow),':',num2str(lastRow)] % Die zu löschenden Excelzeilen
worksheet.Rows.Item(cellRange).Delete;
workbook.Save;
excel.Quit();
fileattrib(file,'+w') % Schreibschutz aufheben
Matrix_size = size(Matrix_Zwischenergebnisse);
firstRow = 5; % Zeile 1 bis 4 sind Header
lastRow = Matrix_size(1,1)+4; % Zahl der Datenreihen
firstCol = 'A'; % Schreibt in Spalte A
lastCol_letter = Matrix_size(1,2);
if lastCol_letter > 26 % Für Matritzen größer 26
lastCol_letter = lastCol_letter - 26
lastCol = char('A' + lastCol_letter) - 1; % Da lastCol_letter auf A aufaddiert wird, muss 1 abgezogen werden
cellRange = [firstCol,num2str(firstRow),': A',lastCol,num2str(lastRow)] % Die zu beschreibenen Excelzeilen
elseif lastCol_letter > 52
lastCol_letter = lastCol_letter - 52 % Für Matritzen größer 52
lastCol = char('A' + lastCol_letter) - 1; % Da lastCol_letter auf A aufaddiert wird, muss 1 abgezogen werden
cellRange = [firstCol,num2str(firstRow),': B',lastCol,num2str(lastRow)] % Die zu beschreibenen Excelzeilen
else
lastCol = char('A' + lastCol_letter) - 1; % Da lastCol_letter auf A aufaddiert wird, muss 1 abgezogen werden
cellRange = [firstCol,num2str(firstRow),':',lastCol,num2str(lastRow)] % Die zu beschreibenen Excelzeilen
end
writecell(Matrix_Zwischenergebnisse,file,'Sheet',3,"Range",cellRange); % schreibt die matrix
%hExcel = actxserver('Excel.Application')
%hWorkbook = hExcel.Workbooks.Open(file)
% Select the entire spreadsheet.
%hExcel.Cells.Select;
% Auto fit all the columns.
%hExcel.Cells.EntireColumn.AutoFit;
% Center align the cell contents.
%hExcel.Selection.HorizontalAlignment = 3;
%hExcel.Selection.VerticalAlignment = 2;
% Put "cursor" or active cell at A1, the upper left cell.
%hExcel.Range('A1').Select;
%excel.Quit();
%fileattrib(file,'+w') % Schreibschutz aufheben
  2 Kommentare
dpb
dpb am 30 Mär. 2020
"I do end up with Read-only documents and wirting problems."
What happens w/o the commented code if you restart MATLAB?
It can happen that ActiveX processes that fail for some reason can leave files open which could be the cause of your symptoms.
Also, ensure there are no orphaned/background Excel processes in the background that haven't closed successfully; you'll have to use the TaskManager under Windows to see them; if there are other instances still showing, kill them.
Mash
Mash am 31 Mär. 2020
Bearbeitet: Mash am 31 Mär. 2020
With the auto width active, there is an Excel background prozess going on, even with
excel.Quit();
fileattrib(file,'+w'); % Schreibschutz aufheben
If I try to write:
Error using writecell (line 127)
Unable to write to file 'C:\Users\Frieder\Nextcloud\IFB_Studienarbeit\Matlab\EinAusgabewerte.xlsx'. You may not have write permissions or the file may
be open by another application.
Error in Verschleissberechnung_2_9/BerechnenButtonPushed (line 403)
writecell(Matrix,file,'Sheet',2,"Range",cellRange); % schreibt die matrix
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by