Using Excel COM to Cells... Having Alignment Issues...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Using Excel COM to Cells... Having Alignment Issues...

Post by Wolf_22 »

Does anyone around here know how to center the contents of a given cell using PHP without the use of Excel Reader and other various libraries? I've done great so far and I have my Excel file being generated with no problems. The only remaining issue is figuring out how to center something within a column.

How is this done?

Here are some examples of things I've done so far:

Code: Select all

$directions_cell->RowHeight = '51.75';
$directions_cell->Font->Bold = 'True';
$directions_cell->WrapText = 'True';
But if I try something like the following...

Code: Select all

$directions_cell->Align = 'True';
...it bombs.

What am I doing wrong? Any help is appreciated.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Excel COM to Cells... Having Alignment Issues...

Post by requinix »

Two references you need available to work with Office stuff using COM:

1. The MSDN, either locally or online.
For Excel, the fun MSDN stuff starts here. I don't know what $directions_cell is or where it came from, but I'm guessing it's a Range object. Its HorizontalAlignment property looks useful.
Unfortunately you can't use the name "xlCenter" because it's a constant and PHP has no idea what its value is. Some Googling tells me that xlCenter=-4108. Read the whole thread - it's useful.
I suggest you put those constants into a class (hopefully something that mimics the original structures), like

Code: Select all

class Constants { // Microsoft.Office.Interop.Excel.Constants

    const xlCenter = -4108;
    // ...

}

$directions_cell->HorizontalAlignment = Constants::xlCenter;
2. The macro/script editor for whichever program.
Start recording a macro, do whatever tasks you want to automate, and stop the macro. Then look at its source code to see what it did. The names and objects will be basically the same as exposed by COM. Use Help, the Object Browser, and the editor's Intellisense.
Wolf_22
Forum Contributor
Posts: 159
Joined: Fri Dec 26, 2008 9:43 pm

Re: Using Excel COM to Cells... Having Alignment Issues...

Post by Wolf_22 »

tasairis, I just about feel over when I tried the -4180 trick... It worked!

I'm curious, though... How in the heck did someone think to use this? That number does not show within the output generated through a recording of a macro. Was this on the MSDN or something?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Excel COM to Cells... Having Alignment Issues...

Post by requinix »

The guy used a message box to display the value. Like any constant, it's a value and not a name - displaying it shows the value, not the name.

IIRC you can find all the values you need with the Object Browser. Last I knew it was the F2 button.
You start writing code in VBA/whatever it's called now, it provides a bit of contextual help, tells you what set of constants you need. You look up that set, look up specific values inside, copy them over into your PHP code, and you're done.
Post Reply