Link to home
Start Free TrialLog in
Avatar of Tim
TimFlag for Canada

asked on

How to set part of cell font style to Italic when using php COM to generate excel

I using php , com to generate excel file.
Now I need set some words to Italic style, like ABCD EF to  ABCD EF

Code like this
$excelfile_name="test.xls";
 $excel = new COM("excel.application") or die("Unable to instanciate excel");
$excel->Workbooks->Open("C:\template.xls");
 $book=$excel->Workbooks(1);
$sheet=$book->worksheets(1);
$sheet->activate;
$cell = $sheet->Cells(7,2) ;  
  $cell->activate;
$cell->value = 'ABCD EF';
.......(need set 'ABCD' font style to Italic)

Thanks
Avatar of rspahitz
rspahitz
Flag of United States of America image

I'm not familiar with handling this through PHP, but in Excel VBA it would be something like this:

    ActiveCell.Characters(Start:=1, Length:=4).Font.FontStyle = "Bold"
    ActiveCell.Characters(Start:=5, Length:=3).Font.FontStyle = "Regular"

Does that help?

Maybe this?

    $Cell.Characters(Start:=1, Length:=4).Font.FontStyle = "Bold"
    $Cell.Characters(Start:=5, Length:=3).Font.FontStyle = "Regular"
Avatar of Tim

ASKER

I test $Cell->Characters , it said "Fatal error: Call to a member function Characters() on a non-object in ".

Any help?
since characters is an array, maybe it needs more, like

$Cell->Characters(1,1)
?
Avatar of Tim

ASKER

Thanks.
I using  $Cell->Characters(1,4)->font->fontStyle = "Bold";
Still get the error "Fatal error: Call to a member function Characters() on a non-object in  "
Any help?
Is there any way in the tool you use to query what's valid?  In Excel, when you type the "." after activecell, a popup box appears showing valid choices ("intellisense").

(I also sent a notifcation to other Excel experts to see if any have worked with PHP)
Never worked with PHP but see of this link helps you?

http://www.codeunit.co.za/2010/07/14/phpexcel-how-to-bold-a-cells-value/

Sid
Avatar of Tim

ASKER

I Think  RQuadling may know how to do this.
Before he give me the answer about php COM excel issue.
I don't know how to ask him, could you help?
Thanks
Hi.

Just checking in ... be back in a while with what I've found.
"Fatal error: Call to a member function Characters() on a non-object in" means that $Cell is incorrect.

Can you show the rest of the code?
ASKER CERTIFIED SOLUTION
Avatar of Richard Quadling
Richard Quadling
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tim

ASKER

Thanks. It's working.
Thank you very much
Glad to have helped.
Avatar of Tim

ASKER

Hi RQuadling:
Could you check my new question.
https://www.experts-exchange.com/questions/27343581/How-to-set-cell-alignment-and-text-wrap-when-using-php-COM-to-generate-excel.html

How to set cell alignment and text wrap when using php COM to generate excel

Thanks