Posts tagged ‘c++’

Adventures in C#: Excel automation

So I’ve been working a fair bit with C# and Excel automation, and I’ve picked up a few things from the whole experience so far - namely that the thing is really poorly put together and requires a whole lot of work to make things work nicely.

The emphasis there is on nicely. You can hack together something real quick in about 5 minutes that probably almost does what you want, and will be good enough if you just need the job done. However, the Excel COM uses about 3 different standards and each time you get to guess which one you’ll need to use.

Over the next few days [or weeks] I’m going to outline a few of the problems I encountered and how I went about fixing them:

Problem: The syntax for inserting a column and changing a column’s width is completely different

This might not seem to big of an issue until you realize that:

column.Insert(); exists, but:
column.Width = 10; or column.setWidth(10); or anything like that, does not exist.

The actual solutions to do these tasks are as follows. Note, they reference the variable oSheet is the Excel.Worksheet you’re working with. These samples assume a basic knowledge of C# and using the Excel COM.

Inserting a columm:

Excel.Range rng = (Excel.Range)oSheet.Cells[rowNumber, colNumber];
Excel.Range column = rng.EntireColumn;
column.Insert(Excel.XlInsertShiftDirection.xlShiftToRight, false);

This will insert a column to the left of row rowNumber.

Changing a column’s width:

((Excel.Range)oSheet.Columns["A", Type.Missing]).ColumnWidth = 10;

This will change column A’s width.

Notice that one method uses numeric identifiers for rows, while the other uses a string. Also, interestingly enough, its a bit of a pain to actual get a hold of a column using either method.

Moving out in a month, this time for real

It’s notable how the tone of this site has changed recently. Most of my posts lately are about the wireless industry in Canada, or other hot button topics that I can rant about without giving a personal view of the situation - apart from being a CS student with clearly too many cell phones, today’s posts aren’t all about me.

Offtopic: Perhaps this is a good thing: I was accosted several weeks ago by someone I attended high school with and told “I shouldn’t write about people that I don’t know anything about.” The world does not work this way, and “know[ing] anything” was misused in place of “don’t say anything if you don’t have anything nice to say tell me what I want to hear.” Likewise, hilarity would not ensue if I were prevented from writing about Soulja Boy.

(I did indeed attend a semester of classes with this individual, which is a decent basis for knowing at least SOMETHING about someone. For what it’s worth, I don’t even remember the post, and a quick perusal of my former site didn’t turn up the allegedly inflammatory remarks.)

Apart from this, though, I’m almost done my first semester back at UW out of two. Of my courses, CS246 has been the most interesting and informative; I’d say my command line C++ skills are now significantly improved.

The big news, though, is that on May 1st I’ll be moving out from home again. I’ll still be in Waterloo, but living with some friends for a year. It’s something I’ve really been looking forward to; there might even be a good business idea when we’re all in reasonably close proximity. Significant downsizing of personal effects will be necessary, and all I’ve essentially done is trade a basement room 20 minutes away from school for a basement room 5 minutes away.

Still, exciting news and interesting times ahead.