Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Posted on 2011-03-10
Last Modified: 2012-08-13
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
 $excel = new COM("excel.application") or die("Unable to instanciate excel");
$cell = $sheet->Cells(7,2) ;  
$cell->value = 'ABCD EF';
.......(need set 'ABCD' font style to Italic)

Question by:Tim
  • 5
  • 4
  • 3
  • +1
LVL 22

Expert Comment

ID: 35099888
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"

Author Comment

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

Any help?
LVL 22

Expert Comment

ID: 35110053
since characters is an array, maybe it needs more, like

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Author Comment

ID: 35110134
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?
LVL 22

Expert Comment

ID: 35110253
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)
LVL 30

Expert Comment

ID: 35110259
Never worked with PHP but see of this link helps you?



Author Comment

ID: 35110295
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?
LVL 40

Expert Comment

by:Richard Quadling
ID: 35120397

Just checking in ... be back in a while with what I've found.
LVL 40

Expert Comment

by:Richard Quadling
ID: 35120419
"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?
LVL 40

Accepted Solution

Richard Quadling earned 500 total points
ID: 35120468
Here is an updated example.
ini_set('com.autoregister_casesensitive', 1); // Optional. When set wdPropertyWords does NOT equal WDPROPERTYWORDS
ini_set('com.autoregister_typelib', 1); // Auto registry the loaded typelibrary - allows access to constants.
ini_set('com.autoregister_verbose', 0); // Suppress Warning: com::com(): Type library constant emptyenum is already defined in $s on line %d messages.
// Load Excel
$o_Excel = new COM('Excel.Application') or die('Cannot load MS Excel');
// Hide Excel.
$o_Excel->Visible = False;
// Create a blank workbook
$o_Wbk = $o_Excel->Workbooks->Add();
// Shortcut to sheet 2
$o_Wks = $o_Wbk->Sheets(2);
// Shortcut to the cell(G8)
$o_Cell = $o_Wks->Cells(6,7);
// Add some text to the cell.
$o_Cell->Value = 'AB_CD_EF';

// Get Font object for 'CD'
$o_Font = $o_Cell->Characters(4,2)->Font;

// Add 20pt bold.
$o_Font->Bold = True;
$o_Font->Size = 20;

// Suppress the "Overwrite file" dialog.
$o_Excel->DisplayAlerts = False;
// Save the workbook.
// Close the workbook.
// Quit Excel.
// Tidy up - STRONGLY recommended and do in reverse order.

Open in new window

Run the code, open the workbook, look at sheet 2. Hopefully you should get the same as shown in this file : NewBook.xls

Author Closing Comment

ID: 35128333
Thanks. It's working.
Thank you very much
LVL 40

Expert Comment

by:Richard Quadling
ID: 35128901
Glad to have helped.

Author Comment

ID: 36710935
Hi RQuadling:
Could you check my new question.

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


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Random times with 12/24 hour switching 9 27
Text box keydown event does not work 38 39
Cost allcocation ... 10 21
Excel VBA 30 36
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question