Applying a border to Excel cells when using COM

조회 수: 54 (최근 30일)
Kenneth Sandberg
Kenneth Sandberg 2016년 11월 2일
댓글: Jared 2024년 10월 23일
I am writing simulation results from Matlab to Excel. In order to make it easier to read I apply some formating to the output. It works fine to change font size, font color, number format etc but applying a border around a selected number of cells doesn't work.
Example:
% -- Create excel sheet --
AppObj = actxserver('Excel.Application');
AppObj.Visible = true;
WkbkObj = AppObj.Workbooks;
DataWkbkObj = WkbkObj.Add;
DataWkbkObj.Sheets.Add().Name = 'Test';
DataSheetObj = DataWkbkObj.Sheets.Item('Test');
% -- Write some data --
DataSheetObj.Range('B2').Value = 43;
DataSheetObj.Range('B3').Value = 5;
DataSheetObj.Range('B4').Value = 7;
% -- Apply blue color --
DataSheetObj.Range('B2:B4').Font.Color = -4165632;
% -- Change number format --
DataSheetObj.Range('B2:B4').NumberFormat = '0,00';
% -- Apply a line on the left side --
DataSheetObj.Range('B2:B4').Borders('xlEdgeLeft').LineStyle = 'xlContinuous';
DataSheetObj.Range('B2:B4').Borders('xlEdgeLeft').Weight = 'xlMedium';
The last two lines doesn't work. The problem is the 'xlEdgeLeft' part. I have tried to analyze the object and use constant values without result. Any ideas?

채택된 답변

Abhinav Gurram
Abhinav Gurram 2016년 11월 11일
To apply specific borders to a range of cells in Excel, you would be required to use the 'Borders.Item' property that returns a Border object, as listed in the MSDN reference here: Borders.Item Property.
In order to set the border style for the range of cells in your example program, you can modify the last two statements as:
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').LineStyle = 1;
DataSheetObj.Range('B2:B4').Borders.Item('xlEdgeLeft').Weight = -4138;
Hope this helps!
  댓글 수: 2
Steven
Steven 2019년 9월 27일
This does not work on R2019a. I get this error:
Expected one output from a curly brace or dot indexing expression, but there were 5 results.
Jared
Jared 2024년 10월 23일
I know this is eight (8) years late, but this will be edify anyone else having this problem with all of the Excel Object library functionality not being available to MATLAB through the COM (component object model) operator (via actxserver).
Most of these types of inputs must be referred to by their Excel enumeration constant when operating from MATLAB through the COM operator.
This link has an exhaustive list of these constants: http://www.smarterdatacollection.com/Blog/?p=374
This is Microsofts list of these constants (but it isn't as exhaustive as the one above): https://learn.microsoft.com/en-us/office/vba/api/excel.constants
So replace 'xlEdgeLeft' with numeral 7.
DataSheetObj.Range('B2:B4').Borders.Item(7).LineStyle = 1;
DataSheetObj.Range('B2:B4').Borders.Item(7).Weight = -4138;

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by