Solved

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

Posted on 2011-03-10
13
822 Views
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
$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
0
Comment
Question by:zcfyhome
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 22

Expert Comment

by:rspahitz
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"
0
 

Author Comment

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

Any help?
0
 
LVL 22

Expert Comment

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

$Cell->Characters(1,1)
?
0
 

Author Comment

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

Expert Comment

by:rspahitz
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)
0
 
LVL 30

Expert Comment

by:SiddharthRout
ID: 35110259
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:zcfyhome
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?
Thanks
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 35120397
Hi.

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

Expert Comment

by:RQuadling
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?
0
 
LVL 40

Accepted Solution

by:
RQuadling earned 500 total points
ID: 35120468
Here is an updated example.
<?php
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.
$o_Wbk->SaveAs('C:\\NewBook.xls');
 
// Close the workbook.
$o_Wbk->Close();
 
// Quit Excel.
$o_Excel->Quit();
 
// Tidy up - STRONGLY recommended and do in reverse order.
unset($o_Font);
unset($o_Cell);
unset($o_Wks);
unset($o_Wbk);
unset($o_Excel);

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
0
 

Author Closing Comment

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

Expert Comment

by:RQuadling
ID: 35128901
Glad to have helped.
0
 

Author Comment

by:zcfyhome
ID: 36710935
Hi RQuadling:
Could you check my new question.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_27343581.html

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

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
excel pivot question 4 40
Facial recognition to look through the whole database for a person 3 43
Filling Blank Cells 14 19
excel help 4 18
A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now