Link to home
Start Free TrialLog in
Avatar of PranjalShah
PranjalShahFlag for United States of America

asked on

Parse Excel Data using PHP

I am using The PHP-ExcelReader package to read the entire content of the file. I am able to extract all the contents to the webpage. But my excel files wont be the same all the time. Only the column headers will be the same. I have attached the sample excel file. What I want to extract is the column under Product SKU , Quantity and Oppr. Product from the sheet. Please guide me for this.

thanks
test.xls
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I saved this file as a csv and looked at it in a text editor.  It looks like it might be able to be worked with fgetcsv().  I will try to post a code example for you in a moment.
Avatar of PranjalShah

ASKER

Thing is user will upload the file. Can we change the file extension from xls to csv once its been uploaded?
Might not need to change the name, so long as the uploaded file has the same structure.
Dont I need to change the file extension from xls to csv to use the fgetcsv()?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Brad Brett
Brad Brett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of armina_99
armina_99

With all the different excel versions around, it would be much easier to teach the persons (perhaps using a description on your website) how to save the file as a .csv file.

A .csv file can either have tabs or ";" 's as separators. In my following code example, I will assume that you export using ; as separator.

<?php

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) 
{
    while (($data = fgetcsv($handle,1000,';')) !== FALSE) 
	{
		foreach( $data as $key => $value )
		{	// Convert all data into arrays by column
			$aValues[$key][]=$value;
		}
    }

	$column_to_find = 'test';
	foreach( $aValues as $column=>$content )
	{
		if( $content[0] == $column_to_find )
		{
			$iColumnNumber = $column;
			break;
		}
	}
	$aColumnContent = $aValues[$iColumnNumber];
	
	
    fclose($handle);

}

Open in new window