Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Parse Excel Data using PHP

Posted on 2011-05-04
7
Medium Priority
?
265 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:PranjalShah
7 Comments
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35694153
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
 
LVL 8

Author Comment

by:PranjalShah
ID: 35694160
Thing is user will upload the file. Can we change the file extension from xls to csv once its been uploaded?
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 35694190
Might not need to change the name, so long as the uploaded file has the same structure.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Author Comment

by:PranjalShah
ID: 35694203
Dont I need to change the file extension from xls to csv to use the fgetcsv()?
0
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 35694251
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
 
LVL 12

Assisted Solution

by:Mohamed Abowarda
Mohamed Abowarda earned 1000 total points
ID: 35694547
0
 
LVL 1

Expert Comment

by:armina_99
ID: 35694630
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question