Saturday, December 3, 2011

Generate Csv Base on Excel


function curPageURL() {

require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();

//$data->read('Senator.xls');
//$data->read('Lamberts-RESELLER_PRICES.xls');


$returnarray=array();
// initialize reader object
$excel = new Spreadsheet_Excel_Reader();

// read spreadsheet data
$excel->read('Original_customer1.xls');

// iterate over spreadsheet cells and print as HTML table
$x=1;
$_count=1;
while($x<=$excel->sheets[0]['numRows']) {
//echo "\n";
$y=1;
while($y<=$excel->sheets[0]['numCols']) {
if($excel->sheets[0]['cells'][$x][$y]=='' and $y!=1):
$cell = $excel->sheets[0]['cells'][$x][$y] ;

$returnarray[$_count]=$cell;
$_count++;
else:
$cell = $excel->sheets[0]['cells'][$x][$y] ;

$returnarray[$_count]=$cell;
$_count++;

endif;
$y++;

}
//echo "\n";
$x++;
}


return $returnarray;
}
?>

$fp = fopen('completeorder2.csv', "w");
$_array= curPageURL();
//echo '
';

//print_r($_array);die;
$_heading=array_chunk($_array, 7, false);
//echo '
';

//print_r($_heading);die;
//print_r($_heading);die;
$_outercontent=count($_heading);

$_innercontent=count($_heading[0]);

//for($j=1;$j<=$_outercontent;$j++):
//for($i = $j ; ; $i++)
//{

// fwrite($fp,'"'.$_array[$i].'","'.$_array[$i+1].'","'.$_array[$i+2].'","'.$_array[$i+3].'","'.$_array[$i+4].'"');
//fwrite($fp,'"'.$_array[$i].'"');
// if($i%5==0):
// fwrite($fp, "\r\n");
// endif;


//}
foreach ($_heading as $fields) {
fputcsv($fp, $fields);
}


//endfor;
//die;
fclose($fp);
print_r('hello');
?>