suttercain Posted August 16, 2007 Share Posted August 16, 2007 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 https://forums.phpfreaks.com/topic/65265-solved-use-of-the-range-function-to-go-through-excel-cells/ Share on other sites More sharing options...
suttercain Posted August 16, 2007 Author Share Posted August 16, 2007 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 https://forums.phpfreaks.com/topic/65265-solved-use-of-the-range-function-to-go-through-excel-cells/#findComment-325904 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.