Applying a border to Excel cells when using COM
70 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Kenneth Sandberg
le 2 Nov 2016
Commenté : Steven
le 27 Sep 2019
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?
0 commentaires
Réponse acceptée
Abhinav Gurram
le 11 Nov 2016
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!
1 commentaire
Steven
le 27 Sep 2019
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!