Jump to content

[SOLVED] Use of the range function to go through Excel cells


suttercain

Recommended Posts

Hi guys,

 

I have been trying to access an excel spreadsheet using PHP. I finally had a break through using the following code:

 

<?php
$filename = "C:\wamp\www\Excel\\test.xls";
$sheet1 = "engine";

$excel_app = new COM("Excel.application") or Die ("Did not connect");
//print "Application name: {$excel_app->Application->value}\n" ;
//print "Loaded version: {$excel_app->Application->version}\n";
$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("A4");
$excel_cell->activate;
$excel_result = $excel_cell->value;
print "$excel_result\n";

#To close all instances of excel:
$Workbook->Close;
unset($Worksheet);
unset($Workbook);
$excel_app->Workbooks->Close();
$excel_app->Quit();
unset($excel_app);
?>

 

The problem lies within the range function. Right now I have range('A4') and that echos out cell A4, which is great, but now I would like to loop through more cells. So if I type in range("A4", "A7") I would now see all the data within those 4 cells. I know I need to use a foreach loop but I keep getting a class error.

 

Any one have any suggestions?

 

Thanks.

 

SC

Got it

 

$Workbook = $excel_app->Workbooks->Open("$filename") or Die("Did not open $filename $Workbook");
$Worksheet = $Workbook->Worksheets($sheet1);
$Worksheet->activate;
$excel_cell = $Worksheet->Range("A4", "A7");
$excel_cell->activate;
$excel_result = $excel_cell->value;
foreach ($excel_cell as $value) {
    echo "Value: $value<br />\n";
}

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.