Data written to wrong cell in excel using actxserver
显示 更早的评论
Hello - I am writing data from matlab to an excel file using the actxserver tool. I have a few layers of headers I am entering before entering the data. When entering the following code the last header line "Business Name" gets written to the A4 location, NOT the A3 location (where the code should be sending it). If however I change "A3" to "A2" for the location of the "Business Name" it gets written to (the desired but not coded) cell A3.
if true
% code
e = actxserver('Excel.Application');
eWorkbook = e.Workbooks.Add;
e.Visible = 1;
eSheets = e.ActiveWorkbook.Sheets;
eSheet1 = eSheets.get('Item',1);
eSheet1.Activate
eSheet1.Name = 'Smith';
% Format Cells - Top Row
eSheet1 = eSheet1.get('Range', 'A1');
eSheet1.Value = 'Smith County';
eSheet1.Font.Bold = 1;
% Format Cells - 2nd row
eSheet1 = eSheet1.get('Range', 'A2');
eSheet1.Value = 'Contract type 1';
eSheet1.Font.Bold = 1;
% Format cells - 3rd row
eSheet1 = eSheet1.get('Range', 'A3');
eSheet1.Value = 'Business Name';
end
This shifting of the cell entry point propagates down and is repeated as more information is added. Has anyone encountered this before? Is this a bug or a feature?
3 个评论
Greg
2017-11-27
You keep reusing eSheet1. This would be ok except it starts as a WorkSheet object, then becomes a Range object. Can't imagine indexing into 'A3' of a single cell makes any sense...
It works for me when I use a different variable for the Range objects.
Emily Carlson
2017-11-27
Greg
2017-11-27
=)
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!