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
https://forums.phpfreaks.com/topic/173516-com-excelapplication/
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
https://forums.phpfreaks.com/topic/173516-com-excelapplication/#findComment-914660
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
https://forums.phpfreaks.com/topic/173516-com-excelapplication/#findComment-914663
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.