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

Link to comment
Share on other sites

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";
}

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.