writecell messes up merge and center when writing to Excel

조회 수: 36 (최근 30일)
Matt C
Matt C 2021년 3월 24일
편집: Fernando Meo 2023년 10월 17일
I tried to switch some of my old xlswrite functions over to writecell as this newer function seems superior in terms of efficiency, however I found that when writing data to cells that have already been merged and centered in an Excel file, writecell wipes out the merge/center. In fact, it seems like writecell wipes other formatting as well (column widths are resized to fit the data). This is pretty annoying as I am just trying to write cell data in specific cells without altering the formatting of the file.
xlswrite does not have this issue. It just puts the data in there.
Any workarounds that don't involve activexserver would be appreciated. At a minimum I would like to understand why this is happening.
  댓글 수: 9
Mikel Arrinda
Mikel Arrinda 2023년 3월 31일
I have the same issue. Is there any solution to this?
Fernando Meo
Fernando Meo 2023년 10월 17일
편집: Fernando Meo 2023년 10월 17일
To preserve the Excel format, use the name aurguments as follows
writecell(CellData , FileName, 'Sheet','Sheet1', 'Range', 'A6:S176', 'AutoFitWidth', false, 'UseExcel', true)
However, when writing to cells which were merged, they are unfortuntely wiped out.
A workaround is to manually remerge them using the following:
e = actxserver('Excel.Application');
Workbook = e.Workbooks.Open(FileName);
e.Visible = 1;
Sheet1 = Workbook.Sheets.Item(SheetName);
Range = Sheet1.Range('I2:N2');
Range.MergeCells = true;
Workbook.Save;
Workbook.Close;
This should be an enhancement request for mathworks.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by