File Exchange Pick of the Week

Our best user submissions

Control Excel using ActiveX 1

Posted by Jiro Doke,

Jiro's pick this week is The X Collection by Yvan Lengwiler.

My day job is a customer training engineer. I get to take people who have never used MATLAB or Simulink to the point where they feel comfortable in applying the tools in their day-to-day work. It's certainly a rewarding role.

Recently during my MATLAB Fundamentals training course, I received a few questions regarding how to exchange data between MATLAB and Microsoft® Excel®. In the training, we talk about functions like xlsread and xlswrite. The person wanted the ability to control the formatting of cells, which xlswrite does not provide. Specifically, he wanted to change the width of a column and some of the cell colors. I pointed him to the function actxserver, which provides a way to connect to Windows® applications using ActiveX. Once connected to Excel, he can use the various APIs that are provided by Excel to customize. The reference page for actxserver has an example for connecting to Excel.

I started browsing the File Exchange in hopes for finding additional examples that I could point people to for using actxserver with Excel. Sure enough, I found quite a few entries, including this one (and many others) by our very own Brett Shoelson. I chose to highlight Yvan's X Collection, because he provides a suite of functions for performing various simple tasks within Excel, using ActiveX. He uses intuitive function names, such as XConnect, XOpenBook, and XAddSheet. Each of these functions take in various handles (Excel application, workbook, or worksheet) as inputs, so it's easy to understand how the functions are related to each other. In fact, if you open the files in the MATLAB Editor, you can place the cursor at one of the handle variables to see how they are used or created. The editor will highlight the same variables, so you can easily pick out where they are used.

One suggestion I have for improvement is to convert this into a class, instead of a suite of functions. Naturally, these functions operate on handles that represent the Excel application, workbooks, and worksheets. Each of them can be a class, and each class can contain methods that specifically operate on the instances of the class. This will make the functions inherently invisible to everyone except to the class instances. You wouldn't have to catch errors resulting from invalid handle inputs. Of course, you may still need to catch other errors that may occur. The other reason for creating a class in this case would be to hide (encapsulate) the actual handles. The hExcel returned from XConnect is a COM.Excel_Application.

hExcel = XConnect()
hExcel =
	COM.Excel_Application

This handle exposes the user to various methods and properties provided by the Excel application.

This may be good or bad, but perhaps the intent is for users to use the functions provided by this suite and not the ones that are natively available. By creating a class that maintains the handles internally as a property, it protects the users from misusing the handle.

Nonetheless, this is a great suite of functions for connecting MATLAB with Excel. I'm not doing justice to Yvan's entry if I don't mention the true motivation for him creating this. If you need to repeatedly read from or write to an Excel file, xlsread or xlswrite would suffer a performance hit because of the repeated connect/disconnect that it's doing with Excel. With Yvan's XCollection, you connect to Excel once, do the necessary read/write operations, and disconnect once.

Note that Yvan's entry does not provide the functionality the my training customer was asking for, but I pointed him to this entry as a starting point.

Comments

Yvan provides detailed documentation of how to use the functions. Give this a try and let us know what you think here or leave a comment for Yvan.


Get the MATLAB code

Published with MATLAB® R2015a

48 views (last 30 days)  | |

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.