Jump to content

COM excel.application


micmania1

Recommended Posts

I've pulled data from a database and want to save it to an excel file.

 

So far my COm section of my script looks like this:

$excel = new COM('excel.application') or die("Excel could not be started.");
$excel->Visible = 1;
$excel->DisplayAlerts = 0;
// add a workbook
$excel->Workbooks->Add();

// save 
$Workbook = $excel->Workbooks[1];
$Workbook->SaveAs("C:\\xampp\\htdocs\\foo\\excel\\test.xlsx");

$Workbook->Worksheets->Add();
$Worksheet = $Workbook->Worksheets[1];

$cell = $Worksheet->Cells(1, 2);


$excel->Quit();

 

How do I write a value to a cell?

 

I've tried this:

$cell->Value = 'Hello, World!';

... without success.

 

Also, another problem is, the script it will be generated with is running on a new version of excel and by default creates it with the new *.xlsx format. Thus meaning when I try to save as *.xls and open the file in excel, it tells me that it isn't written in the correct format and asks if I want to still open the file. This is because the format is actually *.xlsx but the filename says *.xls.

 

Does anybody have links to any informative sites which has more info about using excel with COM?

 

Thanks :)

Link to comment
Share on other sites

I have not used PHPExcel a lot, but I tested it once and it supports also .xlsx files. This could be probably the best tool to work with excel sheets & php. Maybe give it a try ? http://phpexcel.codeplex.com/

Also I've seen one of the developers posting in these forums, a guy called Mark Baker, so it could be easy to get guidance also.

Link to comment
Share on other sites

Stupidity got the better of me here  :-[

 

I don't save after I make the change.

 

My new script for anybodies future reference:

$excel = new COM('excel.application') or die("Excel could not be started.");
$excel->Visible = 1;
$excel->DisplayAlerts = 0;
// add a workbook
$excel->Workbooks->Add();

// save 
$Workbook = $excel->Workbooks[1];

$Workbook->Worksheets->Add();
$Worksheet = $Workbook->Worksheets[1];

$cell = $Worksheet->Cells(1, 2)->Value = 'Hello, World';

$Workbook->SaveAs("C:\\excel\\test.xlsx");
$excel->Quit();

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.