473,379 Members | 1,423 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

PHP COM Excel Obj

Hope this is the right place to post :-)

I am trying to use an Excel COM object via PHP. I am able to read/write
data to cells, use AutoFilter, and AutoFit on columns. I can even set
the cell background color.

However, I am having problems with setting borders on cells and making a
column have centered text. I am able to do this with PERL. So, I am
looking for a an expert to tell me how to do it in PHP (I am a noob with
PHP).

Here is a snippet of PERL code that works:

$worksheet->Columns("c")->{ColumnWidth}=56;
my @edges = qw (xlEdgeBottom xlEdgeLeft xlEdgeRight xlEdgeTop
xlInsideHorizontal xlInsideVertical);

$range = "b1:c56";
foreach my $edge (@edges)
{
with (my $Borders =
$worksheet->Range($range)->Borders(eval($edge)), LineStyle
=>xlContinuous, Weight => xlThin, ColorIndex => 1);
}
My problem is the Borders. I have tried numerous combinations without
luck. Such as:
$workseet->Range($range)->Borders()->LineStyle = "xlContinuous";
$workseet->Range($range)->Borders("xlEdgeTop)->LineStyle = "xlContinuous";
$workseet->Range($range)->Borders()->LineStyle->Value = "xlContinuous";
$workseet->Range($range)->Borders("xlEdgeTop)->LineStyle->Value =
"xlContinuous";
....

Nothing seems to work. I am sure it is the PERL array and how I am
trying to lay the syntax out in PHP, but I am at a loss.

Thanks for your help!!
May 31 '06 #1
2 3635
I am trying to use an Excel COM object via PHP. I am able to read/write
data to cells, use AutoFilter, and AutoFit on columns. I can even set
the cell background color.

However, I am having problems with setting borders on cells and making a
column have centered text. I am able to do this with PERL. So, I am
looking for a an expert to tell me how to do it in PHP (I am a noob with
PHP). My problem is the Borders. I have tried numerous combinations without
luck. Such as: $workseet->Range($range)->Borders()->LineStyle = "xlContinuous";
$workseet->Range($range)->Borders("xlEdgeTop)->LineStyle = "xlContinuous";
$workseet->Range($range)->Borders()->LineStyle->Value = "xlContinuous";
$workseet->Range($range)->Borders("xlEdgeTop)->LineStyle->Value =
"xlContinuous"; Nothing seems to work. I am sure it is the PERL array and how I am
trying to lay the syntax out in PHP, but I am at a loss.


This is probably more of an MSExcel question, but I suppose it
overlaps...

xlEdgeTop, xlContinuous are VBA constants, and you used them correctly
in your PERL sample. You are using them incorrectly in your PHP sample.
Correcting other typos (so I assume this is not a cut-and-paste from
the actual code - tut tut!)

$worksheet->Range($range)->Borders()->LineStyle = xlContinuous;

This assumes you have previously DEFINEd xlEdgeTop and xlContinuous
somewhere, such as:

' XlBordersIndex enumerated constants
DEFINE( "xlEdgeTop", 8 );
' XlLineStyle enumerated constants
DEFINE( "xlContinuous", 1 );

---
Steve

Jun 1 '06 #2
>

This is probably more of an MSExcel question, but I suppose it
overlaps...

xlEdgeTop, xlContinuous are VBA constants, and you used them correctly
in your PERL sample. You are using them incorrectly in your PHP sample.
Correcting other typos (so I assume this is not a cut-and-paste from
the actual code - tut tut!)

$worksheet->Range($range)->Borders()->LineStyle = xlContinuous;

This assumes you have previously DEFINEd xlEdgeTop and xlContinuous
somewhere, such as:

' XlBordersIndex enumerated constants
DEFINE( "xlEdgeTop", 8 );
' XlLineStyle enumerated constants
DEFINE( "xlContinuous", 1 );

---
Steve


Thanks Steve!! That got me pointed in the right direction. I now have
things working properly. Here is an example code in case anyone is
curious. I had to look at the PERL again to see how the hash was setup
when setting the LineStyle. Once I got that syntax correct in PHP and
got the Constants defined, worked like a charm.

Thanks again!

<?php

// Example in using Excel COM Object

//Set this to where you wish to save
$xl_file = "c:/tmp/my_test.xls";

//Create new object
$XL = new COM("Excel.application") or Die ("Could not connect to Excel");

//Ignore Alerts
$XL->DisplayAlerts = 0;

//Make Excel Visible
$XL->Visible = 1;

//Create a new workbook
$WB = $XL->Workbooks->Add;

//Go to worksheet number 1
$WS = $WB->Worksheets(1);

//Make sure that worksheet is active
$WS->activate;

//Give the worksheet name
$WS->Name = "My Test";

// XlBordersIndex
DEFINE("xlEdgeTop" , 8);
DEFINE("xlEdgeBottom" , 9);
DEFINE("xlEdgeRight" , 10);
DEFINE("xlEdgeLeft" , 7);
DEFINE("xlDiagonalUp" , 6);
DEFINE("xlDiagonalDown" , 5);
DEFINE("xlInsideHorizontal", 12);
DEFINE("xlInsideVertical" , 11);

// XlLineStyle
DEFINE("xlContinuous", 1);
DEFINE("xlDash", -4115);
DEFINE("xlDot", -4118);
DEFINE("xlDashDot", 4);
DEFINE("xlDashDotDot", 5);
DEFINE("xlDouble", -4119);
DEFINE("xlSlantDashDot", 13);
DEFINE("xlLineStyleNone", -4142);

// XlBorderWeight
DEFINE("xlHaireline", 1);
DEFINE("xlMedium" , -4138);
DEFINE("xlThick" , 4);
DEFINE("xlThin" , 2);

// XlVAlign
DEFINE("xlVAlignBottom" , -4107);
DEFINE("xlVAlignCenter" , -4108);
DEFINE("xlVAlignDistributed", -4117);
DEFINE("xlVAlignJustify" , -4130);
DEFINE("xlVAlignTop" , -4160);

// Range/Column data alignment
DEFINE("xlLeft", 2);
DEFINE("xlCenter", 3);
DEFINE("xlRight", 4);

$cells = array("B2","D2","F2","H2","B4","D4","F4","H4");
$cell_data = array("Continuous","Dash","DashDot","DashDotDot",

"Dot","Double","SlantDashDot","None");
$cell_line = array(xlContinuous,xlDash,xlDashDot,xlDashDotDot,

xlDot,xlDouble,xlSlantDashDot,xlLineStyleNone);
$cell_border = array(xlEdgeTop, xlEdgeBottom, xlEdgeRight, xlEdgeLeft);

for ($i=0; $i<count($cells); $i++)
{
$cell = $WS->Range($cells[$i]);
$cell->activate;
$cell->Value = $cell_data[$i];
$cell->Interior->ColorIndex = "36";
$cell->Font->FontStyle = "Bold";
foreach ($cell_border as $cb)
{
$WS->Range($cells[$i])->Borders($cb)->LineStyle =
$cell_line[$i];
}
}

//Adjust column widths
$WS->Columns("A:H")->AutoFit;

//Cell data is considered text not numeric
$WS->Columns("A:H")->NumberFormat = "@";

//Center cell data for columns
$WS->Columns("A:H")->HorizontalAlignment = xlCenter;

//Save your new excel file
$XL->ActiveWorkbook->SaveAs($xl_file);

//Clean up and close, quit, release
$WB->Close;
unset($WS);
unset($wB);
$XL->Workbooks->Close();
$XL->Quit();
unset($XL);

?>
Jun 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Allison Bailey | last post by:
Hi Folks, I'm a brand new Python programmer, so please point me in the right direction if this is not the best forum for this question.... I would like to open an existing MS Excel spreadsheet...
3
by: Otie | last post by:
I found the following under the GetObject help notes and in the example for GetObject: "This example uses the GetObject function to get a reference to a specific Microsoft Excel worksheet...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
22
by: Howard Kaikow | last post by:
There's a significant problem in automating Excel from VB .NET. Reminds me of a problem I encountered almost 3 years ago that was caused by the Norton Auntie Virus Office plug-in. Can anybody...
9
by: Anthony | last post by:
To me, creating Excel 2003 spreadsheets programmatically via VB.NET hasn't really changed since the days of VB6. That is, I'd do something similar to this Code: Dim ExcelApp As...
7
by: Alain \Mbuna\ | last post by:
Hi everybody. In my program I have some data that is calculated after some input from the user. I have written some code that opens an Excel workbook, with 5 worksheets and the calculated data...
16
by: alexia.bee | last post by:
Hi all, In some weird reason, excel instance won;t die if i remove the comment from 4 lines of setting values into struct. here is a snipcode public...
9
by: Doug Glancy | last post by:
I got the following code from Francesco Balena's site, for disposing of Com objects: Sub SetNothing(Of T)(ByRef obj As T) ' Dispose of the object if possible If obj IsNot Nothing AndAlso...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.