Page 1 of 1
Using Excel COM to Cells... Having Alignment Issues...
Posted: Wed Oct 20, 2010 11:16 pm
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...
...it bombs.
What am I doing wrong? Any help is appreciated.
Re: Using Excel COM to Cells... Having Alignment Issues...
Posted: Wed Oct 20, 2010 11:51 pm
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.
Re: Using Excel COM to Cells... Having Alignment Issues...
Posted: Fri Oct 22, 2010 3:33 pm
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?
Re: Using Excel COM to Cells... Having Alignment Issues...
Posted: Fri Oct 22, 2010 9:39 pm
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.