writecell messes up merge and center when writing to Excel

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.

10 Comments

hello
maybe you should share your code and some excel files showing the issue and what format it should have.
Here is the most basic illustration of the issue:
clear
clc
writecell({2},'Sample.xlsx','Range','C5')
Where Sample.xlsx has merged cells at C5 (see attached file).
When you run the code and open the file, the 2 is in C5, the the merge/center operation has been undone and the column width has been autofit to the data. I also added a fill color to see if it was wiping all formatting, but that was unchanged.
that piece of code gives me the right result (attached)
??
The problem is that the cells that were merged in the original file C5:C11, are no longer merged.
sorry , but that cannot be the result of the code provided ...
writecell({2},'Sample.xlsx','Range','C5')
The picture is of the original (blank) file with the cells merged. I simply posted it to show that the cells in the original file were merged. You posted the result, which has a 2 in C5 but cells C5:C11 are unmerged.
ok I have to admit I never used the "merging" capabilities of the old xlswrite functions - and beside that the documentation of writecell does not provide much info on this either
I have the same issue. Is there any solution to this?
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.
xlswrite is considerble slower but it does not merge the cells.
Also if you get the effort to start active X stay there, and do all in one run

Sign in to comment.

Answers (0)

Products

Release

R2020a

Asked:

on 24 Mar 2021

Community Treasure Hunt

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

Start Hunting!