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
LVL 8
PranjalShahAsked:
Who is Participating?
 
Ray PaseurCommented:
Well, I may have to leave this to someone else.  I tried this script and got the gibberish you can see at this link:
http://laprbass.com/RAY_temp_pranjalshah.php

I am sure there is a way to process this, or to get Excel to produce a plain-text or XML version of the spreadsheet, but I don't know how to do it.  Best of luck, ~Ray
<?php // RAY_temp_pranjalshah.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;

$fp = fopen('RAY_temp_xls.xls', 'r');
if ($fp)
{
    while (!feof($fp))
    {
        $row = fgets($fp);
        echo PHP_EOL . $row;
    }
    echo PHP_EOL . "DONE";
}
else
{
    echo "FOPEN FAILED";
}

Open in new window

0
 
Ray PaseurCommented:
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.
0
 
PranjalShahAuthor Commented:
Thing is user will upload the file. Can we change the file extension from xls to csv once its been uploaded?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
Ray PaseurCommented:
Might not need to change the name, so long as the uploaded file has the same structure.
0
 
PranjalShahAuthor Commented:
Dont I need to change the file extension from xls to csv to use the fgetcsv()?
0
 
Mohamed AbowardaSoftware EngineerCommented:
0
 
armina_99Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.