Import Data from Excel

Importing Data from the excel sheet can be done in php on following way,

<?php

require_once ‘reader.php’;

$filename=”test.xls”;
$prod=parseExcel($filename);
echo”

";
print_r($prod);
echo"

“;
function parseExcel($excel_file_name_with_path)
{
$data = new Spreadsheet_Excel_Reader();
// Set output Encoding.
$data->setOutputEncoding(‘CP1251’);
$data->read($excel_file_name_with_path);

$colname=array(‘id’,’name’);

for ($i = 1; $i <= $data->sheets[0][‘numRows’]; $i++) {
for ($j = 1; $j <= $data->sheets[0][‘numCols’]; $j++) {

$product[$i-1][$j-1]=$data->sheets[0][‘cells’][$i][$j];
$product[$i-1][$colname[$j-1]]=$data->sheets[0][‘cells’][$i][$j];
}
}
return $product;
}
?>

The above code, will read the content from the test.xls  and import to the string which can later on to upload in the table or just print the value.

Here ‘reader.php’ used as the library to read data from the excel, that can be download from the following url.

http://www.box.net/shared/zf9zip4v41

Export to Excel:

To export data from table to excel can done same as the above except adding this header to tell the php to content will redirect to excel.

header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=test_list.xls”);
header(“Pragma: no-cache”);


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: